@@ -0,0 +1,115 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// DBConfigFile 数据库配置文件结构
|
||||
type DBConfigFile struct {
|
||||
Type string `json:"type"` // "sqlite" 或 "mysql"
|
||||
Host string `json:"host"` // MySQL 主机
|
||||
Port string `json:"port"` // MySQL 端口
|
||||
User string `json:"user"` // MySQL 用户名
|
||||
Password string `json:"password"` // MySQL 密码
|
||||
Database string `json:"database"` // MySQL 数据库名
|
||||
TablePrefix string `json:"table_prefix"` // 表前缀
|
||||
DSN string `json:"dsn"` // SQLite 数据目录
|
||||
Initialized bool `json:"initialized"` // 是否已初始化
|
||||
}
|
||||
|
||||
var (
|
||||
configFile *DBConfigFile
|
||||
configFileLock sync.RWMutex
|
||||
configFilePath = "data/db-config.json"
|
||||
)
|
||||
|
||||
// LoadDBConfig 加载数据库配置
|
||||
func LoadDBConfig() (*DBConfigFile, error) {
|
||||
configFileLock.RLock()
|
||||
if configFile != nil {
|
||||
configFileLock.RUnlock()
|
||||
return configFile, nil
|
||||
}
|
||||
configFileLock.RUnlock()
|
||||
|
||||
configFileLock.Lock()
|
||||
defer configFileLock.Unlock()
|
||||
|
||||
// 双重检查
|
||||
if configFile != nil {
|
||||
return configFile, nil
|
||||
}
|
||||
|
||||
// 确保目录存在
|
||||
dir := filepath.Dir(configFilePath)
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
return nil, fmt.Errorf("创建配置目录失败: %w", err)
|
||||
}
|
||||
|
||||
// 读取配置文件
|
||||
data, err := os.ReadFile(configFilePath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
// 配置文件不存在,返回默认配置
|
||||
configFile = &DBConfigFile{
|
||||
Type: "mysql",
|
||||
Host: "localhost",
|
||||
Port: "3306",
|
||||
User: "root",
|
||||
Password: "",
|
||||
Database: "software_download_center",
|
||||
TablePrefix: "",
|
||||
DSN: "data",
|
||||
Initialized: false,
|
||||
}
|
||||
return configFile, nil
|
||||
}
|
||||
return nil, fmt.Errorf("读取配置文件失败: %w", err)
|
||||
}
|
||||
|
||||
configFile = &DBConfigFile{}
|
||||
if err := json.Unmarshal(data, configFile); err != nil {
|
||||
return nil, fmt.Errorf("解析配置文件失败: %w", err)
|
||||
}
|
||||
|
||||
return configFile, nil
|
||||
}
|
||||
|
||||
// SaveDBConfig 保存数据库配置
|
||||
func SaveDBConfig(config *DBConfigFile) error {
|
||||
configFileLock.Lock()
|
||||
defer configFileLock.Unlock()
|
||||
|
||||
// 确保目录存在
|
||||
dir := filepath.Dir(configFilePath)
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
return fmt.Errorf("创建配置目录失败: %w", err)
|
||||
}
|
||||
|
||||
// 序列化配置
|
||||
data, err := json.MarshalIndent(config, "", " ")
|
||||
if err != nil {
|
||||
return fmt.Errorf("序列化配置失败: %w", err)
|
||||
}
|
||||
|
||||
// 写入文件
|
||||
if err := os.WriteFile(configFilePath, data, 0644); err != nil {
|
||||
return fmt.Errorf("写入配置文件失败: %w", err)
|
||||
}
|
||||
|
||||
configFile = config
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsDBInitialized 检查数据库是否已初始化
|
||||
func IsDBInitialized() bool {
|
||||
config, err := LoadDBConfig()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return config.Initialized
|
||||
}
|
||||
Reference in New Issue
Block a user