// Package main 是服务启动入口。 // // 启动前的配置兜底:本仓库不提供默认的 config.yaml(仅按环境提供 // config.dev.yaml / config.test.yaml / config.prod.yaml),框架默认却要找 // config.yaml,导致直接运行时报「config not found」。这里在加载配置前, // 若未通过 GF_GCFG_FILE 指定文件名,则自动回落到 config.dev.yaml。 // // 因此以下方式均可直接启动,无需手动传环境变量: // - GoLand 直接点运行(含自动生成的临时配置) // - 命令行 go run main.go / 双击二进制 // // 如需切换环境,显式设置环境变量即可覆盖默认行为: // // GF_GCFG_FILE=config.test.yaml # 或 config.prod.yaml package main import ( _ "github.com/gogf/gf/contrib/drivers/mysql/v2" // MySQL 驱动,gdb 必需 "github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/genv" "service.xpcool.com/internal/cmd" ) const ( // envKeyForFile 是 GoFrame 官方的「配置文件名」环境变量。 // 框架在初始化文件适配器(gcfg.NewAdapterFile)时读取它,用于决定加载哪个配置文件。 envKeyForFile = "GF_GCFG_FILE" // envKeyForPath 是 GoFrame 官方的「配置文件目录」环境变量。 envKeyForPath = "GF_GCFG_PATH" // defaultConfigFile 是未显式指定配置文件时使用的默认文件名(开发环境)。 defaultConfigFile = "config.dev.yaml" ) func main() { injectDefaultConfigFile() cmd.Main.Run(gctx.GetInitCtx()) } // injectDefaultConfigFile 在配置系统初始化前,兜底指定默认配置文件。 // // 仅在「调用方未显式指定配置文件或目录」时才写入默认值, // 以免覆盖使用者通过环境变量或命令行做出的选择。 func injectDefaultConfigFile() { if !genv.Get(envKeyForFile).IsEmpty() || !genv.Get(envKeyForPath).IsEmpty() { return } _ = genv.Set(envKeyForFile, defaultConfigFile) }