add system config cache for AppServer object

This commit is contained in:
RockYang 2023-07-31 06:56:28 +08:00
parent a18188876c
commit 29094ba3b3
2 changed files with 22 additions and 13 deletions

View File

@ -24,7 +24,9 @@ type AppServer struct {
Config *types.AppConfig Config *types.AppConfig
Engine *gin.Engine Engine *gin.Engine
ChatContexts *types.LMap[string, []interface{}] // 聊天上下文 Map [chatId] => []Message ChatContexts *types.LMap[string, []interface{}] // 聊天上下文 Map [chatId] => []Message
ChatConfig *types.ChatConfig // 聊天配置
ChatConfig *types.ChatConfig // chat config cache
SysConfig *types.SystemConfig // system config cache
// 保存 Websocket 会话 UserId, 每个 UserId 只能连接一次 // 保存 Websocket 会话 UserId, 每个 UserId 只能连接一次
// 防止第三方直接连接 socket 调用 OpenAI API // 防止第三方直接连接 socket 调用 OpenAI API
@ -61,9 +63,8 @@ func (s *AppServer) Init(debug bool) {
if debug { // 调试模式允许跨域请求 API if debug { // 调试模式允许跨域请求 API
s.Debug = debug s.Debug = debug
logger.Info("Enabled debug mode") logger.Info("Enabled debug mode")
s.Engine.Use(corsMiddleware())
} }
s.Engine.Use(corsMiddleware())
s.Engine.Use(sessionMiddleware(s.Config)) s.Engine.Use(sessionMiddleware(s.Config))
s.Engine.Use(authorizeMiddleware(s)) s.Engine.Use(authorizeMiddleware(s))
s.Engine.Use(errorHandler) s.Engine.Use(errorHandler)
@ -82,6 +83,15 @@ func (s *AppServer) Run(db *gorm.DB) error {
if err != nil { if err != nil {
return err return err
} }
// load system configs
res = db.Where("marker", "system").First(&config)
if res.Error != nil {
return res.Error
}
err = utils.JsonDecode(config.Config, &s.SysConfig)
if err != nil {
return err
}
logger.Infof("http://%s", s.Config.Listen) logger.Infof("http://%s", s.Config.Listen)
return s.Engine.Run(s.Config.Listen) return s.Engine.Run(s.Config.Listen)
} }

View File

@ -15,13 +15,19 @@ type AppConfig struct {
StaticDir string // 静态资源目录 StaticDir string // 静态资源目录
StaticUrl string // 静态资源 URL StaticUrl string // 静态资源 URL
Redis RedisConfig // redis 连接信息 Redis RedisConfig // redis 连接信息
ApiConfig ChatPlusApiConfig // chatplus api configs ApiConfig ChatPlusApiConfig // ChatPlus API authorization configs
AesEncryptKey string AesEncryptKey string
SmsConfig AliYunSmsConfig // 短信发送配置 SmsConfig AliYunSmsConfig // AliYun send message service config
StartWechatBot bool // 是否启动微信机器人 StartWechatBot bool // 是否启动微信机器人
EnabledMsgService bool // 是否启用短信服务 EnabledMsgService bool // 是否启用短信服务
} }
type ChatPlusApiConfig struct {
ApiURL string
AppId string
Token string
}
type AliYunSmsConfig struct { type AliYunSmsConfig struct {
AccessKey string AccessKey string
AccessSecret string AccessSecret string
@ -76,7 +82,6 @@ type ChatConfig struct {
EnableHistory bool `json:"enable_history"` // 是否允许保存聊天记录 EnableHistory bool `json:"enable_history"` // 是否允许保存聊天记录
ApiKey string `json:"api_key"` ApiKey string `json:"api_key"`
ContextDeep int `json:"context_deep"` // 上下文深度 ContextDeep int `json:"context_deep"` // 上下文深度
} }
type SystemConfig struct { type SystemConfig struct {
@ -86,10 +91,4 @@ type SystemConfig struct {
UserInitCalls int `json:"user_init_calls"` // 新用户注册默认总送多少次调用 UserInitCalls int `json:"user_init_calls"` // 新用户注册默认总送多少次调用
} }
type ChatPlusApiConfig struct { const UserInitCalls = 20
ApiURL string
AppId string
Token string
}
const UserInitCalls = 1000