package main import ( "context" "flag" "log/slog" "os" "os/signal" "syscall" "chatappgateway/internal/app" "chatappgateway/internal/config" ) func main() { // 允许通过命令行切换配置文件,便于本地、测试和容器环境复用。 configPath := flag.String("config", config.DefaultPath, "path to config file") flag.Parse() // 启动阶段先加载配置,避免服务在半初始化状态下对外提供流量。 cfg, err := config.Load(*configPath) if err != nil { slog.Error("load config failed", "error", err) os.Exit(1) } // 使用文本日志,方便直接接入容器日志采集。 logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ Level: slog.LevelInfo, })) // 使用进程信号驱动优雅退出。 ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) defer stop() application, err := app.New(ctx, cfg, logger) if err != nil { logger.Error("build application failed", "error", err) os.Exit(1) } defer application.Close() if err := application.Run(ctx); err != nil { logger.Error("application exited with error", "error", err) os.Exit(1) } }