feat: add log settings page
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/engigu/baihu-panel/internal/cache"
|
||||
"github.com/engigu/baihu-panel/internal/constant"
|
||||
"github.com/engigu/baihu-panel/internal/database"
|
||||
@@ -32,21 +35,58 @@ func (s *SettingsService) InitSettings() error {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 初始化日志清理配置
|
||||
var logRetentionCount int64
|
||||
database.DB.Model(&models.Setting{}).Where("section = ? AND `key` = ?", constant.SectionSystem, constant.KeyLogRetention).Count(&logRetentionCount)
|
||||
if logRetentionCount == 0 {
|
||||
if err := database.DB.Create(&models.Setting{
|
||||
ID: utils.GenerateID(),
|
||||
Section: constant.SectionSystem,
|
||||
Key: constant.KeyLogRetention,
|
||||
Value: models.BigText(constant.DefaultLogRetention),
|
||||
}).Error; err != nil {
|
||||
return err
|
||||
// 检查是否需要从旧的 JSON 迁移
|
||||
oldVal := s.Get(constant.SectionSystem, "log_retention")
|
||||
if oldVal != "" {
|
||||
var oldConfigs map[string]struct {
|
||||
Days int `json:"days"`
|
||||
MaxCount int `json:"max_count"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(oldVal), &oldConfigs); err == nil {
|
||||
migrationMap := map[string]string{}
|
||||
if cfg, ok := oldConfigs[constant.LogCategorySystemNotice]; ok {
|
||||
migrationMap[constant.KeySystemNoticeDays] = fmt.Sprintf("%d", cfg.Days)
|
||||
migrationMap[constant.KeySystemNoticeMaxCount] = fmt.Sprintf("%d", cfg.MaxCount)
|
||||
}
|
||||
if cfg, ok := oldConfigs[constant.LogCategoryPushLog]; ok {
|
||||
migrationMap[constant.KeyPushLogDays] = fmt.Sprintf("%d", cfg.Days)
|
||||
migrationMap[constant.KeyPushLogMaxCount] = fmt.Sprintf("%d", cfg.MaxCount)
|
||||
}
|
||||
if cfg, ok := oldConfigs[constant.LogCategoryLoginLog]; ok {
|
||||
migrationMap[constant.KeyLoginLogDays] = fmt.Sprintf("%d", cfg.Days)
|
||||
migrationMap[constant.KeyLoginLogMaxCount] = fmt.Sprintf("%d", cfg.MaxCount)
|
||||
}
|
||||
|
||||
if len(migrationMap) > 0 {
|
||||
for k, v := range migrationMap {
|
||||
s.Set(constant.SectionSystem, k, v)
|
||||
}
|
||||
// 迁移完成后删除旧键
|
||||
s.Delete(constant.SectionSystem, "log_retention")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 默认值初始化
|
||||
defaultRetention := map[string]string{
|
||||
constant.KeySystemNoticeDays: "30",
|
||||
constant.KeySystemNoticeMaxCount: "500",
|
||||
constant.KeyPushLogDays: "15",
|
||||
constant.KeyPushLogMaxCount: "5000",
|
||||
constant.KeyLoginLogDays: "30",
|
||||
constant.KeyLoginLogMaxCount: "1000",
|
||||
}
|
||||
|
||||
for k, v := range defaultRetention {
|
||||
var count int64
|
||||
database.DB.Model(&models.Setting{}).Where("section = ? AND `key` = ?", constant.SectionSystem, k).Count(&count)
|
||||
if count == 0 {
|
||||
s.Set(constant.SectionSystem, k, v)
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化或获取 JWT Secret 密码
|
||||
var secCount int64
|
||||
database.DB.Model(&models.Setting{}).Where("section = ? AND `key` = ?", constant.SectionSecurity, constant.KeySecret).Count(&secCount)
|
||||
|
||||
Reference in New Issue
Block a user