mirror of
https://github.com/skyle1995/NetworkAuth.git
synced 2026-05-25 02:24:05 +08:00
更新 安装支持设置Redis
修复 页脚没有加载设置值
This commit is contained in:
@@ -244,7 +244,19 @@ func SettingsPublicHandler(c *gin.Context) {
|
|||||||
|
|
||||||
var list []models.Settings
|
var list []models.Settings
|
||||||
// 查询公开的基本信息、维护模式和所有前端平台配置
|
// 查询公开的基本信息、维护模式和所有前端平台配置
|
||||||
if err := db.Where("name IN ? OR name LIKE ?", []string{"site_title", "site_description", "site_keywords", "site_logo", "contact_email", "maintenance_mode"}, "platform_%").Find(&list).Error; err != nil {
|
if err := db.Where("name IN ? OR name LIKE ?", []string{
|
||||||
|
"site_title",
|
||||||
|
"site_description",
|
||||||
|
"site_keywords",
|
||||||
|
"site_logo",
|
||||||
|
"contact_email",
|
||||||
|
"maintenance_mode",
|
||||||
|
"footer_text",
|
||||||
|
"icp_record",
|
||||||
|
"icp_record_link",
|
||||||
|
"psb_record",
|
||||||
|
"psb_record_link",
|
||||||
|
}, "platform_%").Find(&list).Error; err != nil {
|
||||||
authBaseController.HandleInternalError(c, "查询失败", err)
|
authBaseController.HandleInternalError(c, "查询失败", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,6 +34,13 @@ func InstallSubmitHandler(c *gin.Context) {
|
|||||||
DbUser string `json:"db_user"`
|
DbUser string `json:"db_user"`
|
||||||
DbPass string `json:"db_pass"`
|
DbPass string `json:"db_pass"`
|
||||||
|
|
||||||
|
// Redis配置(可选)
|
||||||
|
RedisEnabled bool `json:"redis_enabled"`
|
||||||
|
RedisHost string `json:"redis_host"`
|
||||||
|
RedisPort int `json:"redis_port"`
|
||||||
|
RedisPassword string `json:"redis_password"`
|
||||||
|
RedisDB int `json:"redis_db"`
|
||||||
|
|
||||||
// 站点和管理员配置
|
// 站点和管理员配置
|
||||||
SiteTitle string `json:"site_title" binding:"required"`
|
SiteTitle string `json:"site_title" binding:"required"`
|
||||||
AdminUsername string `json:"admin_username" binding:"required"`
|
AdminUsername string `json:"admin_username" binding:"required"`
|
||||||
@@ -45,6 +52,22 @@ func InstallSubmitHandler(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 校验 Redis 配置(启用时)
|
||||||
|
if req.RedisEnabled {
|
||||||
|
if strings.TrimSpace(req.RedisHost) == "" {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"code": 1, "msg": "启用 Redis 时主机地址不能为空"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if req.RedisPort < 1 || req.RedisPort > 65535 {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"code": 1, "msg": "Redis 端口号无效"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if req.RedisDB < 0 || req.RedisDB > 15 {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"code": 1, "msg": "Redis 数据库索引必须在 0-15 之间"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 1. 更新配置文件
|
// 1. 更新配置文件
|
||||||
err := config.UpdateConfig(func(cfg *config.AppConfig) {
|
err := config.UpdateConfig(func(cfg *config.AppConfig) {
|
||||||
cfg.Database.Type = req.DbType
|
cfg.Database.Type = req.DbType
|
||||||
@@ -55,6 +78,20 @@ func InstallSubmitHandler(c *gin.Context) {
|
|||||||
cfg.Database.MySQL.Username = req.DbUser
|
cfg.Database.MySQL.Username = req.DbUser
|
||||||
cfg.Database.MySQL.Password = req.DbPass
|
cfg.Database.MySQL.Password = req.DbPass
|
||||||
}
|
}
|
||||||
|
// 写入 Redis 配置
|
||||||
|
if req.RedisEnabled {
|
||||||
|
cfg.Redis.Host = strings.TrimSpace(req.RedisHost)
|
||||||
|
cfg.Redis.Port = req.RedisPort
|
||||||
|
cfg.Redis.Password = req.RedisPassword
|
||||||
|
cfg.Redis.DB = req.RedisDB
|
||||||
|
} else {
|
||||||
|
if cfg.Redis.Host == "" {
|
||||||
|
cfg.Redis.Host = "localhost"
|
||||||
|
}
|
||||||
|
if cfg.Redis.Port == 0 {
|
||||||
|
cfg.Redis.Port = 6379
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"code": 1, "msg": "更新配置文件失败: " + err.Error()})
|
c.JSON(http.StatusInternalServerError, gin.H{"code": 1, "msg": "更新配置文件失败: " + err.Error()})
|
||||||
@@ -180,6 +217,14 @@ func InstallSubmitHandler(c *gin.Context) {
|
|||||||
logrus.WithError(err).Error("安装完成后加密管理器初始化失败")
|
logrus.WithError(err).Error("安装完成后加密管理器初始化失败")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 根据用户提供的 Redis 配置重新初始化 Redis 连接
|
||||||
|
if req.RedisEnabled {
|
||||||
|
utils.ReInitRedis()
|
||||||
|
if !utils.IsRedisAvailable() {
|
||||||
|
logrus.Warn("安装完成后 Redis 连接失败,请检查配置;系统将以无 Redis 模式运行")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 启动日志清理定时任务
|
// 启动日志清理定时任务
|
||||||
services.StartLogCleanupTask()
|
services.StartLogCleanupTask()
|
||||||
|
|
||||||
|
|||||||
@@ -162,6 +162,21 @@ func InitRedis() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReInitRedis 强制重新初始化Redis客户端
|
||||||
|
// - 用于安装/配置变更后无视 sync.Once 重新建立连接
|
||||||
|
// - 旧连接会被关闭
|
||||||
|
func ReInitRedis() {
|
||||||
|
// 关闭旧连接
|
||||||
|
if redisClient != nil {
|
||||||
|
_ = redisClient.Close()
|
||||||
|
redisClient = nil
|
||||||
|
}
|
||||||
|
redisAvailable = false
|
||||||
|
// 重置 sync.Once,使下次 GetRedis/InitRedis 重新执行
|
||||||
|
redisOnce = sync.Once{}
|
||||||
|
InitRedis()
|
||||||
|
}
|
||||||
|
|
||||||
// GetRedis 获取全局Redis客户端,可能返回nil(当不可用时)
|
// GetRedis 获取全局Redis客户端,可能返回nil(当不可用时)
|
||||||
func GetRedis() *redis.Client {
|
func GetRedis() *redis.Client {
|
||||||
if redisClient == nil {
|
if redisClient == nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user