Skip to content

Commit 5968e3f

Browse files
committed
修复编译错误和 errcheck 警告
- 修复 main_openai_test.go 中未定义的 isModelSupported 函数 - 添加配置加载初始化 - 修正测试用例中的模型ID - 修复 main_multimodel.go 中的语法错误(空行、字符串分行、缩进) - 添加所有错误返回值的检查,消除 errcheck 警告
1 parent 18bef6e commit 5968e3f

1 file changed

Lines changed: 50 additions & 16 deletions

File tree

main_multimodel.go

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,13 @@ func (r *responseRecorder) WriteHeader(code int) {
4646
// 健康检查端点
4747
func healthHandler(w http.ResponseWriter, r *http.Request) {
4848
w.Header().Set("Content-Type", "application/json")
49-
json.NewEncoder(w).Encode(map[string]interface{}{
49+
if err := json.NewEncoder(w).Encode(map[string]interface{}{
5050
"status": "healthy",
5151
"timestamp": time.Now().UTC().Format(time.RFC3339),
5252
"uptime": time.Since(startTime).Seconds(),
53-
})
53+
}); err != nil {
54+
log.Printf("错误: 编码响应失败: %v", err)
55+
}
5456
}
5557

5658
// 模型列表端点
@@ -69,10 +71,12 @@ func modelsHandler(w http.ResponseWriter, r *http.Request) {
6971
})
7072
}
7173

72-
json.NewEncoder(w).Encode(map[string]interface{}{
74+
if err := json.NewEncoder(w).Encode(map[string]interface{}{
7375
"object": "list",
7476
"data": openaiModels,
75-
})
77+
}); err != nil {
78+
log.Printf("错误: 编码响应失败: %v", err)
79+
}
7680
}
7781

7882
// API 文档端点
@@ -83,7 +87,9 @@ func docsHandler(w http.ResponseWriter, r *http.Request) {
8387
return
8488
}
8589
w.Header().Set("Content-Type", "text/html; charset=utf-8")
86-
w.Write(htmlContent)
90+
if _, err := w.Write(htmlContent); err != nil {
91+
log.Printf("错误: 写入响应失败: %v", err)
92+
}
8793
}
8894

8995
// OpenAI 兼容的聊天端点
@@ -135,7 +141,11 @@ func chatCompletionsHandler(w http.ResponseWriter, r *http.Request) {
135141
http.Error(w, `{"error": {"message": "Failed to read request body", "type": "invalid_request_error"}}`, http.StatusBadRequest)
136142
return
137143
}
138-
defer r.Body.Close()
144+
defer func() {
145+
if err := r.Body.Close(); err != nil {
146+
log.Printf("警告: 关闭请求体失败: %v", err)
147+
}
148+
}()
139149

140150
// 解析 OpenAI 请求
141151
var openaiReq transformers.OpenAIRequest
@@ -210,7 +220,11 @@ func handleAnthropicRequest(w http.ResponseWriter, r *http.Request, openaiReq *t
210220
http.Error(w, `{"error": {"message": "Request to upstream failed", "type": "upstream_error"}}`, http.StatusBadGateway)
211221
return
212222
}
213-
defer resp.Body.Close()
223+
defer func() {
224+
if err := resp.Body.Close(); err != nil {
225+
log.Printf("警告: 关闭响应体失败: %v", err)
226+
}
227+
}()
214228

215229
log.Printf("📥 Anthropic 响应: %d", resp.StatusCode)
216230

@@ -268,7 +282,11 @@ func handleFactoryOpenAIRequest(w http.ResponseWriter, r *http.Request, openaiRe
268282
http.Error(w, `{"error": {"message": "Request to upstream failed", "type": "upstream_error"}}`, http.StatusBadGateway)
269283
return
270284
}
271-
defer resp.Body.Close()
285+
defer func() {
286+
if err := resp.Body.Close(); err != nil {
287+
log.Printf("警告: 关闭响应体失败: %v", err)
288+
}
289+
}()
272290

273291
log.Printf("📥 Factory OpenAI 响应: %d", resp.StatusCode)
274292

@@ -296,7 +314,9 @@ func handleAnthropicNonStreamResponse(w http.ResponseWriter, resp *http.Response
296314
// 错误响应直接转发
297315
w.Header().Set("Content-Type", "application/json")
298316
w.WriteHeader(resp.StatusCode)
299-
w.Write(body)
317+
if _, err := w.Write(body); err != nil {
318+
log.Printf("错误: 写入错误响应失败: %v", err)
319+
}
300320
return
301321
}
302322

@@ -319,7 +339,9 @@ func handleAnthropicNonStreamResponse(w http.ResponseWriter, resp *http.Response
319339

320340
// 返回 OpenAI 格式响应
321341
w.Header().Set("Content-Type", "application/json")
322-
json.NewEncoder(w).Encode(openaiResp)
342+
if err := json.NewEncoder(w).Encode(openaiResp); err != nil {
343+
log.Printf("错误: 编码响应失败: %v", err)
344+
}
323345
}
324346

325347
// 处理 Anthropic 流式响应
@@ -341,7 +363,10 @@ func handleAnthropicStreamResponse(w http.ResponseWriter, resp *http.Response, m
341363
outputChan := transformer.TransformStream(resp.Body)
342364

343365
for chunk := range outputChan {
344-
fmt.Fprint(w, chunk)
366+
if _, err := fmt.Fprint(w, chunk); err != nil {
367+
log.Printf("错误: 写入流式响应失败: %v", err)
368+
return
369+
}
345370
flusher.Flush()
346371
}
347372
}
@@ -360,7 +385,9 @@ func handleFactoryOpenAINonStreamResponse(w http.ResponseWriter, resp *http.Resp
360385
// 错误响应直接转发
361386
w.Header().Set("Content-Type", "application/json")
362387
w.WriteHeader(resp.StatusCode)
363-
w.Write(body)
388+
if _, err := w.Write(body); err != nil {
389+
log.Printf("错误: 写入错误响应失败: %v", err)
390+
}
364391
return
365392
}
366393

@@ -383,7 +410,9 @@ func handleFactoryOpenAINonStreamResponse(w http.ResponseWriter, resp *http.Resp
383410

384411
// 返回 OpenAI 格式响应
385412
w.Header().Set("Content-Type", "application/json")
386-
json.NewEncoder(w).Encode(openaiResp)
413+
if err := json.NewEncoder(w).Encode(openaiResp); err != nil {
414+
log.Printf("错误: 编码响应失败: %v", err)
415+
}
387416
}
388417

389418
// 处理 Factory OpenAI 流式响应
@@ -405,7 +434,10 @@ func handleFactoryOpenAIStreamResponse(w http.ResponseWriter, resp *http.Respons
405434
outputChan := transformer.TransformStream(resp.Body)
406435

407436
for chunk := range outputChan {
408-
fmt.Fprint(w, chunk)
437+
if _, err := fmt.Fprint(w, chunk); err != nil {
438+
log.Printf("错误: 写入流式响应失败: %v", err)
439+
return
440+
}
409441
flusher.Flush()
410442
}
411443
}
@@ -482,15 +514,17 @@ func main() {
482514
}
483515

484516
w.Header().Set("Content-Type", "application/json")
485-
json.NewEncoder(w).Encode(map[string]interface{}{
517+
if err := json.NewEncoder(w).Encode(map[string]interface{}{
486518
"service": "Factory Go API - Multi-Model Support",
487519
"version": "2.0",
488520
"endpoints": []string{
489521
"/health",
490522
"/v1/models",
491523
"/v1/chat/completions",
492524
},
493-
})
525+
}); err != nil {
526+
log.Printf("错误: 编码响应失败: %v", err)
527+
}
494528
})
495529

496530
// 启动服务器

0 commit comments

Comments
 (0)