From 82a7798b2b9c37a8d76fd25261e99ae5d4caf38e Mon Sep 17 00:00:00 2001 From: "qcqcqc@wsl" <1220204124@zust.edu.cn> Date: Tue, 15 Apr 2025 16:10:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81debug=E5=8F=82=E6=95=B0?= =?UTF-8?q?=EF=BC=8Cdebug=E6=97=B6=E4=BB=8E=E6=9C=AC=E5=9C=B0=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E8=AF=BB=E5=8F=96=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend-service/cmd/main.go | 30 +++++++++++++++++++++++++++++- shared/pkg/constants/constants.go | 1 + 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/backend-service/cmd/main.go b/backend-service/cmd/main.go index 78ba654..2005b87 100644 --- a/backend-service/cmd/main.go +++ b/backend-service/cmd/main.go @@ -31,6 +31,15 @@ func isForce() bool { return len(os.Args) > 1 && os.Args[1] == constants.ForceFlag } +func isDebug() bool { + for _, arg := range os.Args[1:] { + if arg == constants.DebugFlag { + return true + } + } + return false +} + func init() { // 初始化配置 @@ -157,7 +166,26 @@ func run() error { // initConfig 初始化配置 func initConfig() error { - viper.AddConfigPath(constants.ConfigPath) + var configDir string + + // 检查是否包含 --debug 参数 + if isDebug() { + // 调试模式:使用常量路径 + configDir = constants.ConfigPath + } else { + // 非调试模式:使用可执行文件路径拼接 + exePath, err := os.Executable() + if err != nil { + return err + } + exeDir := filepath.Dir(exePath) + configDir = filepath.Join(exeDir, constants.ConfigPath) + } + + viper.AddConfigPath(configDir) + viper.SetConfigName("config") + viper.SetConfigType("yaml") + return viper.ReadInConfig() } diff --git a/shared/pkg/constants/constants.go b/shared/pkg/constants/constants.go index 016b7e4..dc2c296 100644 --- a/shared/pkg/constants/constants.go +++ b/shared/pkg/constants/constants.go @@ -13,6 +13,7 @@ const ( PidFilePath = "/tmp/bash_service.pid" // PID 文件路径 DaemonFlag = "-daemon" // 后台进程标志 ForceFlag = "-force" // 强制结束之前的后台进程 + DebugFlag = "-debug" // 强制结束之前的后台进程 ConfigFileMode = 0644 // 文件权限 ConfigPath = "./config" // 配置文件路径 LogFileMode = 0755 // 日志文件权限