保存核心服务启动修复及初始化联动回归检查点

This commit is contained in:
xuans committed 2026-09-16 20:56:22 +08:00
1 parent 7d374dc10d
commit 16428f05b3
8 files changed
+87 -19

No files matched your search

+1 -6
View File
@@ -684,12 +684,7 @@ func newServeCommand() *cobra.Command {
if !cmd.Flags().Changed("bootstrap-public-ip") {
advertisedIP = os.Getenv("SCERT_BOOTSTRAP_PUBLIC_IP")
}
selectedFirewallMode := firewallMode
if !cmd.Flags().Changed("bootstrap-firewall-mode") {
if value, ok := os.LookupEnv("SCERT_BOOTSTRAP_FIREWALL_MODE"); ok {
selectedFirewallMode = value
}
}
selectedFirewallMode := bootstrapFirewallMode(cmd, firewallMode)
return serveLifecycle(ctx, cfg, reload,
func(ctx context.Context, initial config.Config) (bool, error) {
return bootstrap.Run(ctx, bootstrap.ServerOptions{
+11 -3
View File
@@ -11,6 +11,7 @@ import (
"git.pcux.cn/xuans/Scert/backend/internal/app"
"git.pcux.cn/xuans/Scert/backend/internal/config"
"github.com/spf13/cobra"
)
type serveStage func(context.Context, config.Config) (bool, error)
@@ -61,13 +62,20 @@ func serveLifecycle(ctx context.Context, cfg config.Config, reload func() (confi
if ctx.Err() != nil {
return nil
}
if !cfg.Web.Enabled {
return errors.New("管理页面已关闭")
}
// 关闭 Web 静态页面不等于停用核心 API、桌面管理和后台续期服务。
_, err := applicationStage(ctx, cfg)
return err
}
func bootstrapFirewallMode(command *cobra.Command, value string) string {
if !command.Flags().Changed("bootstrap-firewall-mode") {
if environment, ok := os.LookupEnv("SCERT_BOOTSTRAP_FIREWALL_MODE"); ok {
return environment
}
}
return value
}
func runApplication(ctx context.Context, cfg config.Config, path string) (bool, error) {
service, err := app.NewWithConfigPath(ctx, cfg, path)
if err != nil {
+40 -3
View File
@@ -38,7 +38,7 @@ func TestLoadServeConfigFailsClosed(t *testing.T) {
}
func TestServeLifecycle(t *testing.T) {
for _, name := range []string{"missing", "ready", "cancel", "bootstrap-error", "reload-error", "not-ready", "path-changed", "web-disabled", "application-error"} {
for _, name := range []string{"missing", "ready", "cancel", "bootstrap-error", "reload-error", "not-ready", "path-changed", "web-disabled", "bootstrap-web-disabled", "application-error"} {
t.Run(name, func(t *testing.T) {
cfg := config.Default(t.TempDir())
cfg.SetupReady = name == "ready" || name == "web-disabled" || name == "application-error"
@@ -53,6 +53,9 @@ func TestServeLifecycle(t *testing.T) {
}
next := cfg
next.SetupReady = name != "not-ready"
if name == "bootstrap-web-disabled" {
next.Web.Enabled = false
}
if name == "path-changed" {
next.Paths.Data += "-changed"
}
@@ -72,17 +75,20 @@ func TestServeLifecycle(t *testing.T) {
if !ready.SetupReady {
t.Fatal("未初始化时构造业务应用")
}
if (name == "web-disabled" || name == "bootstrap-web-disabled") && ready.Web.Enabled {
t.Fatal("关闭静态页面的配置不能被启动流程覆盖")
}
if name == "application-error" {
return false, errors.New("test")
}
return false, nil
})
wantError := name != "missing" && name != "ready" && name != "cancel"
wantError := name != "missing" && name != "ready" && name != "cancel" && name != "web-disabled" && name != "bootstrap-web-disabled"
if (err != nil) != wantError {
t.Fatalf("错误边界不符:%v", err)
}
wantApp := 0
if name == "missing" || name == "ready" || name == "application-error" {
if name == "missing" || name == "ready" || name == "application-error" || name == "web-disabled" || name == "bootstrap-web-disabled" {
wantApp = 1
}
if appCalls != wantApp {
@@ -97,3 +103,34 @@ func TestServeLifecycle(t *testing.T) {
})
}
}
func TestBootstrapFirewallModeFlagOverridesEnvironment(t *testing.T) {
for _, test := range []struct {
name string
environment string
flag string
want string
}{
{name: "environment", environment: "external", want: "external"},
{name: "flag", environment: "external", flag: "auto", want: "auto"},
{name: "external-flag", environment: "auto", flag: "external", want: "external"},
{name: "invalid-environment", environment: "invalid", want: "invalid"},
} {
t.Run(test.name, func(t *testing.T) {
t.Setenv("SCERT_BOOTSTRAP_FIREWALL_MODE", test.environment)
command := newServeCommand()
if test.flag != "" {
if err := command.ParseFlags([]string{"--bootstrap-firewall-mode", test.flag}); err != nil {
t.Fatal(err)
}
}
value, err := command.Flags().GetString("bootstrap-firewall-mode")
if err != nil {
t.Fatal(err)
}
if got := bootstrapFirewallMode(command, value); got != test.want {
t.Fatalf("本机策略优先级错误:得到 %q,期望 %q", got, test.want)
}
})
}
}