-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
95 lines (83 loc) · 2.43 KB
/
api.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package ct
import (
"context"
"encoding/json"
"net/http"
"github.com/advancevillage/3rd/logx"
"github.com/advancevillage/3rd/netx"
"github.com/advancevillage/ct/proto"
)
var (
HttpRequestBodyCode = uint32(1000)
JsonFromatCode = uint32(1100)
NotSupportCode = uint32(1101)
UpdateCode = uint32(1200)
QueryCode = uint32(1201)
HttpRequestBodyErr = "read request body error"
JsonFormatErr = "json format error"
NotSupportMsg = "not support action error"
UpdateMsg = "update forward error"
QueryMsg = "query forward error"
SrvOk = uint32(http.StatusOK)
SrvErr = uint32(http.StatusInternalServerError)
)
type queryRequest struct {
proto.ActionRequest
}
type queryResponse struct {
proto.ActionResponse
Conn []string
}
func (s *Srv) httpHandler(ctx context.Context, wr netx.IHTTPWriteReader) {
//1. 解析参数
var reply = &proto.ActionResponse{
Code: SrvErr,
}
var b, err = wr.Read()
if err != nil {
reply.Errors = append(reply.Errors, &proto.Error{Code: HttpRequestBodyCode, Msg: HttpRequestBodyErr})
wr.Write(http.StatusOK, reply)
return
}
var req = &proto.ActionRequest{}
err = json.Unmarshal(b, req)
if err != nil {
reply.Errors = append(reply.Errors, &proto.Error{Code: JsonFromatCode, Msg: JsonFormatErr})
wr.Write(http.StatusOK, reply)
return
}
//2. 提取TraceId
var sctx = context.WithValue(ctx, logx.TraceId, req.GetTraceId())
reply.TraceId = req.GetTraceId()
//3. 请求处理
switch req.GetAction() {
case "QueryForward":
var (
request = &queryRequest{}
response = &queryResponse{}
)
response.TraceId = reply.GetTraceId()
err = json.Unmarshal(b, request)
if err != nil {
response.Code = SrvErr
response.Errors = append(response.Errors, &proto.Error{Code: JsonFromatCode, Msg: JsonFormatErr})
wr.Write(http.StatusOK, response)
} else {
response.Code = SrvOk
s.queryConn(sctx, response, request)
}
wr.Write(http.StatusOK, response)
default:
reply.Errors = append(reply.Errors, &proto.Error{Code: NotSupportCode, Msg: NotSupportMsg})
wr.Write(http.StatusOK, reply)
}
}
func (s *Srv) queryConn(ctx context.Context, response *queryResponse, request *queryRequest) {
var conns, err = s.ctCli.ShowConn(ctx)
if err != nil {
s.logger.Errorw(ctx, "query ct fail", "err", err)
response.Errors = append(response.Errors, &proto.Error{Code: QueryCode, Msg: QueryMsg})
response.Code = SrvErr
}
response.Conn = conns
}