package main import ( "context" "errors" "net" "net/url" "os" "os/signal" "path/filepath" "strconv" "strings" "syscall" "time" "git.ipao.vip/rogee/creator-hub/internal/hub" "git.ipao.vip/rogee/creator-hub/internal/phasea" "github.com/gofiber/fiber/v3" "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/viper" ) type config struct { listenAddr string webDir string databaseURL string logLevel logrus.Level } func main() { logrus.SetFormatter(&logrus.JSONFormatter{}) if err := execute(newCommand()); err != nil { logrus.WithField("service", "control-plane").WithError(err).Error("service stopped") os.Exit(1) } } func execute(command *cobra.Command) error { ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) defer stop() return command.ExecuteContext(ctx) } func newCommand() *cobra.Command { command := &cobra.Command{ Use: "control-plane", Short: "Run the CreatorHub control plane", Args: cobra.NoArgs, SilenceErrors: true, SilenceUsage: true, RunE: func(command *cobra.Command, _ []string) error { cfg, err := loadConfig() if err != nil { return err } logrus.SetLevel(cfg.logLevel) phaseAStore, err := phasea.Open(command.Context(), cfg.databaseURL) if err != nil { return err } defer phaseAStore.Close() hubStore, err := hub.Open(command.Context(), cfg.databaseURL) if err != nil { return err } defer hubStore.Close() logStartup(cfg) heartbeatContext, stopHeartbeat := context.WithCancel(command.Context()) heartbeatDone := make(chan struct{}) go func() { defer close(heartbeatDone) runtimeLeaseHeartbeat(heartbeatContext, hubStore) }() listenErr := newHandlerWithStores(cfg.webDir, phaseAStore, hubStore).Listen(cfg.listenAddr, fiber.ListenConfig{ GracefulContext: command.Context(), DisableStartupMessage: true, }) stopHeartbeat() <-heartbeatDone return listenErr }, } return command } func runtimeLeaseHeartbeat(ctx context.Context, store hubStore) { ticker := time.NewTicker(20 * time.Second) defer ticker.Stop() for { select { case <-ctx.Done(): return case <-ticker.C: if err := reconcileRuntimeLeases(ctx, store, defaultNetworkExitProbe(), resolveExitCredential); err != nil && ctx.Err() == nil { logrus.WithField("service", "control-plane").WithError(err).Warn("runtime lease reconciliation failed") } } } } func logStartup(cfg config) { logrus.WithFields(logrus.Fields{ "service": "control-plane", "listen_addr": cfg.listenAddr, }).Info("service starting") } func loadConfig() (config, error) { v := viper.New() v.SetDefault("listen_addr", ":8080") v.SetDefault("web_dir", "web/dist") v.SetDefault("database_url", "postgres://creatorhub@127.0.0.1:5432/creatorhub?sslmode=disable") v.SetDefault("log_level", "info") _ = v.BindEnv("listen_addr", "LISTEN_ADDR") _ = v.BindEnv("web_dir", "WEB_DIR") _ = v.BindEnv("database_url", "DATABASE_URL") _ = v.BindEnv("log_level", "LOG_LEVEL") level, err := logrus.ParseLevel(v.GetString("log_level")) if err != nil { return config{}, errors.New("LOG_LEVEL must be panic, fatal, error, warn, info, debug, or trace") } cfg := config{ listenAddr: strings.TrimSpace(v.GetString("listen_addr")), webDir: strings.TrimSpace(v.GetString("web_dir")), databaseURL: strings.TrimSpace(v.GetString("database_url")), logLevel: level, } if cfg.listenAddr == "" { return config{}, errors.New("LISTEN_ADDR must not be empty") } if err := validateListenAddr(cfg.listenAddr); err != nil { return config{}, err } if cfg.webDir == "" { return config{}, errors.New("WEB_DIR must not be empty") } databaseURL, err := url.Parse(cfg.databaseURL) if err != nil || databaseURL.Host == "" || databaseURL.Path == "" || databaseURL.Path == "/" || (databaseURL.Scheme != "postgres" && databaseURL.Scheme != "postgresql") { return config{}, errors.New("DATABASE_URL must be a postgres URL with a host") } return cfg, nil } func validateListenAddr(addr string) error { _, port, err := net.SplitHostPort(addr) if err != nil { return errors.New("LISTEN_ADDR must be a host:port address") } number, err := strconv.Atoi(port) if err != nil || number < 1 || number > 65535 { return errors.New("LISTEN_ADDR port must be 1..65535") } return nil } func newHandler(webDirectory string) *fiber.App { return newHandlerWithStores(webDirectory, nil, nil) } func newHandlerWithStores(webDirectory string, phaseAStore *phasea.Store, hubStore *hub.Store) *fiber.App { app := fiber.New(fiber.Config{ AppName: "CreatorHub control plane", BodyLimit: 1 << 20, // 读超时只约束请求读取;创建/升级环境的处理器可等待网关拉取镜像(最长 11 分钟)。 ReadTimeout: 5 * time.Second, IdleTimeout: 60 * time.Second, }) app.Get("/healthz", func(c fiber.Ctx) error { c.Status(fiber.StatusNoContent) return nil }) if hubStore != nil { registerHub(app, hubStore) } if phaseAStore != nil { registerPhaseA(app, phaseAStore, hubStore) } app.Get("/*", spaHandler(webDirectory)) return app } func spaHandler(directory string) fiber.Handler { return func(c fiber.Ctx) error { path, err := url.PathUnescape(strings.TrimPrefix(c.Path(), "/")) path = filepath.Clean(path) if err == nil && filepath.IsLocal(path) { name := filepath.Join(directory, path) if info, err := os.Stat(name); err == nil && !info.IsDir() { return c.SendFile(name) } } return c.SendFile(filepath.Join(directory, "index.html")) } }