系统配置重构,支持后台管理页面配置

This commit is contained in:
RockYang
2025-08-20 10:47:17 +08:00
parent 6242f648f1
commit 0956bef9db
19 changed files with 2107 additions and 138 deletions

View File

@@ -0,0 +1,275 @@
package service
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Copyright 2023 The Geek-AI Authors. All rights reserved.
// Use of this source code is governed by a Apache-2.0 license
// that can be found in the LICENSE file.
// @Author yangjian102621@163.com
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
import (
"context"
"encoding/json"
"geekai/core/types"
"geekai/store/model"
"github.com/go-redis/redis/v8"
"gorm.io/gorm"
)
const (
// 迁移状态Redis key
MigrationStatusKey = "config_migration:status"
// 迁移完成标志
MigrationCompleted = "completed"
)
// ConfigMigrationService 配置迁移服务
type ConfigMigrationService struct {
db *gorm.DB
redisClient *redis.Client
}
func NewConfigMigrationService(db *gorm.DB, redisClient *redis.Client) *ConfigMigrationService {
return &ConfigMigrationService{
db: db,
redisClient: redisClient,
}
}
// MigrateFromConfig 从 config.toml 迁移配置到数据库(仅首次启动时执行)
func (s *ConfigMigrationService) MigrateFromConfig(config *types.AppConfig) error {
// 检查是否已经迁移过
if s.isMigrationCompleted() {
logger.Info("配置迁移已完成,跳过迁移")
return nil
}
logger.Info("开始迁移配置到数据库...")
// 迁移支付配置
if err := s.migratePaymentConfig(config); err != nil {
logger.Errorf("迁移支付配置失败: %v", err)
return err
}
// 迁移存储配置
if err := s.migrateStorageConfig(config); err != nil {
logger.Errorf("迁移存储配置失败: %v", err)
return err
}
// 迁移通信配置
if err := s.migrateCommunicationConfig(config); err != nil {
logger.Errorf("迁移通信配置失败: %v", err)
return err
}
// 迁移API配置
if err := s.migrateApiConfig(config); err != nil {
logger.Errorf("迁移API配置失败: %v", err)
return err
}
// 标记迁移完成
if err := s.markMigrationCompleted(); err != nil {
logger.Errorf("标记迁移完成失败: %v", err)
return err
}
logger.Info("配置迁移完成")
return nil
}
// 检查是否已经迁移完成
func (s *ConfigMigrationService) isMigrationCompleted() bool {
ctx := context.Background()
status, err := s.redisClient.Get(ctx, MigrationStatusKey).Result()
if err != nil {
// Redis中没有找到标志说明未迁移过
return false
}
return status == MigrationCompleted
}
// 标记迁移完成
func (s *ConfigMigrationService) markMigrationCompleted() error {
ctx := context.Background()
// 设置迁移完成标志,永不过期
return s.redisClient.Set(ctx, MigrationStatusKey, MigrationCompleted, 0).Err()
}
// 迁移支付配置
func (s *ConfigMigrationService) migratePaymentConfig(config *types.AppConfig) error {
// 支付宝配置
alipayConfig := map[string]any{
"enabled": config.AlipayConfig.Enabled,
"sand_box": config.AlipayConfig.SandBox,
"app_id": config.AlipayConfig.AppId,
"private_key": config.AlipayConfig.PrivateKey,
"alipay_public_key": config.AlipayConfig.AlipayPublicKey,
"notify_url": config.AlipayConfig.NotifyURL,
"return_url": config.AlipayConfig.ReturnURL,
}
if err := s.saveConfig("alipay", alipayConfig); err != nil {
return err
}
// 微信支付配置
wechatConfig := map[string]any{
"enabled": config.WechatPayConfig.Enabled,
"app_id": config.WechatPayConfig.AppId,
"mch_id": config.WechatPayConfig.MchId,
"serial_no": config.WechatPayConfig.SerialNo,
"private_key": config.WechatPayConfig.PrivateKey,
"api_v3_key": config.WechatPayConfig.ApiV3Key,
"notify_url": config.WechatPayConfig.NotifyURL,
}
if err := s.saveConfig("wechat", wechatConfig); err != nil {
return err
}
// 虎皮椒配置
hupiConfig := map[string]any{
"enabled": config.HuPiPayConfig.Enabled,
"app_id": config.HuPiPayConfig.AppId,
"app_secret": config.HuPiPayConfig.AppSecret,
"api_url": config.HuPiPayConfig.ApiURL,
"notify_url": config.HuPiPayConfig.NotifyURL,
"return_url": config.HuPiPayConfig.ReturnURL,
}
if err := s.saveConfig("hupi", hupiConfig); err != nil {
return err
}
// GeekPay配置
geekpayConfig := map[string]any{
"enabled": config.GeekPayConfig.Enabled,
"app_id": config.GeekPayConfig.AppId,
"private_key": config.GeekPayConfig.PrivateKey,
"api_url": config.GeekPayConfig.ApiURL,
"notify_url": config.GeekPayConfig.NotifyURL,
"return_url": config.GeekPayConfig.ReturnURL,
"methods": config.GeekPayConfig.Methods,
}
if err := s.saveConfig("geekpay", geekpayConfig); err != nil {
return err
}
return nil
}
// 迁移存储配置
func (s *ConfigMigrationService) migrateStorageConfig(config *types.AppConfig) error {
ossConfig := map[string]any{
"active": config.OSS.Active,
"local": map[string]any{
"base_path": config.OSS.Local.BasePath,
"base_url": config.OSS.Local.BaseURL,
},
"minio": map[string]any{
"endpoint": config.OSS.Minio.Endpoint,
"access_key": config.OSS.Minio.AccessKey,
"access_secret": config.OSS.Minio.AccessSecret,
"bucket": config.OSS.Minio.Bucket,
"use_ssl": config.OSS.Minio.UseSSL,
"domain": config.OSS.Minio.Domain,
},
"qiniu": map[string]any{
"zone": config.OSS.QiNiu.Zone,
"access_key": config.OSS.QiNiu.AccessKey,
"access_secret": config.OSS.QiNiu.AccessSecret,
"bucket": config.OSS.QiNiu.Bucket,
"domain": config.OSS.QiNiu.Domain,
},
"aliyun": map[string]any{
"endpoint": config.OSS.AliYun.Endpoint,
"access_key": config.OSS.AliYun.AccessKey,
"access_secret": config.OSS.AliYun.AccessSecret,
"bucket": config.OSS.AliYun.Bucket,
"sub_dir": config.OSS.AliYun.SubDir,
"domain": config.OSS.AliYun.Domain,
},
}
return s.saveConfig("oss", ossConfig)
}
// 迁移通信配置
func (s *ConfigMigrationService) migrateCommunicationConfig(config *types.AppConfig) error {
// SMTP配置
smtpConfig := map[string]any{
"use_tls": config.SmtpConfig.UseTls,
"host": config.SmtpConfig.Host,
"port": config.SmtpConfig.Port,
"app_name": config.SmtpConfig.AppName,
"from": config.SmtpConfig.From,
"password": config.SmtpConfig.Password,
}
if err := s.saveConfig("smtp", smtpConfig); err != nil {
return err
}
// 短信配置
smsConfig := map[string]any{
"active": config.SMS.Active,
"ali": map[string]any{
"access_key": config.SMS.Ali.AccessKey,
"access_secret": config.SMS.Ali.AccessSecret,
"product": config.SMS.Ali.Product,
"domain": config.SMS.Ali.Domain,
"sign": config.SMS.Ali.Sign,
"code_temp_id": config.SMS.Ali.CodeTempId,
},
"bao": map[string]any{
"username": config.SMS.Bao.Username,
"password": config.SMS.Bao.Password,
"domain": config.SMS.Bao.Domain,
"sign": config.SMS.Bao.Sign,
"code_template": config.SMS.Bao.CodeTemplate,
},
}
return s.saveConfig("sms", smsConfig)
}
// 迁移API配置
func (s *ConfigMigrationService) migrateApiConfig(config *types.AppConfig) error {
apiConfig := map[string]any{
"api_url": config.ApiConfig.ApiURL,
"app_id": config.ApiConfig.AppId,
"token": config.ApiConfig.Token,
"jimeng_config": map[string]any{
"access_key": config.ApiConfig.JimengConfig.AccessKey,
"secret_key": config.ApiConfig.JimengConfig.SecretKey,
},
}
return s.saveConfig("api", apiConfig)
}
// 保存配置到数据库
func (s *ConfigMigrationService) saveConfig(key string, config any) error {
// 检查是否已存在
var existingConfig model.Config
if err := s.db.Where("name", key).First(&existingConfig).Error; err == nil {
// 配置已存在,跳过
logger.Infof("配置 %s 已存在,跳过迁移", key)
return nil
}
// 序列化配置
configJSON, err := json.Marshal(config)
if err != nil {
return err
}
// 保存到数据库
newConfig := model.Config{
Name: key,
Value: string(configJSON),
}
if err := s.db.Create(&newConfig).Error; err != nil {
return err
}
logger.Infof("成功迁移配置 %s", key)
return nil
}

View File

@@ -0,0 +1,146 @@
package service
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Copyright 2023 The Geek-AI Authors. All rights reserved.
// Use of this source code is governed by a Apache-2.0 license
// that can be found in the LICENSE file.
// @Author yangjian102621@163.com
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
import (
"context"
"encoding/json"
"fmt"
"geekai/store/model"
"sync"
"github.com/go-redis/redis/v8"
"gorm.io/gorm"
)
// ConfigService 统一的配置访问、缓存与通知服务
type ConfigService struct {
db *gorm.DB
rdb *redis.Client
mu sync.RWMutex
cache map[string]json.RawMessage
watchers map[string][]chan struct{}
}
func NewConfigService(db *gorm.DB, rdb *redis.Client) *ConfigService {
s := &ConfigService{
db: db,
rdb: rdb,
cache: make(map[string]json.RawMessage),
watchers: make(map[string][]chan struct{}),
}
go s.subscribe()
return s
}
// Get 以原始 JSON 获取配置(带本地缓存)
func (s *ConfigService) Get(key string) (json.RawMessage, error) {
s.mu.RLock()
if v, ok := s.cache[key]; ok {
s.mu.RUnlock()
return v, nil
}
s.mu.RUnlock()
var cfg model.Config
if err := s.db.Where("name", key).First(&cfg).Error; err != nil {
return nil, err
}
s.mu.Lock()
s.cache[key] = json.RawMessage(cfg.Value)
s.mu.Unlock()
return json.RawMessage(cfg.Value), nil
}
// GetInto 将配置解析进传入结构体
func (s *ConfigService) GetInto(key string, dest interface{}) error {
data, err := s.Get(key)
if err != nil {
return err
}
if len(data) == 0 {
return nil
}
return json.Unmarshal(data, dest)
}
// Set 设置配置并写入数据库,同时触发通知
func (s *ConfigService) Set(key string, config json.RawMessage) error {
value := string(config)
cfg := model.Config{Name: key, Value: value}
if err := s.db.Where("name", key).FirstOrCreate(&cfg, model.Config{Name: key}).Error; err != nil {
return err
}
if cfg.Id > 0 {
cfg.Value = value
if err := s.db.Updates(&cfg).Error; err != nil {
return err
}
}
s.mu.Lock()
s.cache[key] = json.RawMessage(value)
s.mu.Unlock()
s.notifyLocal(key)
s.publish(key)
return nil
}
// Watch 返回一个通道,当指定 key 发生变化时收到事件
func (s *ConfigService) Watch(key string) <-chan struct{} {
ch := make(chan struct{}, 1)
s.mu.Lock()
s.watchers[key] = append(s.watchers[key], ch)
s.mu.Unlock()
return ch
}
func (s *ConfigService) notifyLocal(key string) {
s.mu.RLock()
list := s.watchers[key]
s.mu.RUnlock()
for _, ch := range list {
select { // 非阻塞通知
case ch <- struct{}{}:
default:
}
}
}
// 通过 Redis 发布配置变更,便于多实例同步
func (s *ConfigService) publish(key string) {
if s.rdb == nil {
return
}
channel := "config:changed"
if err := s.rdb.Publish(context.Background(), channel, key).Err(); err != nil {
logger.Warnf("publish config change failed: %v", err)
}
}
func (s *ConfigService) subscribe() {
if s.rdb == nil {
return
}
channel := "config:changed"
sub := s.rdb.Subscribe(context.Background(), channel)
for msg := range sub.Channel() {
key := msg.Payload
logger.Infof("config changed: %s", key)
// 失效本地缓存并本地广播
s.mu.Lock()
delete(s.cache, key)
s.mu.Unlock()
s.notifyLocal(key)
}
}
// Test 预留统一测试入口,根据 key 执行连通性检查
func (s *ConfigService) Test(key string) (string, error) {
// TODO: 实现各配置类型的测试逻辑
return fmt.Sprintf("%s ok", key), nil
}

View File

@@ -1,5 +1,7 @@
package service
import logger2 "geekai/logger"
const FailTaskProgress = 101
const (
TaskStatusRunning = "RUNNING"
@@ -15,6 +17,8 @@ type NotifyMessage struct {
Type string `json:"type"`
}
var logger = logger2.GetLogger()
const TranslatePromptTemplate = "Translate the following painting prompt words into English keyword phrases. Without any explanation, directly output the keyword phrases separated by commas. The content to be translated is: [%s]"
const ImagePromptOptimizeTemplate = `

View File

@@ -1,64 +0,0 @@
package service
// * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// * Copyright 2023 The Geek-AI Authors. All rights reserved.
// * Use of this source code is governed by a Apache-2.0 license
// * that can be found in the LICENSE file.
// * @Author yangjian102621@163.com
// * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
import (
"context"
"geekai/core/types"
logger2 "geekai/logger"
"github.com/xxl-job/xxl-job-executor-go"
"gorm.io/gorm"
)
var logger = logger2.GetLogger()
type XXLJobExecutor struct {
executor xxl.Executor
db *gorm.DB
}
func NewXXLJobExecutor(config *types.AppConfig, db *gorm.DB) *XXLJobExecutor {
if !config.XXLConfig.Enabled {
logger.Info("XXL-JOB service is disabled")
return nil
}
exec := xxl.NewExecutor(
xxl.ServerAddr(config.XXLConfig.ServerAddr),
xxl.AccessToken(config.XXLConfig.AccessToken), //请求令牌(默认为空)
xxl.ExecutorIp(config.XXLConfig.ExecutorIp), //可自动获取
xxl.ExecutorPort(config.XXLConfig.ExecutorPort), //默认9999非必填
xxl.RegistryKey(config.XXLConfig.RegistryKey), //执行器名称
xxl.SetLogger(&customLogger{}), //自定义日志
)
exec.Init()
return &XXLJobExecutor{executor: exec, db: db}
}
func (e *XXLJobExecutor) Run() error {
e.executor.RegTask("ClearOrders", e.ClearOrders)
return e.executor.Run()
}
// ClearOrders 清理未支付的订单,如果没有抛出异常则表示执行成功
func (e *XXLJobExecutor) ClearOrders(cxt context.Context, param *xxl.RunReq) (msg string) {
logger.Info("执行清理未支付订单...")
return "success"
}
type customLogger struct{}
func (l *customLogger) Info(format string, a ...interface{}) {
logger.Debugf(format, a...)
}
func (l *customLogger) Error(format string, a ...interface{}) {
logger.Errorf(format, a...)
}