[优化] 代码优化

This commit is contained in:
ryan
2026-06-01 08:56:23 +08:00
parent 95d7045b4a
commit 77ad3ea7e3
6 changed files with 48 additions and 16 deletions
+4 -10
View File
@@ -16,9 +16,6 @@ var (
LogDir = flag.String("log-dir", "", "specify the log directory")
)
// UploadPath Maybe override by ENV_VAR
var UploadPath = "upload"
func printHelp() {
fmt.Println("OpenFlare " + Version + " - Internal OpenResty Control Plane.")
fmt.Println("Copyright (C) 2023 JustSong. All rights reserved.")
@@ -28,7 +25,8 @@ func printHelp() {
func init() {
executableName := strings.ToLower(filepath.Base(os.Args[0]))
if !strings.Contains(executableName, ".test") {
isTest := strings.Contains(executableName, ".test") || flag.Lookup("test.v") != nil
if !isTest {
flag.Parse()
}
@@ -54,9 +52,7 @@ func init() {
if os.Getenv("DSN") != "" {
SQLDSN = os.Getenv("DSN")
}
if os.Getenv("UPLOAD_PATH") != "" {
UploadPath = os.Getenv("UPLOAD_PATH")
}
if os.Getenv("AGENT_TOKEN") != "" {
AgentToken = os.Getenv("AGENT_TOKEN")
}
@@ -76,7 +72,5 @@ func init() {
}
}
}
if _, err := os.Stat(UploadPath); os.IsNotExist(err) {
_ = os.Mkdir(UploadPath, 0777)
}
}
+1 -1
View File
@@ -101,7 +101,7 @@ func main() {
if common.SQLDSN != "" {
dbBackend = "postgres"
}
slog.Info("server config", "port", port, "gin_mode", gin.Mode(), "log_level", common.GetLogLevel(), "db_backend", dbBackend, "sqlite_path", common.SQLitePath, "redis_enabled", common.RedisEnabled, "upload_path", common.UploadPath, "log_dir", valueOrDefault(*common.LogDir, "stdout"), "agent_token_configured", common.AgentToken != "", "node_offline_threshold", common.NodeOfflineThreshold)
slog.Info("server config", "port", port, "gin_mode", gin.Mode(), "log_level", common.GetLogLevel(), "db_backend", dbBackend, "sqlite_path", common.SQLitePath, "redis_enabled", common.RedisEnabled, "log_dir", valueOrDefault(*common.LogDir, "stdout"), "agent_token_configured", common.AgentToken != "", "node_offline_threshold", common.NodeOfflineThreshold)
slog.Info("server listening", "address", fmt.Sprintf(":%s", port))
err = server.Run(":" + port)
if err != nil {
@@ -51,7 +51,7 @@ export const emptyDraft: WAFRuleGroupPayload = {
export const defaultRuleModalState: RuleModalState = {
open: false,
listType: 'whitelist',
listType: 'blacklist',
dimension: 'ip',
ipValue: '',
ipGroupIDs: [],
@@ -246,4 +246,46 @@ describe('WAF IP groups', () => {
expect(await screen.findByText(/IP组: edge blacklist/)).toBeInTheDocument();
});
it('defaults new rule entries to blacklist', async () => {
vi.stubGlobal(
'fetch',
vi.fn((input: RequestInfo | URL) => {
const url = String(input);
if (url.includes('/waf/rule-groups')) {
return Promise.resolve(
new Response(
JSON.stringify({
success: true,
message: '',
data: [buildRuleGroup()],
}),
),
);
}
if (url.includes('/waf/ip-groups') || url.includes('/proxy-routes/')) {
return Promise.resolve(
new Response(
JSON.stringify({ success: true, message: '', data: [] }),
),
);
}
return Promise.reject(new Error(`Unhandled fetch: ${url}`));
}),
);
renderWithProviders(<WAFPage />);
await userEvent.click(
await screen.findByRole('button', { name: /黑白名单/ }),
);
await userEvent.click(screen.getByRole('button', { name: /添加/ }));
expect(
await screen.findByRole('heading', { name: '添加黑名单规则' }),
).toBeInTheDocument();
});
});