fix(server): 添加YMT DSN检查并支持动态端口配置

当YMT DSN为空时跳过迁移
从环境变量读取端口号,默认使用8080
This commit is contained in:
zhouyonggao 2025-11-24 17:46:28 +08:00
parent 7bc0c54e1d
commit 20aa051af4
1 changed files with 9 additions and 4 deletions

View File

@ -33,12 +33,17 @@ func main() {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
if ymtDSN != "" {
if err := migrate.Apply(ymt); err != nil { if err := migrate.Apply(ymt); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} else {
log.Println("skip migrations: YMT DSN missing")
}
r := api.NewRouter(ymt, marketing) r := api.NewRouter(ymt, marketing)
srv := &http.Server{Addr: ":8080", Handler: r, ReadTimeout: 15 * time.Second, WriteTimeout: 60 * time.Second} addr := ":" + func() string { s := os.Getenv("PORT"); if s == "" { return "8080" }; return s }()
log.Println("server listening on :8080") srv := &http.Server{Addr: addr, Handler: r, ReadTimeout: 15 * time.Second, WriteTimeout: 60 * time.Second}
log.Println("server listening on ", addr)
log.Fatal(srv.ListenAndServe()) log.Fatal(srv.ListenAndServe())
} }