feat 配置文件路径从环境变量中获取

This commit is contained in:
timerzz 2024-04-12 14:51:51 +08:00
parent 6835f8bca4
commit d3ead81583
2 changed files with 9 additions and 3 deletions

View File

@ -21,8 +21,10 @@ func main() {
ctx := context.Background() ctx := context.Background()
_ctx, cancel := signal.NotifyContext(ctx, os.Kill, os.Interrupt) _ctx, cancel := signal.NotifyContext(ctx, os.Kill, os.Interrupt)
defer cancel() defer cancel()
cfg, _ := options.LoadConfig() cfg, err := options.LoadConfig()
if err != nil {
glog.Fatalf("获取配置失败:%v", err)
}
db, err := database.InitDatabase(&cfg.DB) db, err := database.InitDatabase(&cfg.DB)
if err != nil { if err != nil {
glog.Fatalf("初始化数据库失败:%v", err) glog.Fatalf("初始化数据库失败:%v", err)

View File

@ -11,8 +11,12 @@ type Config struct {
} }
func LoadConfig() (*Config, error) { func LoadConfig() (*Config, error) {
cfgPath := os.Getenv("CONFIG_PATH")
if cfgPath == "" {
cfgPath = "/data/cfg.yaml"
}
var opt Config var opt Config
f, err := os.Open("/data/cfg.yaml") f, err := os.Open(cfgPath)
if err != nil { if err != nil {
return nil, err return nil, err
} }