From c211a19dd878d6b0853b86efd3bf568f215c2a98 Mon Sep 17 00:00:00 2001 From: duorameng <2997944583@qq.com> Date: Wed, 13 May 2026 18:00:57 +0800 Subject: [PATCH] fix: try to fix cmd lost init envs --- cmd/resetpwd/resetpwd.go | 3 +++ internal/bootstrap/bootstrap.go | 27 ++++++++++++++++++--------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/cmd/resetpwd/resetpwd.go b/cmd/resetpwd/resetpwd.go index e906bb0..c69510a 100644 --- a/cmd/resetpwd/resetpwd.go +++ b/cmd/resetpwd/resetpwd.go @@ -71,6 +71,9 @@ func Run(args []string) { user := userService.GetUserByUsername(username) if user == nil { fmt.Printf("找不到用户 [%s]\n", username) + fmt.Println(">> 提示: 程序当前可能连接到了默认的空 SQLite 数据库。") + fmt.Println(">> 若您的生产环境使用的是 MySQL 或指定路径配置,请在执行命令时携带配置文件路径环境变量,例如:") + fmt.Println(">> BH_CONFIG_PATH=/app/data/config.ini baihu resetpwd " + username) return } diff --git a/internal/bootstrap/bootstrap.go b/internal/bootstrap/bootstrap.go index af167b2..1d593de 100644 --- a/internal/bootstrap/bootstrap.go +++ b/internal/bootstrap/bootstrap.go @@ -5,6 +5,7 @@ import ( "os" "path/filepath" "runtime" + "sync" "time" "github.com/engigu/baihu-panel/internal/constant" @@ -30,16 +31,24 @@ func New() *App { return app } +var ( + globalApp *App + initOnce sync.Once +) + func InitBasic() *App { - app := &App{} - utils.InitRuntime() - utils.InitSecretKey() - - // 自动加载配置 (内部会自动处理 BH_CONFIG_PATH 环境变量与默认路径的优先级) - app.initConfigWithPath("") - app.initDatabase() - logger.Infof("[System] 低于1.0.11版本升级最新版本错误指引: https://github.com/engigu/baihu-panel/issues/64") - return app + initOnce.Do(func() { + app := &App{} + utils.InitRuntime() + utils.InitSecretKey() + + // 自动加载配置 (内部会自动处理 BH_CONFIG_PATH 环境变量与默认路径的优先级) + app.initConfigWithPath("") + app.initDatabase() + logger.Infof("[System] 低于1.0.11版本升级最新版本错误指引: https://github.com/engigu/baihu-panel/issues/64") + globalApp = app + }) + return globalApp } // InitBasicForCmd 专为命令行工具定制的基础环境初始化入口