mirror of
https://github.com/skyle1995/NetworkAuth.git
synced 2026-05-25 02:24:05 +08:00
Remove the incorrect modifications
This commit is contained in:
@@ -10,7 +10,6 @@ import (
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/viper"
|
||||
"networkDev/utils"
|
||||
)
|
||||
|
||||
// ============================================================================
|
||||
@@ -174,17 +173,7 @@ func GetSecureDefaultAppConfig() (*AppConfig, error) {
|
||||
|
||||
// Init 初始化配置文件
|
||||
func Init(cfgFilePath string) {
|
||||
// 确保配置文件路径为绝对路径
|
||||
absoluteConfigPath, err := utils.EnsureAbsolutePath(cfgFilePath)
|
||||
if err != nil {
|
||||
log.WithFields(
|
||||
log.Fields{
|
||||
"err": err,
|
||||
"path": cfgFilePath,
|
||||
},
|
||||
).Fatal("转换配置文件路径为绝对路径失败")
|
||||
}
|
||||
viper.SetConfigFile(absoluteConfigPath)
|
||||
viper.SetConfigFile(cfgFilePath)
|
||||
viper.SetConfigType("json")
|
||||
viper.AddConfigPath(".")
|
||||
|
||||
@@ -215,8 +204,8 @@ func Init(cfgFilePath string) {
|
||||
return
|
||||
}
|
||||
|
||||
// 写入配置文件,使用绝对路径
|
||||
err = os.WriteFile(absoluteConfigPath, configBytes, 0o644)
|
||||
// 写入配置文件
|
||||
err = os.WriteFile(cfgFilePath, configBytes, 0o644)
|
||||
if err != nil {
|
||||
log.WithFields(
|
||||
log.Fields{
|
||||
@@ -279,12 +268,6 @@ func Init(cfgFilePath string) {
|
||||
|
||||
// CreateDefaultConfig 创建默认配置文件
|
||||
func CreateDefaultConfig(filePath string) error {
|
||||
// 确保文件路径为绝对路径
|
||||
absoluteFilePath, err := utils.EnsureAbsolutePath(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 生成带有安全密钥的默认配置
|
||||
defaultConfig, err := GetSecureDefaultAppConfig()
|
||||
if err != nil {
|
||||
@@ -302,5 +285,5 @@ func CreateDefaultConfig(filePath string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.WriteFile(absoluteFilePath, configBytes, 0o644)
|
||||
return os.WriteFile(filePath, configBytes, 0o644)
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/viper"
|
||||
"networkDev/utils"
|
||||
)
|
||||
|
||||
// ============================================================================
|
||||
@@ -135,15 +134,6 @@ func validateSQLiteConfig(config *SQLiteConfig) error {
|
||||
return errors.New("SQLite数据库路径不能为空")
|
||||
}
|
||||
|
||||
// 确保数据库路径为绝对路径
|
||||
absolutePath, err := utils.EnsureAbsolutePath(config.Path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("转换SQLite数据库路径为绝对路径失败: %w", err)
|
||||
}
|
||||
|
||||
// 更新配置中的路径为绝对路径
|
||||
config.Path = absolutePath
|
||||
|
||||
// 检查目录是否存在,不存在则创建
|
||||
dir := filepath.Dir(config.Path)
|
||||
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
||||
|
||||
@@ -3,7 +3,6 @@ package database
|
||||
import (
|
||||
"fmt"
|
||||
"networkDev/utils"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
|
||||
"github.com/glebarez/sqlite"
|
||||
@@ -92,15 +91,7 @@ func initSQLite() error {
|
||||
if path == "" {
|
||||
path = "./database.db"
|
||||
}
|
||||
|
||||
// 确保数据库路径为绝对路径
|
||||
absolutePath, err := utils.EnsureAbsolutePath(path)
|
||||
if err != nil {
|
||||
logrus.WithError(err).Error("转换SQLite数据库路径为绝对路径失败")
|
||||
return err
|
||||
}
|
||||
|
||||
dsn := fmt.Sprintf("file:%s?cache=shared&_busy_timeout=5000&_fk=1", absolutePath)
|
||||
dsn := fmt.Sprintf("file:%s?cache=shared&_busy_timeout=5000&_fk=1", path)
|
||||
db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{})
|
||||
if err != nil {
|
||||
logrus.WithError(err).Error("SQLite 初始化失败")
|
||||
@@ -115,9 +106,7 @@ func initSQLite() error {
|
||||
}
|
||||
|
||||
dbInstance = db
|
||||
// 记录连接成功信息(只显示文件名,不泄露完整路径)
|
||||
fileName := filepath.Base(absolutePath)
|
||||
logrus.WithField("file", fileName).Info("SQLite 连接已建立")
|
||||
logrus.WithField("path", path).Info("SQLite 连接已建立")
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// EnsureAbsolutePath 确保路径为绝对路径
|
||||
// 如果传入的路径已经是绝对路径,直接返回
|
||||
// 如果是相对路径,则基于当前工作目录转换为绝对路径
|
||||
func EnsureAbsolutePath(path string) (string, error) {
|
||||
if filepath.IsAbs(path) {
|
||||
return path, nil
|
||||
}
|
||||
|
||||
// 获取当前工作目录
|
||||
currentDir, err := os.Getwd()
|
||||
if err != nil {
|
||||
return "", errors.New("获取当前工作目录失败")
|
||||
}
|
||||
|
||||
// 将相对路径转换为绝对路径
|
||||
return filepath.Join(currentDir, path), nil
|
||||
}
|
||||
|
||||
// EnsureAbsolutePathWithBase 基于指定基础目录确保路径为绝对路径
|
||||
// 如果传入的路径已经是绝对路径,直接返回
|
||||
// 如果是相对路径,则基于指定的基础目录转换为绝对路径
|
||||
func EnsureAbsolutePathWithBase(path, baseDir string) (string, error) {
|
||||
if filepath.IsAbs(path) {
|
||||
return path, nil
|
||||
}
|
||||
|
||||
// 确保基础目录是绝对路径
|
||||
absBaseDir, err := EnsureAbsolutePath(baseDir)
|
||||
if err != nil {
|
||||
return "", errors.New("基础目录路径处理失败")
|
||||
}
|
||||
|
||||
// 将相对路径转换为绝对路径
|
||||
return filepath.Join(absBaseDir, path), nil
|
||||
}
|
||||
|
||||
// GetExecutableDir 获取可执行文件所在目录的绝对路径
|
||||
func GetExecutableDir() (string, error) {
|
||||
execPath, err := os.Executable()
|
||||
if err != nil {
|
||||
return "", errors.New("获取可执行文件路径失败")
|
||||
}
|
||||
return filepath.Dir(execPath), nil
|
||||
}
|
||||
|
||||
// EnsureAbsolutePathFromExecutable 基于可执行文件目录确保路径为绝对路径
|
||||
// 如果传入的路径已经是绝对路径,直接返回
|
||||
// 如果是相对路径,则基于可执行文件所在目录转换为绝对路径
|
||||
func EnsureAbsolutePathFromExecutable(path string) (string, error) {
|
||||
if filepath.IsAbs(path) {
|
||||
return path, nil
|
||||
}
|
||||
|
||||
// 获取可执行文件目录
|
||||
execDir, err := GetExecutableDir()
|
||||
if err != nil {
|
||||
// 如果获取可执行文件目录失败,回退到当前工作目录
|
||||
return EnsureAbsolutePath(path)
|
||||
}
|
||||
|
||||
// 将相对路径转换为绝对路径
|
||||
return filepath.Join(execDir, path), nil
|
||||
}
|
||||
Reference in New Issue
Block a user