config.go
1 package handler 2 3 import ( 4 "krillin-ai/config" 5 "krillin-ai/internal/response" 6 "krillin-ai/log" 7 8 "github.com/gin-gonic/gin" 9 "go.uber.org/zap" 10 ) 11 12 // 全局变量,用于标记配置是否需要重新初始化 13 var configUpdated bool 14 15 // ConfigRequest 定义前端发送的配置数据结构 16 type ConfigRequest struct { 17 App struct { 18 SegmentDuration int `json:"segmentDuration"` 19 TranscribeParallelNum int `json:"transcribeParallelNum"` 20 TranslateParallelNum int `json:"translateParallelNum"` 21 TranscribeMaxAttempts int `json:"transcribeMaxAttempts"` 22 TranslateMaxAttempts int `json:"translateMaxAttempts"` 23 MaxSentenceLength int `json:"maxSentenceLength"` 24 Proxy string `json:"proxy"` 25 } `json:"app"` 26 Server struct { 27 Host string `json:"host"` 28 Port int `json:"port"` 29 } `json:"server"` 30 Llm struct { 31 BaseUrl string `json:"baseUrl"` 32 ApiKey string `json:"apiKey"` 33 Model string `json:"model"` 34 } `json:"llm"` 35 Transcribe struct { 36 Provider string `json:"provider"` 37 EnableGpuAcceleration bool `json:"enableGpuAcceleration"` 38 Openai struct { 39 BaseUrl string `json:"baseUrl"` 40 ApiKey string `json:"apiKey"` 41 Model string `json:"model"` 42 } `json:"openai"` 43 Fasterwhisper struct { 44 Model string `json:"model"` 45 } `json:"fasterwhisper"` 46 Whisperkit struct { 47 Model string `json:"model"` 48 } `json:"whisperkit"` 49 Whispercpp struct { 50 Model string `json:"model"` 51 } `json:"whispercpp"` 52 Aliyun struct { 53 Oss struct { 54 AccessKeyId string `json:"accessKeyId"` 55 AccessKeySecret string `json:"accessKeySecret"` 56 Bucket string `json:"bucket"` 57 } `json:"oss"` 58 Speech struct { 59 AccessKeyId string `json:"accessKeyId"` 60 AccessKeySecret string `json:"accessKeySecret"` 61 AppKey string `json:"appKey"` 62 } `json:"speech"` 63 } `json:"aliyun"` 64 } `json:"transcribe"` 65 Tts struct { 66 Provider string `json:"provider"` 67 Openai struct { 68 BaseUrl string `json:"baseUrl"` 69 ApiKey string `json:"apiKey"` 70 Model string `json:"model"` 71 } `json:"openai"` 72 Aliyun struct { 73 Oss struct { 74 AccessKeyId string `json:"accessKeyId"` 75 AccessKeySecret string `json:"accessKeySecret"` 76 Bucket string `json:"bucket"` 77 } `json:"oss"` 78 Speech struct { 79 AccessKeyId string `json:"accessKeyId"` 80 AccessKeySecret string `json:"accessKeySecret"` 81 AppKey string `json:"appKey"` 82 } `json:"speech"` 83 } `json:"aliyun"` 84 } `json:"tts"` 85 } 86 87 // GetConfig 获取当前配置 88 func (h Handler) GetConfig(c *gin.Context) { 89 log.GetLogger().Info("获取配置信息") 90 91 // 转换配置为前端需要的格式 92 configResponse := ConfigRequest{ 93 App: struct { 94 SegmentDuration int `json:"segmentDuration"` 95 TranscribeParallelNum int `json:"transcribeParallelNum"` 96 TranslateParallelNum int `json:"translateParallelNum"` 97 TranscribeMaxAttempts int `json:"transcribeMaxAttempts"` 98 TranslateMaxAttempts int `json:"translateMaxAttempts"` 99 MaxSentenceLength int `json:"maxSentenceLength"` 100 Proxy string `json:"proxy"` 101 }{ 102 SegmentDuration: config.Conf.App.SegmentDuration, 103 TranscribeParallelNum: config.Conf.App.TranscribeParallelNum, 104 TranslateParallelNum: config.Conf.App.TranslateParallelNum, 105 TranscribeMaxAttempts: config.Conf.App.TranscribeMaxAttempts, 106 TranslateMaxAttempts: config.Conf.App.TranslateMaxAttempts, 107 MaxSentenceLength: config.Conf.App.MaxSentenceLength, 108 Proxy: config.Conf.App.Proxy, 109 }, 110 Server: struct { 111 Host string `json:"host"` 112 Port int `json:"port"` 113 }{ 114 Host: config.Conf.Server.Host, 115 Port: config.Conf.Server.Port, 116 }, 117 Llm: struct { 118 BaseUrl string `json:"baseUrl"` 119 ApiKey string `json:"apiKey"` 120 Model string `json:"model"` 121 }{ 122 BaseUrl: config.Conf.Llm.BaseUrl, 123 ApiKey: config.Conf.Llm.ApiKey, 124 Model: config.Conf.Llm.Model, 125 }, 126 } 127 128 // 转录配置 129 configResponse.Transcribe.Provider = config.Conf.Transcribe.Provider 130 configResponse.Transcribe.EnableGpuAcceleration = config.Conf.Transcribe.EnableGpuAcceleration 131 configResponse.Transcribe.Openai.BaseUrl = config.Conf.Transcribe.Openai.BaseUrl 132 configResponse.Transcribe.Openai.ApiKey = config.Conf.Transcribe.Openai.ApiKey 133 configResponse.Transcribe.Openai.Model = config.Conf.Transcribe.Openai.Model 134 configResponse.Transcribe.Fasterwhisper.Model = config.Conf.Transcribe.Fasterwhisper.Model 135 configResponse.Transcribe.Whisperkit.Model = config.Conf.Transcribe.Whisperkit.Model 136 configResponse.Transcribe.Whispercpp.Model = config.Conf.Transcribe.Whispercpp.Model 137 configResponse.Transcribe.Aliyun.Oss.AccessKeyId = config.Conf.Transcribe.Aliyun.Oss.AccessKeyId 138 configResponse.Transcribe.Aliyun.Oss.AccessKeySecret = config.Conf.Transcribe.Aliyun.Oss.AccessKeySecret 139 configResponse.Transcribe.Aliyun.Oss.Bucket = config.Conf.Transcribe.Aliyun.Oss.Bucket 140 configResponse.Transcribe.Aliyun.Speech.AccessKeyId = config.Conf.Transcribe.Aliyun.Speech.AccessKeyId 141 configResponse.Transcribe.Aliyun.Speech.AccessKeySecret = config.Conf.Transcribe.Aliyun.Speech.AccessKeySecret 142 configResponse.Transcribe.Aliyun.Speech.AppKey = config.Conf.Transcribe.Aliyun.Speech.AppKey 143 144 // TTS配置 145 configResponse.Tts.Provider = config.Conf.Tts.Provider 146 configResponse.Tts.Openai.BaseUrl = config.Conf.Tts.Openai.BaseUrl 147 configResponse.Tts.Openai.ApiKey = config.Conf.Tts.Openai.ApiKey 148 configResponse.Tts.Openai.Model = config.Conf.Tts.Openai.Model 149 configResponse.Tts.Aliyun.Oss.AccessKeyId = config.Conf.Tts.Aliyun.Oss.AccessKeyId 150 configResponse.Tts.Aliyun.Oss.AccessKeySecret = config.Conf.Tts.Aliyun.Oss.AccessKeySecret 151 configResponse.Tts.Aliyun.Oss.Bucket = config.Conf.Tts.Aliyun.Oss.Bucket 152 configResponse.Tts.Aliyun.Speech.AccessKeyId = config.Conf.Tts.Aliyun.Speech.AccessKeyId 153 configResponse.Tts.Aliyun.Speech.AccessKeySecret = config.Conf.Tts.Aliyun.Speech.AccessKeySecret 154 configResponse.Tts.Aliyun.Speech.AppKey = config.Conf.Tts.Aliyun.Speech.AppKey 155 156 response.R(c, response.Response{ 157 Error: 0, 158 Msg: "获取配置成功", 159 Data: configResponse, 160 }) 161 } 162 163 // UpdateConfig 更新配置 164 func (h Handler) UpdateConfig(c *gin.Context) { 165 var req ConfigRequest 166 if err := c.ShouldBindJSON(&req); err != nil { 167 log.GetLogger().Error("UpdateConfig ShouldBindJSON err", zap.Error(err)) 168 response.R(c, response.Response{ 169 Error: -1, 170 Msg: "参数错误: " + err.Error(), 171 Data: nil, 172 }) 173 return 174 } 175 176 log.GetLogger().Info("更新配置信息") 177 178 // 更新配置备份,确保桌面应用能检测到配置变化 179 config.ConfigBackup = config.Conf 180 181 // 标记配置已更新,需要重新初始化服务 182 configUpdated = true 183 184 // 更新应用配置 185 config.Conf.App.SegmentDuration = req.App.SegmentDuration 186 config.Conf.App.TranscribeParallelNum = req.App.TranscribeParallelNum 187 config.Conf.App.TranslateParallelNum = req.App.TranslateParallelNum 188 config.Conf.App.TranscribeMaxAttempts = req.App.TranscribeMaxAttempts 189 config.Conf.App.TranslateMaxAttempts = req.App.TranslateMaxAttempts 190 config.Conf.App.MaxSentenceLength = req.App.MaxSentenceLength 191 config.Conf.App.Proxy = req.App.Proxy 192 193 // 更新服务器配置 194 config.Conf.Server.Host = req.Server.Host 195 config.Conf.Server.Port = req.Server.Port 196 197 // 更新LLM配置 198 config.Conf.Llm.BaseUrl = req.Llm.BaseUrl 199 config.Conf.Llm.ApiKey = req.Llm.ApiKey 200 config.Conf.Llm.Model = req.Llm.Model 201 202 // 更新转录配置 203 config.Conf.Transcribe.Provider = req.Transcribe.Provider 204 config.Conf.Transcribe.EnableGpuAcceleration = req.Transcribe.EnableGpuAcceleration 205 config.Conf.Transcribe.Openai.BaseUrl = req.Transcribe.Openai.BaseUrl 206 config.Conf.Transcribe.Openai.ApiKey = req.Transcribe.Openai.ApiKey 207 config.Conf.Transcribe.Openai.Model = req.Transcribe.Openai.Model 208 config.Conf.Transcribe.Fasterwhisper.Model = req.Transcribe.Fasterwhisper.Model 209 config.Conf.Transcribe.Whisperkit.Model = req.Transcribe.Whisperkit.Model 210 config.Conf.Transcribe.Whispercpp.Model = req.Transcribe.Whispercpp.Model 211 config.Conf.Transcribe.Aliyun.Oss.AccessKeyId = req.Transcribe.Aliyun.Oss.AccessKeyId 212 config.Conf.Transcribe.Aliyun.Oss.AccessKeySecret = req.Transcribe.Aliyun.Oss.AccessKeySecret 213 config.Conf.Transcribe.Aliyun.Oss.Bucket = req.Transcribe.Aliyun.Oss.Bucket 214 config.Conf.Transcribe.Aliyun.Speech.AccessKeyId = req.Transcribe.Aliyun.Speech.AccessKeyId 215 config.Conf.Transcribe.Aliyun.Speech.AccessKeySecret = req.Transcribe.Aliyun.Speech.AccessKeySecret 216 config.Conf.Transcribe.Aliyun.Speech.AppKey = req.Transcribe.Aliyun.Speech.AppKey 217 218 // 更新TTS配置 219 config.Conf.Tts.Provider = req.Tts.Provider 220 config.Conf.Tts.Openai.BaseUrl = req.Tts.Openai.BaseUrl 221 config.Conf.Tts.Openai.ApiKey = req.Tts.Openai.ApiKey 222 config.Conf.Tts.Openai.Model = req.Tts.Openai.Model 223 config.Conf.Tts.Aliyun.Oss.AccessKeyId = req.Tts.Aliyun.Oss.AccessKeyId 224 config.Conf.Tts.Aliyun.Oss.AccessKeySecret = req.Tts.Aliyun.Oss.AccessKeySecret 225 config.Conf.Tts.Aliyun.Oss.Bucket = req.Tts.Aliyun.Oss.Bucket 226 config.Conf.Tts.Aliyun.Speech.AccessKeyId = req.Tts.Aliyun.Speech.AccessKeyId 227 config.Conf.Tts.Aliyun.Speech.AccessKeySecret = req.Tts.Aliyun.Speech.AccessKeySecret 228 config.Conf.Tts.Aliyun.Speech.AppKey = req.Tts.Aliyun.Speech.AppKey 229 230 // 验证配置 231 if err := config.CheckConfig(); err != nil { 232 log.GetLogger().Error("配置验证失败", zap.Error(err)) 233 // 恢复原配置 234 config.Conf = config.ConfigBackup 235 response.R(c, response.Response{ 236 Error: -1, 237 Msg: "配置验证失败: " + err.Error(), 238 Data: nil, 239 }) 240 return 241 } 242 243 // 保存配置到文件 244 if err := config.SaveConfig(); err != nil { 245 log.GetLogger().Error("保存配置失败", zap.Error(err)) 246 response.R(c, response.Response{ 247 Error: -1, 248 Msg: "保存配置失败: " + err.Error(), 249 Data: nil, 250 }) 251 return 252 } 253 254 log.GetLogger().Info("配置更新成功") 255 response.R(c, response.Response{ 256 Error: 0, 257 Msg: "配置更新成功", 258 Data: nil, 259 }) 260 }