mirror of
https://github.com/skyle1995/NetworkAuth.git
synced 2026-05-25 02:24:05 +08:00
修改项目为前后端分离方案
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
appconfig "NetworkAuth/config"
|
||||
"NetworkAuth/utils"
|
||||
"context"
|
||||
"fmt"
|
||||
@@ -40,7 +41,7 @@ var (
|
||||
func Init() (*gorm.DB, error) {
|
||||
var initErr error
|
||||
once.Do(func() {
|
||||
initErr = performInit()
|
||||
initErr = performInitFromViper()
|
||||
})
|
||||
return dbInstance, initErr
|
||||
}
|
||||
@@ -57,83 +58,101 @@ func GetDB() (*gorm.DB, error) {
|
||||
// ReInit 重新初始化数据库连接
|
||||
// 用于在修改配置后重新连接数据库
|
||||
func ReInit() (*gorm.DB, error) {
|
||||
// 如果已有连接,尝试关闭它
|
||||
if dbInstance != nil {
|
||||
if healthCheckCancel != nil {
|
||||
healthCheckCancel()
|
||||
healthCheckCancel = nil
|
||||
}
|
||||
if sqlDB, err := dbInstance.DB(); err == nil {
|
||||
sqlDB.Close()
|
||||
}
|
||||
}
|
||||
dbInstance = nil
|
||||
closeCurrentDB()
|
||||
|
||||
// 重新执行初始化逻辑(不经过 once.Do)
|
||||
return dbInstance, performInit()
|
||||
// 在 ReInit 时,强制从 viper 重新读取配置并连接,忽略"系统尚未安装"的检查
|
||||
// 因为这是安装过程触发的
|
||||
var cfg appconfig.AppConfig
|
||||
if err := viper.Unmarshal(&cfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := performInitWithConfig(&cfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if dbInstance == nil {
|
||||
return nil, fmt.Errorf("数据库实例初始化后为空")
|
||||
}
|
||||
|
||||
return dbInstance, nil
|
||||
}
|
||||
|
||||
func performInit() error {
|
||||
// 检查是否已经有配置文件(通过检查文件是否存在)
|
||||
func InitWithAppConfig(cfg *appconfig.AppConfig) (*gorm.DB, error) {
|
||||
closeCurrentDB()
|
||||
if err := performInitWithConfig(cfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return dbInstance, nil
|
||||
}
|
||||
|
||||
func performInitFromViper() error {
|
||||
configFile := viper.ConfigFileUsed()
|
||||
// 如果 viper 没有使用配置文件(可能是因为没找到文件而使用了默认配置),
|
||||
// 或者配置文件路径为空,我们应该假设处于未安装状态。
|
||||
// 但 viper.ConfigFileUsed() 在 ReadInConfig 成功后会返回文件名。
|
||||
// 如果 ReadInConfig 失败(因为文件不存在),viper 可能会返回空或者我们在 config.go 中设置的路径。
|
||||
|
||||
// 在 config.go 中,如果文件不存在,我们加载了默认配置但没有写文件。
|
||||
// 此时 viper.ConfigFileUsed() 可能是空的或者我们设置的路径。
|
||||
// 让我们检查该路径对应的文件是否存在。
|
||||
|
||||
if configFile == "" {
|
||||
configFile = "config.json"
|
||||
}
|
||||
|
||||
_, err := os.Stat(configFile)
|
||||
isConfigExists := !os.IsNotExist(err)
|
||||
|
||||
// 如果配置文件不存在,说明还没有经过安装初始化,暂时不连接数据库
|
||||
if !isConfigExists {
|
||||
logrus.Info("尚未初始化配置,跳过数据库连接")
|
||||
return nil
|
||||
// 从 viper 中读取配置
|
||||
var cfg appconfig.AppConfig
|
||||
if err := viper.Unmarshal(&cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var initErr error
|
||||
dbType := viper.GetString("database.type")
|
||||
switch dbType {
|
||||
// 检查数据库类型,如果文件或配置不存在,说明系统尚未安装,跳过数据库连接
|
||||
switch cfg.Database.Type {
|
||||
case "sqlite":
|
||||
dbPath := cfg.Database.SQLite.Path
|
||||
if dbPath == "" {
|
||||
dbPath = "./database.db"
|
||||
}
|
||||
if _, err := os.Stat(dbPath); os.IsNotExist(err) {
|
||||
logrus.Info("SQLite 数据库文件不存在,系统尚未安装,跳过数据库连接")
|
||||
return nil
|
||||
}
|
||||
case "mysql":
|
||||
initErr = initMySQL()
|
||||
// 只有在明确配置了 host 并且不是安装请求时才去连接 MySQL
|
||||
// 我们通过检查是否已有有效配置来判断,比如检查 database 是否为空
|
||||
if cfg.Database.MySQL.Database == "" {
|
||||
logrus.Info("MySQL 数据库名称未配置,说明系统尚未安装,跳过数据库连接")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return performInitWithConfig(&cfg)
|
||||
}
|
||||
|
||||
func performInitWithConfig(cfg *appconfig.AppConfig) error {
|
||||
if cfg == nil {
|
||||
return fmt.Errorf("应用配置不能为空")
|
||||
}
|
||||
if err := appconfig.ValidateConfigValue(cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
var initErr error
|
||||
switch cfg.Database.Type {
|
||||
case "mysql":
|
||||
initErr = initMySQL(&cfg.Database.MySQL, cfg.Log.Level)
|
||||
if initErr != nil {
|
||||
logrus.WithError(initErr).Error("MySQL 数据库连接失败,请检查配置或重新安装")
|
||||
// 既然 MySQL 连不上,说明系统无法正常工作,直接返回错误,由外层决定是否退出
|
||||
return initErr
|
||||
}
|
||||
default:
|
||||
initErr = initSQLite()
|
||||
initErr = initSQLite(&cfg.Database.SQLite, cfg.Log.Level)
|
||||
}
|
||||
|
||||
// 如果数据库初始化成功,配置连接池和启动健康检查
|
||||
if initErr == nil && dbInstance != nil {
|
||||
// 加载数据库配置
|
||||
var configPrefix string
|
||||
if dbType == "mysql" {
|
||||
configPrefix = "database.mysql"
|
||||
} else {
|
||||
configPrefix = "database.sqlite"
|
||||
}
|
||||
|
||||
dbConfig := utils.LoadDatabaseConfig(configPrefix)
|
||||
|
||||
// 验证配置
|
||||
if err := utils.ValidateDatabaseConfig(dbConfig); err != nil {
|
||||
logrus.WithError(err).Warn("数据库配置验证失败,使用默认配置")
|
||||
dbConfig = utils.GetDefaultDatabaseConfig()
|
||||
}
|
||||
|
||||
// 配置连接池
|
||||
if err := utils.ConfigureConnectionPool(dbInstance, dbConfig); err != nil {
|
||||
logrus.WithError(err).Error("配置数据库连接池失败")
|
||||
}
|
||||
|
||||
// 启动健康检查
|
||||
healthCheckCancel = utils.StartHealthCheck(dbInstance, dbConfig)
|
||||
if initErr != nil || dbInstance == nil {
|
||||
return initErr
|
||||
}
|
||||
return initErr
|
||||
dbConfig := buildPoolConfig(cfg)
|
||||
if err := utils.ValidateDatabaseConfig(dbConfig); err != nil {
|
||||
logrus.WithError(err).Warn("数据库配置验证失败,使用默认配置")
|
||||
dbConfig = utils.GetDefaultDatabaseConfig()
|
||||
}
|
||||
if err := utils.ConfigureConnectionPool(dbInstance, dbConfig); err != nil {
|
||||
logrus.WithError(err).Error("配置数据库连接池失败")
|
||||
}
|
||||
healthCheckCancel = utils.StartHealthCheck(dbInstance, dbConfig)
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetDB 设置全局 *gorm.DB 实例(用于测试)
|
||||
@@ -145,16 +164,38 @@ func SetDB(db *gorm.DB) {
|
||||
// 私有函数
|
||||
// ============================================================================
|
||||
|
||||
// initSQLite 初始化 SQLite 数据库
|
||||
// 使用 viper 中的 database.sqlite.path 作为数据库文件路径
|
||||
func initSQLite() error {
|
||||
path := viper.GetString("database.sqlite.path")
|
||||
if path == "" {
|
||||
path = "./recharge.db"
|
||||
func closeCurrentDB() {
|
||||
if healthCheckCancel != nil {
|
||||
healthCheckCancel()
|
||||
healthCheckCancel = nil
|
||||
}
|
||||
dsn := fmt.Sprintf("file:%s?cache=shared&_busy_timeout=5000&_fk=1", path)
|
||||
if dbInstance != nil {
|
||||
if sqlDB, err := dbInstance.DB(); err == nil {
|
||||
sqlDB.Close()
|
||||
}
|
||||
}
|
||||
dbInstance = nil
|
||||
}
|
||||
|
||||
func buildPoolConfig(cfg *appconfig.AppConfig) *utils.DatabaseConfig {
|
||||
dbConfig := utils.GetDefaultDatabaseConfig()
|
||||
if cfg.Database.Type == "mysql" {
|
||||
if cfg.Database.MySQL.MaxIdleConns > 0 {
|
||||
dbConfig.MaxIdleConns = cfg.Database.MySQL.MaxIdleConns
|
||||
}
|
||||
if cfg.Database.MySQL.MaxOpenConns > 0 {
|
||||
dbConfig.MaxOpenConns = cfg.Database.MySQL.MaxOpenConns
|
||||
}
|
||||
return dbConfig
|
||||
}
|
||||
dbConfig.MaxIdleConns = 1
|
||||
dbConfig.MaxOpenConns = 1
|
||||
return dbConfig
|
||||
}
|
||||
|
||||
func buildGormLogger(level string) gLogger.Interface {
|
||||
var logLevel gLogger.LogLevel
|
||||
switch viper.GetString("logger.level") {
|
||||
switch level {
|
||||
case "debug":
|
||||
logLevel = gLogger.Info
|
||||
case "error":
|
||||
@@ -162,55 +203,45 @@ func initSQLite() error {
|
||||
default:
|
||||
logLevel = gLogger.Warn
|
||||
}
|
||||
gl := gLogger.New(log.New(os.Stdout, "\r\n", log.LstdFlags), gLogger.Config{SlowThreshold: 2 * time.Second, LogLevel: logLevel, IgnoreRecordNotFoundError: true, Colorful: false})
|
||||
db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{Logger: gl})
|
||||
return gLogger.New(log.New(os.Stdout, "\r\n", log.LstdFlags), gLogger.Config{SlowThreshold: 2 * time.Second, LogLevel: logLevel, IgnoreRecordNotFoundError: true, Colorful: false})
|
||||
}
|
||||
|
||||
func initSQLite(sqliteConfig *appconfig.SQLiteConfig, logLevel string) error {
|
||||
path := sqliteConfig.Path
|
||||
if path == "" {
|
||||
path = "./database.db"
|
||||
}
|
||||
dsn := fmt.Sprintf("file:%s?cache=shared&_busy_timeout=5000&_fk=1", path)
|
||||
db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{Logger: buildGormLogger(logLevel)})
|
||||
if err != nil {
|
||||
logrus.WithError(err).Error("SQLite 初始化失败")
|
||||
return err
|
||||
}
|
||||
|
||||
// SQLite 连接池配置(SQLite 对连接池支持有限,但仍可设置基本参数)
|
||||
if sqlDB, err := db.DB(); err == nil {
|
||||
// SQLite 通常使用单连接,但可以设置一些基本参数
|
||||
sqlDB.SetMaxOpenConns(1) // SQLite 建议使用单连接
|
||||
sqlDB.SetMaxOpenConns(1)
|
||||
sqlDB.SetMaxIdleConns(1)
|
||||
}
|
||||
|
||||
dbInstance = db
|
||||
logrus.WithField("path", path).Info("SQLite 连接已建立")
|
||||
return nil
|
||||
}
|
||||
|
||||
// initMySQL 初始化 MySQL 数据库
|
||||
// 从 viper 读取 database.mysql.* 配置构建 DSN
|
||||
func initMySQL() error {
|
||||
host := viper.GetString("database.mysql.host")
|
||||
port := viper.GetInt("database.mysql.port")
|
||||
user := viper.GetString("database.mysql.username")
|
||||
pass := viper.GetString("database.mysql.password")
|
||||
dbname := viper.GetString("database.mysql.database")
|
||||
charset := viper.GetString("database.mysql.charset")
|
||||
func initMySQL(mysqlConfig *appconfig.MySQLConfig, logLevel string) error {
|
||||
host := mysqlConfig.Host
|
||||
port := mysqlConfig.Port
|
||||
user := mysqlConfig.Username
|
||||
pass := mysqlConfig.Password
|
||||
dbname := mysqlConfig.Database
|
||||
charset := mysqlConfig.Charset
|
||||
if charset == "" {
|
||||
charset = "utf8mb4"
|
||||
}
|
||||
|
||||
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=True&loc=Local", user, pass, host, port, dbname, charset)
|
||||
var logLevel gLogger.LogLevel
|
||||
switch viper.GetString("logger.level") {
|
||||
case "debug":
|
||||
logLevel = gLogger.Info
|
||||
case "error":
|
||||
logLevel = gLogger.Error
|
||||
default:
|
||||
logLevel = gLogger.Warn
|
||||
}
|
||||
gl := gLogger.New(log.New(os.Stdout, "\r\n", log.LstdFlags), gLogger.Config{SlowThreshold: 2 * time.Second, LogLevel: logLevel, IgnoreRecordNotFoundError: true, Colorful: false})
|
||||
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{Logger: gl})
|
||||
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{Logger: buildGormLogger(logLevel)})
|
||||
if err != nil {
|
||||
logrus.WithError(err).Error("MySQL 初始化失败")
|
||||
return err
|
||||
}
|
||||
|
||||
dbInstance = db
|
||||
logrus.WithField("host", host).WithField("database", dbname).Info("MySQL 连接已建立")
|
||||
return nil
|
||||
|
||||
@@ -18,11 +18,11 @@ func AutoMigrate() error {
|
||||
&models.Settings{},
|
||||
&models.OperationLog{},
|
||||
&models.LoginLog{},
|
||||
&models.User{},
|
||||
&models.App{},
|
||||
&models.API{},
|
||||
&models.Function{},
|
||||
&models.Variable{},
|
||||
&models.User{},
|
||||
&models.Function{},
|
||||
); err != nil {
|
||||
logrus.WithError(err).Error("AutoMigrate 执行失败")
|
||||
return err
|
||||
|
||||
@@ -1,232 +1,329 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"NetworkAuth/config"
|
||||
"NetworkAuth/models"
|
||||
"NetworkAuth/utils"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// ============================================================================
|
||||
// 公共函数
|
||||
// ============================================================================
|
||||
|
||||
// SeedDefaultSettings 初始化默认系统设置
|
||||
// - 检查各项设置是否已存在,如不存在则创建默认值
|
||||
// - 包含站点基本信息、SEO设置等常用配置项
|
||||
func SeedDefaultSettings() error {
|
||||
db, err := GetDB()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 生成安全的随机密钥
|
||||
jwtSecret, err := config.GenerateSecureJWTSecret()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
encryptionKey, err := config.GenerateSecureEncryptionKey()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 生成默认管理员密码(admin123)的盐值和哈希
|
||||
// 这样可以确保admin_password和admin_password_salt在初始化时就有值
|
||||
adminSalt, err := utils.GenerateRandomSalt()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
adminPasswordHash, err := utils.HashPasswordWithSalt("admin123", adminSalt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 检查是否已有 admin_password,如果有,说明是旧版本升级,应该把 is_installed 默认设为 1
|
||||
var adminPwdCount int64
|
||||
db.Model(&models.Settings{}).Where("name = ?", "admin_password").Count(&adminPwdCount)
|
||||
isInstalledDefault := "0"
|
||||
if adminPwdCount > 0 {
|
||||
isInstalledDefault = "1"
|
||||
}
|
||||
|
||||
// 定义默认设置项
|
||||
defaultSettings := []models.Settings{
|
||||
// ===== 系统安装状态 =====
|
||||
{
|
||||
Name: "is_installed",
|
||||
Value: isInstalledDefault,
|
||||
Description: "系统是否已初始化安装,0=未安装,1=已安装",
|
||||
},
|
||||
// ===== 管理员账号相关默认项 =====
|
||||
{
|
||||
Name: "admin_username",
|
||||
Value: "admin",
|
||||
Description: "管理员用户名",
|
||||
},
|
||||
{
|
||||
Name: "admin_password",
|
||||
Value: adminPasswordHash,
|
||||
Description: "管理员密码哈希值",
|
||||
},
|
||||
{
|
||||
Name: "admin_password_salt",
|
||||
Value: adminSalt,
|
||||
Description: "管理员密码加密盐值",
|
||||
},
|
||||
// ===== 系统和安全相关默认项 =====
|
||||
{
|
||||
Name: "maintenance_mode",
|
||||
Value: "0",
|
||||
Description: "维护模式,0=关闭维护模式,1=开启维护模式",
|
||||
},
|
||||
{
|
||||
Name: "encryption_key",
|
||||
Value: encryptionKey,
|
||||
Description: "数据加密密钥",
|
||||
},
|
||||
{
|
||||
Name: "jwt_secret",
|
||||
Value: jwtSecret,
|
||||
Description: "JWT签名密钥",
|
||||
},
|
||||
{
|
||||
Name: "jwt_refresh",
|
||||
Value: "6",
|
||||
Description: "JWT令牌刷新阈值(小时)",
|
||||
},
|
||||
{
|
||||
Name: "jwt_expire",
|
||||
Value: "24",
|
||||
Description: "JWT令牌有效期(小时)",
|
||||
},
|
||||
{
|
||||
Name: "session_timeout",
|
||||
Value: "3600",
|
||||
Description: "会话超时时间(秒),默认1小时",
|
||||
},
|
||||
{
|
||||
Name: "max_upload_size",
|
||||
Value: "10485760",
|
||||
Description: "文件上传最大尺寸(字节),默认10MB",
|
||||
},
|
||||
{
|
||||
Name: "default_user_role",
|
||||
Value: "1",
|
||||
Description: "新用户默认角色,0=管理员,1=普通用户",
|
||||
},
|
||||
// ===== 日志清理策略默认项 =====
|
||||
{
|
||||
Name: "login_log_cleanup_days",
|
||||
Value: "30",
|
||||
Description: "登录日志保留天数(0表示不按天清理)",
|
||||
},
|
||||
{
|
||||
Name: "login_log_cleanup_limit",
|
||||
Value: "10000",
|
||||
Description: "登录日志保留条数(0表示不按数量清理)",
|
||||
},
|
||||
{
|
||||
Name: "operation_log_cleanup_days",
|
||||
Value: "30",
|
||||
Description: "操作日志保留天数(0表示不按天清理)",
|
||||
},
|
||||
{
|
||||
Name: "operation_log_cleanup_limit",
|
||||
Value: "10000",
|
||||
Description: "操作日志保留条数(0表示不按数量清理)",
|
||||
},
|
||||
// ===== Cookie相关默认项 =====
|
||||
{
|
||||
Name: "cookie_secure",
|
||||
Value: "true",
|
||||
Description: "Cookie Secure属性(是否只在HTTPS下发送)",
|
||||
},
|
||||
{
|
||||
Name: "cookie_same_site",
|
||||
Value: "Lax",
|
||||
Description: "Cookie SameSite属性(Strict/Lax/None)",
|
||||
},
|
||||
{
|
||||
Name: "cookie_domain",
|
||||
Value: "",
|
||||
Description: "Cookie域名",
|
||||
},
|
||||
{
|
||||
Name: "cookie_max_age",
|
||||
Value: "86400",
|
||||
Description: "Cookie最大存活时间(秒)",
|
||||
},
|
||||
// ===== 站点基本信息默认项 =====
|
||||
{
|
||||
Name: "site_title",
|
||||
Value: "NetworkAuth",
|
||||
Description: "网站标题,显示在浏览器标题栏和页面顶部",
|
||||
},
|
||||
{
|
||||
Name: "site_keywords",
|
||||
Value: "NetworkAuth,鉴权,API管理,GoLang",
|
||||
Description: "网站关键词,用于SEO优化,多个关键词用逗号分隔",
|
||||
},
|
||||
{
|
||||
Name: "site_description",
|
||||
Value: "NetworkAuth 网络授权服务,专注于应用鉴权与接口管理",
|
||||
Description: "网站描述,用于SEO优化和社交媒体分享",
|
||||
},
|
||||
{
|
||||
Name: "site_logo",
|
||||
Value: "/static/logo.png",
|
||||
Description: "网站Logo图片路径",
|
||||
},
|
||||
{
|
||||
Name: "contact_email",
|
||||
Value: "admin@example.com",
|
||||
Description: "联系邮箱,用于客服和业务咨询",
|
||||
},
|
||||
// ===== 页脚与备案相关默认项 =====
|
||||
{
|
||||
Name: "footer_text",
|
||||
Value: "Copyright © 2026 NetworkAuth. All Rights Reserved.",
|
||||
Description: "页脚展示的版权或说明信息",
|
||||
},
|
||||
{
|
||||
Name: "icp_record",
|
||||
Value: "",
|
||||
Description: "ICP备案号,留空则不显示",
|
||||
},
|
||||
{
|
||||
Name: "icp_record_link",
|
||||
Value: "https://beian.miit.gov.cn",
|
||||
Description: "工信部ICP备案查询链接,留空则不显示",
|
||||
},
|
||||
{
|
||||
Name: "psb_record",
|
||||
Value: "",
|
||||
Description: "公安备案号,留空则不显示",
|
||||
},
|
||||
{
|
||||
Name: "psb_record_link",
|
||||
Value: "",
|
||||
Description: "公安备案查询链接,留空则不显示",
|
||||
},
|
||||
}
|
||||
|
||||
// 逐个检查并创建不存在的设置项
|
||||
for _, setting := range defaultSettings {
|
||||
var count int64
|
||||
if err := db.Model(&models.Settings{}).Where("name = ?", setting.Name).Count(&count).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if count == 0 {
|
||||
if err := db.Create(&setting).Error; err != nil {
|
||||
logrus.WithError(err).WithField("name", setting.Name).Error("创建默认设置失败")
|
||||
return err
|
||||
}
|
||||
logrus.WithField("name", setting.Name).WithField("value", setting.Value).Debug("创建默认设置项")
|
||||
}
|
||||
}
|
||||
|
||||
logrus.Info("默认系统设置初始化完成")
|
||||
return nil
|
||||
}
|
||||
package database
|
||||
|
||||
import (
|
||||
"NetworkAuth/config"
|
||||
"NetworkAuth/models"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// ============================================================================
|
||||
// 公共函数
|
||||
// ============================================================================
|
||||
|
||||
// SeedDefaultSettings 初始化默认系统设置
|
||||
// - 检查各项设置是否已存在,如不存在则创建默认值
|
||||
// - 包含站点基本信息、SEO设置等常用配置项
|
||||
func SeedDefaultSettings() error {
|
||||
db, err := GetDB()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 生成安全的随机密钥
|
||||
jwtSecret, err := config.GenerateSecureJWTSecret()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
encryptionKey, err := config.GenerateSecureEncryptionKey()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
isInstalledDefault := "0"
|
||||
|
||||
// 定义默认设置项
|
||||
var defaultSettings []models.Settings
|
||||
|
||||
// ===== 系统安装状态 =====
|
||||
defaultSettings = append(defaultSettings, []models.Settings{
|
||||
{
|
||||
Name: "is_installed",
|
||||
Value: isInstalledDefault,
|
||||
Description: "系统是否已初始化安装,0=未安装,1=已安装",
|
||||
},
|
||||
}...)
|
||||
|
||||
// ===== 系统和安全相关默认项 =====
|
||||
defaultSettings = append(defaultSettings, []models.Settings{
|
||||
{
|
||||
Name: "maintenance_mode",
|
||||
Value: "0",
|
||||
Description: "维护模式,0=关闭维护模式,1=开启维护模式",
|
||||
},
|
||||
{
|
||||
Name: "encryption_key",
|
||||
Value: encryptionKey,
|
||||
Description: "数据加密密钥",
|
||||
},
|
||||
{
|
||||
Name: "jwt_secret",
|
||||
Value: jwtSecret,
|
||||
Description: "JWT签名密钥",
|
||||
},
|
||||
{
|
||||
Name: "jwt_refresh",
|
||||
Value: "6",
|
||||
Description: "JWT令牌刷新阈值(小时)",
|
||||
},
|
||||
{
|
||||
Name: "jwt_expire",
|
||||
Value: "24",
|
||||
Description: "JWT令牌有效期(小时)",
|
||||
},
|
||||
{
|
||||
Name: "session_timeout",
|
||||
Value: "3600",
|
||||
Description: "会话超时时间(秒),默认1小时",
|
||||
},
|
||||
{
|
||||
Name: "max_upload_size",
|
||||
Value: "10",
|
||||
Description: "文件上传最大尺寸",
|
||||
},
|
||||
{
|
||||
Name: "max_upload_size_unit",
|
||||
Value: "MB",
|
||||
Description: "文件上传大小单位(B/KB/MB/GB)",
|
||||
},
|
||||
}...)
|
||||
|
||||
// ===== 日志清理策略默认项 =====
|
||||
defaultSettings = append(defaultSettings, []models.Settings{
|
||||
{
|
||||
Name: "login_log_cleanup_days",
|
||||
Value: "30",
|
||||
Description: "登录日志保留天数(0表示不按天清理)",
|
||||
},
|
||||
{
|
||||
Name: "login_log_cleanup_limit",
|
||||
Value: "10000",
|
||||
Description: "登录日志保留条数(0表示不按数量清理)",
|
||||
},
|
||||
{
|
||||
Name: "operation_log_cleanup_days",
|
||||
Value: "30",
|
||||
Description: "操作日志保留天数(0表示不按天清理)",
|
||||
},
|
||||
{
|
||||
Name: "operation_log_cleanup_limit",
|
||||
Value: "10000",
|
||||
Description: "操作日志保留条数(0表示不按数量清理)",
|
||||
},
|
||||
}...)
|
||||
|
||||
// ===== Cookie相关默认项 =====
|
||||
defaultSettings = append(defaultSettings, []models.Settings{
|
||||
{
|
||||
Name: "cookie_secure",
|
||||
Value: "true",
|
||||
Description: "Cookie Secure属性(是否只在HTTPS下发送)",
|
||||
},
|
||||
{
|
||||
Name: "cookie_same_site",
|
||||
Value: "Lax",
|
||||
Description: "Cookie SameSite属性(Strict/Lax/None)",
|
||||
},
|
||||
{
|
||||
Name: "cookie_domain",
|
||||
Value: "",
|
||||
Description: "Cookie域名",
|
||||
},
|
||||
{
|
||||
Name: "cookie_max_age",
|
||||
Value: "86400",
|
||||
Description: "Cookie最大存活时间(秒)",
|
||||
},
|
||||
}...)
|
||||
|
||||
// ===== 站点基本信息默认项 =====
|
||||
defaultSettings = append(defaultSettings, []models.Settings{
|
||||
{
|
||||
Name: "site_title",
|
||||
Value: "NetworkAuth",
|
||||
Description: "网站标题,显示在浏览器标题栏和页面顶部",
|
||||
},
|
||||
{
|
||||
Name: "site_keywords",
|
||||
Value: "NetworkAuth,网络授权服务,GoLang,Web服务",
|
||||
Description: "网站关键词,用于SEO优化,多个关键词用逗号分隔",
|
||||
},
|
||||
{
|
||||
Name: "site_description",
|
||||
Value: "网络授权服务 (NetworkAuth) 是一个专注于应用鉴权、接口管理和动态逻辑分发的后端系统",
|
||||
Description: "网站描述,用于SEO优化和社交媒体分享",
|
||||
},
|
||||
{
|
||||
Name: "site_logo",
|
||||
Value: "/logo.svg",
|
||||
Description: "网站Logo图片路径",
|
||||
},
|
||||
{
|
||||
Name: "contact_email",
|
||||
Value: "admin@example.com",
|
||||
Description: "联系邮箱,用于客服和业务咨询",
|
||||
},
|
||||
}...)
|
||||
|
||||
// ===== 页脚与备案相关默认项 =====
|
||||
defaultSettings = append(defaultSettings, []models.Settings{
|
||||
{
|
||||
Name: "footer_text",
|
||||
Value: "Copyright © 2026 NetworkAuth. All Rights Reserved.",
|
||||
Description: "页脚展示的版权或说明信息",
|
||||
},
|
||||
{
|
||||
Name: "icp_record",
|
||||
Value: "",
|
||||
Description: "ICP备案号,留空则不显示",
|
||||
},
|
||||
{
|
||||
Name: "icp_record_link",
|
||||
Value: "https://beian.miit.gov.cn",
|
||||
Description: "工信部ICP备案查询链接,留空则不显示",
|
||||
},
|
||||
{
|
||||
Name: "psb_record",
|
||||
Value: "",
|
||||
Description: "公安备案号,留空则不显示",
|
||||
},
|
||||
{
|
||||
Name: "psb_record_link",
|
||||
Value: "",
|
||||
Description: "公安备案查询链接,留空则不显示",
|
||||
},
|
||||
}...)
|
||||
|
||||
// ===== 前端平台配置相关默认项 =====
|
||||
defaultSettings = append(defaultSettings, []models.Settings{
|
||||
{
|
||||
Name: "platform_fixed_header",
|
||||
Value: "1",
|
||||
Description: "是否固定页头 (0 = 关闭,1 = 开启)",
|
||||
},
|
||||
{
|
||||
Name: "platform_hidden_side_bar",
|
||||
Value: "0",
|
||||
Description: "是否隐藏侧边栏 (0 = 关闭,1 = 开启)",
|
||||
},
|
||||
{
|
||||
Name: "platform_multi_tags_cache",
|
||||
Value: "0",
|
||||
Description: "是否开启多标签页缓存 (0 = 关闭,1 = 开启)",
|
||||
},
|
||||
{
|
||||
Name: "platform_keep_alive",
|
||||
Value: "1",
|
||||
Description: "是否开启组件缓存 (0 = 关闭,1 = 开启)",
|
||||
},
|
||||
{
|
||||
Name: "platform_layout",
|
||||
Value: "vertical",
|
||||
Description: "布局模式 (vertical/horizontal/mix/comprehensive)",
|
||||
},
|
||||
{
|
||||
Name: "platform_theme",
|
||||
Value: "light",
|
||||
Description: "主题配色 (light/dark)",
|
||||
},
|
||||
{
|
||||
Name: "platform_dark_mode",
|
||||
Value: "0",
|
||||
Description: "是否开启暗黑模式 (0 = 关闭,1 = 开启)",
|
||||
},
|
||||
{
|
||||
Name: "platform_overall_style",
|
||||
Value: "light",
|
||||
Description: "整体风格",
|
||||
},
|
||||
{
|
||||
Name: "platform_grey",
|
||||
Value: "0",
|
||||
Description: "是否开启灰色模式 (0 = 关闭,1 = 开启)",
|
||||
},
|
||||
{
|
||||
Name: "platform_weak",
|
||||
Value: "0",
|
||||
Description: "是否开启色弱模式 (0 = 关闭,1 = 开启)",
|
||||
},
|
||||
{
|
||||
Name: "platform_hide_tabs",
|
||||
Value: "0",
|
||||
Description: "是否隐藏标签页 (0 = 关闭,1 = 开启)",
|
||||
},
|
||||
{
|
||||
Name: "platform_hide_footer",
|
||||
Value: "0",
|
||||
Description: "是否隐藏页脚 (0 = 关闭,1 = 开启)",
|
||||
},
|
||||
{
|
||||
Name: "platform_stretch",
|
||||
Value: "0",
|
||||
Description: "是否开启页面宽度拉伸 (0 = 关闭,1 = 开启)",
|
||||
},
|
||||
{
|
||||
Name: "platform_sidebar_status",
|
||||
Value: "1",
|
||||
Description: "侧边栏状态 (0 = 折叠,1 = 展开)",
|
||||
},
|
||||
{
|
||||
Name: "platform_ep_theme_color",
|
||||
Value: "#409EFF",
|
||||
Description: "Element Plus 主题色",
|
||||
},
|
||||
{
|
||||
Name: "platform_show_logo",
|
||||
Value: "1",
|
||||
Description: "是否显示Logo (0 = 关闭,1 = 开启)",
|
||||
},
|
||||
{
|
||||
Name: "platform_show_model",
|
||||
Value: "smart",
|
||||
Description: "显示模式 (smart等)",
|
||||
},
|
||||
{
|
||||
Name: "platform_menu_arrow_icon_no_transition",
|
||||
Value: "0",
|
||||
Description: "菜单箭头图标是否取消过渡动画 (0 = 关闭,1 = 开启)",
|
||||
},
|
||||
{
|
||||
Name: "platform_caching_async_routes",
|
||||
Value: "0",
|
||||
Description: "是否缓存异步路由 (0 = 关闭,1 = 开启)",
|
||||
},
|
||||
{
|
||||
Name: "platform_tooltip_effect",
|
||||
Value: "light",
|
||||
Description: "提示框效果 (light/dark)",
|
||||
},
|
||||
{
|
||||
Name: "platform_responsive_storage_name_space",
|
||||
Value: "responsive-",
|
||||
Description: "响应式存储命名空间",
|
||||
},
|
||||
{
|
||||
Name: "platform_menu_search_history",
|
||||
Value: "6",
|
||||
Description: "菜单搜索历史最大记录数",
|
||||
},
|
||||
}...)
|
||||
|
||||
// 逐个检查并创建不存在的设置项
|
||||
for _, setting := range defaultSettings {
|
||||
var count int64
|
||||
if err := db.Model(&models.Settings{}).Where("name = ?", setting.Name).Count(&count).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if count == 0 {
|
||||
if err := db.Create(&setting).Error; err != nil {
|
||||
logrus.WithError(err).WithField("name", setting.Name).Error("创建系统设置失败")
|
||||
return err
|
||||
}
|
||||
logrus.WithField("name", setting.Name).WithField("value", setting.Value).Debug("创建系统设置项")
|
||||
}
|
||||
}
|
||||
|
||||
logrus.Info("系统设置初始化完成")
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user