Fix the abnormal path retrieval issue in some systems.

This commit is contained in:
2026-04-09 01:00:31 +08:00
parent 76f0d815aa
commit 792f547dc3
7 changed files with 453 additions and 336 deletions

View File

@@ -6,6 +6,8 @@ import (
"os"
"path/filepath"
"NetworkAuth/utils"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
@@ -107,7 +109,7 @@ func GetDefaultAppConfig() *AppConfig {
MaxOpenConns: 100,
},
SQLite: SQLiteConfig{
Path: "./database.db",
Path: "database.db",
},
},
Redis: RedisConfig{
@@ -118,7 +120,7 @@ func GetDefaultAppConfig() *AppConfig {
},
Log: LogConfig{
Level: "info",
File: "./logs/app.log",
File: "logs/app.log",
MaxSize: 100,
MaxBackups: 5,
MaxAge: 30,
@@ -128,6 +130,9 @@ func GetDefaultAppConfig() *AppConfig {
// Init 初始化配置文件
func Init(cfgFilePath string) {
if !filepath.IsAbs(cfgFilePath) {
cfgFilePath = filepath.Join(utils.GetRootDir(), cfgFilePath)
}
currentConfigFilePath = cfgFilePath
viper.SetConfigFile(cfgFilePath)
viper.SetConfigType("json")
@@ -204,7 +209,10 @@ func SaveConfig(appConfig *AppConfig) error {
return err
}
if currentConfigFilePath == "" {
currentConfigFilePath = "./config.json"
currentConfigFilePath = "config.json"
}
if !filepath.IsAbs(currentConfigFilePath) {
currentConfigFilePath = filepath.Join(utils.GetRootDir(), currentConfigFilePath)
}
if err := os.MkdirAll(filepath.Dir(currentConfigFilePath), 0755); err != nil {
return err

View File

@@ -9,6 +9,8 @@ import (
"strconv"
"strings"
"NetworkAuth/utils"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
@@ -125,8 +127,13 @@ func validateSQLiteConfig(config *SQLiteConfig) error {
return errors.New("SQLite数据库路径不能为空")
}
path := config.Path
if !filepath.IsAbs(path) {
path = filepath.Join(utils.GetRootDir(), path)
}
// 检查目录是否存在,不存在则创建
dir := filepath.Dir(config.Path)
dir := filepath.Dir(path)
if _, err := os.Stat(dir); os.IsNotExist(err) {
if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("创建SQLite数据库目录失败: %w", err)
@@ -160,7 +167,11 @@ func validateLogConfig(config *LogConfig) error {
// 检查日志文件目录(仅当日志文件路径不为空时)
if config.File != "" {
dir := filepath.Dir(config.File)
path := config.File
if !filepath.IsAbs(path) {
path = filepath.Join(utils.GetRootDir(), path)
}
dir := filepath.Dir(path)
if _, err := os.Stat(dir); os.IsNotExist(err) {
if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("创建日志目录失败: %w", err)