U:调整数据库位置逻辑
This commit is contained in:
1 parent
a455b7cf35
commit
3bb4fc0b08
6 files changed
+71
-20
No files matched your search
@@ -17,5 +17,4 @@ logging:
|
||||
level: "info"
|
||||
|
||||
database:
|
||||
type: "sqlite"
|
||||
path: "argus.db"
|
||||
type: "sqlite"
|
||||
@@ -53,8 +53,10 @@ logging:
|
||||
database:
|
||||
# 数据库类型: sqlite, mysql (默认: sqlite)
|
||||
type: "sqlite"
|
||||
# SQLite 数据库文件路径 (默认: test.db)
|
||||
path: "test.db"
|
||||
# SQLite 数据库文件路径 (如果为空,将在运行目录下创建db/db.sqlite)
|
||||
# 支持相对路径(相对于可执行文件)和绝对路径
|
||||
# 示例: "data/my.db", "/var/lib/argus/argus.db", 或留空使用默认路径
|
||||
path: ""
|
||||
|
||||
# MySQL 配置 (仅当 type 为 mysql 时生效)
|
||||
host: "localhost"
|
||||
|
||||
@@ -33,6 +33,8 @@ type PathConfig struct {
|
||||
TempPath string
|
||||
// png 临时文件
|
||||
PngTempPath string
|
||||
// 数据库路径
|
||||
DatabasePath string
|
||||
}
|
||||
|
||||
// Config 配置结构
|
||||
@@ -109,6 +111,7 @@ func InitConfig() *Config {
|
||||
LogPath: fileConfig.Paths.LogPath,
|
||||
TempPath: fileConfig.Paths.TempPath,
|
||||
PngTempPath: fileConfig.Paths.PngTempPath,
|
||||
DatabasePath: fileConfig.Paths.DatabasePath,
|
||||
}
|
||||
|
||||
CONFIG = Config{
|
||||
@@ -148,18 +151,54 @@ func convertToImageFormat(format string) consts.ImageFormat {
|
||||
// GetDatabaseConfig 获取数据库配置
|
||||
func GetDatabaseConfig() DatabaseConfig {
|
||||
if CONFIG.FileConfig != nil {
|
||||
return CONFIG.FileConfig.Database
|
||||
dbConfig := CONFIG.FileConfig.Database
|
||||
|
||||
// 如果是SQLite且需要处理路径
|
||||
if dbConfig.Type == SQLite {
|
||||
dbConfig.Path = processSQLitePath(dbConfig.Path)
|
||||
}
|
||||
|
||||
return dbConfig
|
||||
}
|
||||
// 返回默认配置
|
||||
return DatabaseConfig{
|
||||
Type: SQLite,
|
||||
Database: "test.db",
|
||||
Path: processSQLitePath(""),
|
||||
Database: "argus",
|
||||
MaxIdleConns: 1,
|
||||
MaxOpenConns: 1,
|
||||
MaxLifetime: 0,
|
||||
}
|
||||
}
|
||||
|
||||
// processSQLitePath 处理SQLite数据库路径
|
||||
// 如果用户指定了路径,使用用户指定的
|
||||
// 如果没有指定,在运行文件根目录下创建db文件夹,并使用db.sqlite作为文件名
|
||||
func processSQLitePath(configPath string) string {
|
||||
// 如果用户指定了路径,直接使用
|
||||
if configPath != "" {
|
||||
// 如果是相对路径,转换为基于AppDir的绝对路径
|
||||
if !filepath.IsAbs(configPath) {
|
||||
return filepath.Join(CONFIG.AppDir, configPath)
|
||||
}
|
||||
return configPath
|
||||
}
|
||||
|
||||
// 用户没有指定路径,使用默认路径:AppDir/db/db.sqlite
|
||||
dbDir := filepath.Join(CONFIG.AppDir, "db")
|
||||
dbFile := filepath.Join(dbDir, "db.sqlite")
|
||||
|
||||
// 确保db目录存在
|
||||
if err := os.MkdirAll(dbDir, 0755); err != nil {
|
||||
// 如果创建目录失败,记录警告但继续使用该路径
|
||||
logger.Warn("创建数据库目录失败,但将继续使用该路径",
|
||||
zap.String("path", dbDir),
|
||||
zap.Error(err))
|
||||
}
|
||||
|
||||
return dbFile
|
||||
}
|
||||
|
||||
// IsDevelopment 判断是否为开发阶段
|
||||
// 如果配置文件中设置了 development 字段且为 true,则认为是开发阶段
|
||||
// 如果未设置或为 false,则认为是生产阶段
|
||||
|
||||
@@ -6,21 +6,21 @@ import (
|
||||
|
||||
// DatabaseConfig 数据库配置
|
||||
type DatabaseConfig struct {
|
||||
Type DatabaseType
|
||||
Type DatabaseType `yaml:"type" json:"type"`
|
||||
// Mysql 属性
|
||||
Host string
|
||||
Host string `yaml:"host" json:"host"`
|
||||
// Mysql 属性
|
||||
Port string
|
||||
Database string
|
||||
Port string `yaml:"port" json:"port"`
|
||||
Database string `yaml:"database" json:"database"`
|
||||
// Mysql 属性
|
||||
Username string
|
||||
Username string `yaml:"username" json:"username"`
|
||||
// Mysql 属性
|
||||
Password string
|
||||
// SQLite specific
|
||||
DBPath string
|
||||
MaxIdleConns int
|
||||
MaxOpenConns int
|
||||
MaxLifetime time.Duration
|
||||
Password string `yaml:"password" json:"password"`
|
||||
// SQLite specific - 数据库文件路径
|
||||
Path string `yaml:"path" json:"path"`
|
||||
MaxIdleConns int `yaml:"max_idle_conns" json:"max_idle_conns"`
|
||||
MaxOpenConns int `yaml:"max_open_conns" json:"max_open_conns"`
|
||||
MaxLifetime time.Duration `yaml:"max_lifetime" json:"max_lifetime"`
|
||||
}
|
||||
|
||||
// DatabaseType 数据库类型
|
||||
|
||||
@@ -41,6 +41,7 @@ type PathsConfig struct {
|
||||
LogPath string `yaml:"log_path" json:"log_path"`
|
||||
TempPath string `yaml:"temp_path" json:"temp_path"`
|
||||
PngTempPath string `yaml:"png_temp_path" json:"png_temp_path"`
|
||||
DatabasePath string `yaml:"database_path" json:"database_path"`
|
||||
}
|
||||
|
||||
// LoggingConfig 日志配置
|
||||
@@ -116,6 +117,7 @@ func GetDefaultFileConfig() *FileConfig {
|
||||
LogPath: "app-logs",
|
||||
TempPath: "app-tmp",
|
||||
PngTempPath: "png-tmp",
|
||||
DatabasePath: "db",
|
||||
},
|
||||
Logging: LoggingConfig{
|
||||
Level: "info",
|
||||
@@ -123,12 +125,12 @@ func GetDefaultFileConfig() *FileConfig {
|
||||
},
|
||||
Database: DatabaseConfig{
|
||||
Type: SQLite,
|
||||
Database: "test.db",
|
||||
Database: "argus",
|
||||
Host: "localhost",
|
||||
Port: "3306",
|
||||
Username: "",
|
||||
Password: "",
|
||||
DBPath: "",
|
||||
Path: "",
|
||||
MaxIdleConns: 1,
|
||||
MaxOpenConns: 1,
|
||||
MaxLifetime: 0,
|
||||
@@ -215,6 +217,9 @@ func MergeWithDefaults(fileConfig *FileConfig) *FileConfig {
|
||||
if fileConfig.Paths.PngTempPath == "" {
|
||||
fileConfig.Paths.PngTempPath = defaultConfig.Paths.PngTempPath
|
||||
}
|
||||
if fileConfig.Paths.DatabasePath == "" {
|
||||
fileConfig.Paths.DatabasePath = defaultConfig.Paths.DatabasePath
|
||||
}
|
||||
|
||||
// Logging配置合并
|
||||
if fileConfig.Logging.Level == "" {
|
||||
|
||||
@@ -232,7 +232,13 @@ func InitDatabase() error {
|
||||
})
|
||||
case config.SQLite:
|
||||
// SQLite特殊配置
|
||||
db, err = gorm.Open(sqlite.Open(databaseConfig.Database), &gorm.Config{
|
||||
dbPath := databaseConfig.Path
|
||||
if dbPath == "" {
|
||||
// 如果没有配置路径,使用默认值
|
||||
dbPath = "test.db"
|
||||
}
|
||||
|
||||
db, err = gorm.Open(sqlite.Open(dbPath), &gorm.Config{
|
||||
Logger: logger.Default.LogMode(logger.Info),
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
Reference in new issue
Block a user