forked from wanghongfei/gogate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server_handler.go
139 lines (108 loc) · 2.59 KB
/
server_handler.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package server
import (
"strconv"
asynclog "github.com/alecthomas/log4go"
"github.com/valyala/fasthttp"
"github.com/wanghongfei/gogate/utils"
)
const (
SERVICE_NAME = "key_service_name"
REQUEST_PATH = "key_request_path"
ROUTE_INFO = "key_route_info"
RELOAD_PATH = "/_mgr/reload"
)
// HTTP请求处理方法.
func (serv *Server) HandleRequest(ctx *fasthttp.RequestCtx) {
defer func() {
recoverPanic(ctx, serv)
serv.markRoutineDone()
}()
serv.markRoutineStart()
// 取出请求path
path := string(ctx.Path())
ctx.SetUserValue(REQUEST_PATH, path)
asynclog.Info("request received: %s %s", string(ctx.Method()), path)
// 处理reload请求
if path == RELOAD_PATH {
err := serv.ReloadRoute()
if nil != err {
asynclog.Error(err)
NewResponse(path, err.Error()).Send(ctx)
return
}
ctx.WriteString(serv.ExtractRoute())
return
}
newReq := new(fasthttp.Request)
ctx.Request.CopyTo(newReq)
// 调用Pre过虑器
ok := invokePreFilters(serv, ctx, newReq)
if !ok {
return
}
// 发请求
sw := utils.NewStopwatch()
resp, err := serv.sendRequest(ctx, newReq)
if nil != err {
asynclog.Error(err)
NewResponse(path, err.Error()).Send(ctx)
serv.recordTraffic(ctx, false)
return
}
serv.recordTraffic(ctx, true)
resp.Header.Add("Time", strconv.FormatInt(sw.Record(), 10))
resp.Header.Set("Server", "gogate")
// 调用Post过虑器
ok = invokePostFilters(serv, newReq, resp)
if !ok {
return
}
// 返回响应
sendResponse(ctx, resp)
}
func sendResponse(ctx *fasthttp.RequestCtx, resp *fasthttp.Response) {
// copy header
ctx.Response.Header = resp.Header
ctx.Response.Header.Add("proxy", "gogate")
ctx.Write(resp.Body())
}
func invokePreFilters(s *Server, ctx *fasthttp.RequestCtx, newReq *fasthttp.Request) bool {
for _, f := range s.preFilters {
next := f.FilterFunc(s, ctx, newReq)
if !next {
return false
}
}
return true
}
func invokePostFilters(s *Server, newReq *fasthttp.Request, resp *fasthttp.Response) bool {
for _, f := range s.postFilters {
next := f.FilterFunc(newReq, resp)
if !next {
return false
}
}
return true
}
func processPanic(ctx *fasthttp.RequestCtx, serv *Server) {
path := string(ctx.Path())
NewResponse(path, "system error").SendWithStatus(ctx, 500)
// 记录流量
serv.recordTraffic(ctx, false)
}
func recoverPanic(ctx *fasthttp.RequestCtx, serv *Server) {
if r := recover(); r != nil {
asynclog.Error(r)
processPanic(ctx, serv)
}
}
func (serv *Server) markRoutineStart() {
if nil != serv.wg {
serv.wg.Add(1)
}
}
func (serv *Server) markRoutineDone() {
if nil != serv.wg {
serv.wg.Done()
}
}