-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
63 lines (56 loc) · 1.51 KB
/
main.go
File metadata and controls
63 lines (56 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"dify-external-datasets/ragflow"
"encoding/json"
"log"
"net/http"
"github.com/joho/godotenv"
)
// ErrorResponse 定义错误响应的结构
type ErrorResponse struct {
ErrorCode int `json:"error_code"`
ErrorMsg string `json:"error_msg"`
}
// ErrorHandler 统一错误处理中间件
func errorHandler(handler func(http.ResponseWriter, *http.Request) error) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if err := handler(w, r); err != nil {
if customErr, ok := err.(*ragflow.CustomError); ok {
statusCode := http.StatusInternalServerError
switch customErr.Code {
case 400:
statusCode = http.StatusBadRequest
case 1001, 1002:
statusCode = http.StatusForbidden
case 2001:
statusCode = http.StatusNotFound
}
w.WriteHeader(statusCode)
json.NewEncoder(w).Encode(ErrorResponse{
ErrorCode: customErr.Code,
ErrorMsg: customErr.Message,
})
return
}
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(ErrorResponse{
ErrorCode: 500,
ErrorMsg: "内部服务器错误",
})
}
}
}
func main() {
// 加载.env文件
if err := godotenv.Load(); err != nil {
log.Println(".env file not found")
}
// 注册路由
http.HandleFunc("/retrieval", errorHandler(ragflow.HandleRetrieval))
// 启动服务器
log.Println("Starting server on :8080")
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal(err)
}
}