diff --git a/main.go b/main.go index d71e5873..f6090181 100644 --- a/main.go +++ b/main.go @@ -16,7 +16,7 @@ var ( ) func init() { - flag.StringVar(&confPath, "c", util.RelativePath("conf.ini"), "配置文件路径") + flag.StringVar(&confPath, "c", util.RelativeExecutablePath("conf.ini"), "配置文件路径") flag.BoolVar(&isEject, "eject", false, "导出内置静态资源") flag.StringVar(&scriptName, "database-script", "", "运行内置数据库助手脚本") flag.Parse() diff --git a/pkg/conf/conf.go b/pkg/conf/conf.go index ceeb5971..6ade75f4 100644 --- a/pkg/conf/conf.go +++ b/pkg/conf/conf.go @@ -26,6 +26,7 @@ type system struct { Debug bool SessionSecret string HashIDSalt string + DataPath string } type ssl struct { diff --git a/pkg/util/path.go b/pkg/util/path.go index 24516032..60ba0a33 100644 --- a/pkg/util/path.go +++ b/pkg/util/path.go @@ -48,11 +48,23 @@ func FormSlash(old string) string { return path.Clean(strings.ReplaceAll(old, "\\", "/")) } -// RelativePath 获取相对工作目录的路径 +// RelativeExecutablePath 获取相对可执行文件的路径 +func RelativeExecutablePath(name string) string { + if filepath.IsAbs(name) { + return name + } + e, _ := os.Executable() + return filepath.Join(filepath.Dir(e), name) +} + +//RelativePath 获取相对路径 func RelativePath(name string) string { if filepath.IsAbs(name) { return name } - e, _ := os.Getwd() - return filepath.Join(e, name) + if conf.SystemConfig.DataPath != "" { + return filepath.Join(conf.SystemConfig.DataPath, name) + } else { + return RelativeExecutablePath(name) + } }