forked from devfeel/dotweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
436 lines (378 loc) · 11.8 KB
/
server.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
package dotweb
import (
"fmt"
"github.com/devfeel/dotweb/framework/convert"
"github.com/devfeel/dotweb/framework/exception"
"github.com/devfeel/dotweb/framework/json"
"github.com/devfeel/dotweb/framework/log"
"github.com/devfeel/dotweb/session"
"net/http"
"strings"
"sync"
"time"
"compress/gzip"
"github.com/devfeel/dotweb/router"
"golang.org/x/net/websocket"
"io"
"net/url"
)
const (
RouteMethod_GET = "GET"
RouteMethod_HEAD = "HEAD"
RouteMethod_OPTIONS = "OPTIONS"
RouteMethod_POST = "POST"
RouteMethod_PUT = "PUT"
RouteMethod_PATCH = "PATCH"
RouteMethod_DELETE = "DELETE"
RouteMethod_HiJack = "HiJack"
RouteMethod_WebSocket = "WebSocket"
DefaultGzipLevel = 9
gzipScheme = "gzip"
)
type (
//HttpModule定义
HttpModule struct {
//响应请求时作为 HTTP 执行管线链中的第一个事件发生
OnBeginRequest func(*HttpContext)
//响应请求时作为 HTTP 执行管线链中的最后一个事件发生。
OnEndRequest func(*HttpContext)
}
//HttpServer定义
HttpServer struct {
router *router.Router
DotApp *DotWeb
sessionManager *session.SessionManager
lock_session *sync.RWMutex
pool *pool
ServerConfig *ServerConfig
binder Binder
}
//server global config
ServerConfig struct {
EnabledDebug bool //is enabled debug mode
EnabledSession bool //is enabled seesion
EnabledGzip bool //is enabled gzip
}
//pool定义
pool struct {
response sync.Pool
context sync.Pool
}
)
func NewServerConfig() *ServerConfig {
config := &ServerConfig{
EnabledDebug: false,
EnabledSession: false,
}
return config
}
// Handle is a function that can be registered to a route to handle HTTP
// requests. Like http.HandlerFunc, but has a third parameter for the values of
// wildcards (variables).
type HttpHandle func(*HttpContext)
func NewHttpServer() *HttpServer {
server := &HttpServer{
router: router.New(),
pool: &pool{
response: sync.Pool{
New: func() interface{} {
return &Response{}
},
},
context: sync.Pool{
New: func() interface{} {
return &HttpContext{}
},
},
},
ServerConfig: NewServerConfig(),
lock_session: new(sync.RWMutex),
binder: &binder{},
}
return server
}
//ServeHTTP makes the httprouter implement the http.Handler interface.
func (server *HttpServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
//针对websocket与调试信息特殊处理
if checkIsWebSocketRequest(req) {
http.DefaultServeMux.ServeHTTP(w, req)
} else {
//设置header信息
w.Header().Set(HeaderServer, DefaultServerName)
server.router.ServeHTTP(w, req)
}
}
//init session manager
func (server *HttpServer) InitSessionManager(config *session.StoreConfig) {
if server.sessionManager == nil {
//设置Session
server.lock_session.Lock()
if manager, err := session.NewDefaultSessionManager(config); err != nil {
//panic error with create session manager
panic(err.Error())
} else {
server.sessionManager = manager
}
server.lock_session.Unlock()
}
}
/*
* 关联当前HttpServer实例对应的DotServer实例
*/
func (server *HttpServer) setDotApp(dotApp *DotWeb) {
server.DotApp = dotApp
}
//get session manager in current httpserver
func (server *HttpServer) GetSessionManager() *session.SessionManager {
if !server.ServerConfig.EnabledSession {
return nil
}
return server.sessionManager
}
// GET is a shortcut for router.Handle("GET", path, handle)
func (server *HttpServer) GET(path string, handle HttpHandle) {
server.RegisterRoute(RouteMethod_GET, path, handle)
}
// HEAD is a shortcut for router.Handle("HEAD", path, handle)
func (server *HttpServer) HEAD(path string, handle HttpHandle) {
server.RegisterRoute(RouteMethod_HEAD, path, handle)
}
// OPTIONS is a shortcut for router.Handle("OPTIONS", path, handle)
func (server *HttpServer) OPTIONS(path string, handle HttpHandle) {
server.RegisterRoute(RouteMethod_OPTIONS, path, handle)
}
// POST is a shortcut for router.Handle("POST", path, handle)
func (server *HttpServer) POST(path string, handle HttpHandle) {
server.RegisterRoute(RouteMethod_POST, path, handle)
}
// PUT is a shortcut for router.Handle("PUT", path, handle)
func (server *HttpServer) PUT(path string, handle HttpHandle) {
server.RegisterRoute(RouteMethod_PUT, path, handle)
}
// PATCH is a shortcut for router.Handle("PATCH", path, handle)
func (server *HttpServer) PATCH(path string, handle HttpHandle) {
server.RegisterRoute(RouteMethod_PATCH, path, handle)
}
// DELETE is a shortcut for router.Handle("DELETE", path, handle)
func (server *HttpServer) DELETE(path string, handle HttpHandle) {
server.RegisterRoute(RouteMethod_DELETE, path, handle)
}
// DELETE is a shortcut for router.Handle("DELETE", path, handle)
func (server *HttpServer) HiJack(path string, handle HttpHandle) {
server.RegisterRoute(RouteMethod_GET, path, handle)
}
// shortcut for router.Handle(httpmethod, path, handle)
// support GET\POST\DELETE\PUT\HEAD\PATCH\OPTIONS\HiJack\WebSocket
func (server *HttpServer) RegisterRoute(routeMethod string, path string, handle HttpHandle) {
logger.Log("Dotweb:RegisterRoute ["+routeMethod+"] ["+path+"]", LogTarget_HttpServer, LogLevel_Debug)
//hijack mode,use get and isHijack = true
if routeMethod == RouteMethod_HiJack {
server.router.Handle(RouteMethod_GET, path, server.wrapRouterHandle(handle, true))
return
}
//websocket mode,use default httpserver
if routeMethod == RouteMethod_WebSocket {
http.Handle(path, websocket.Handler(server.wrapWebSocketHandle(handle)))
return
}
//GET\POST\DELETE\PUT\HEAD\PATCH\OPTIONS mode
server.router.Handle(routeMethod, path, server.wrapRouterHandle(handle, false))
return
}
// ServerFile is a shortcut for router.ServeFiles(path, filepath)
// simple demo:server.ServerFile("/src/*filepath", "/var/www")
func (server *HttpServer) ServerFile(urlpath string, filepath string) {
server.router.ServeFiles(urlpath, http.Dir(filepath))
}
// WebSocket is a shortcut for websocket.Handler
func (server *HttpServer) WebSocket(path string, handle HttpHandle) {
server.RegisterRoute(RouteMethod_WebSocket, path, handle)
}
func (server *HttpServer) Binder() Binder {
return server.binder
}
type LogJson struct {
RequestUrl string
HttpHeader string
HttpBody string
}
//wrap HttpHandle to httprouter.Handle
func (server *HttpServer) wrapRouterHandle(handle HttpHandle, isHijack bool) router.Handle {
return func(w http.ResponseWriter, r *http.Request, params router.Params) {
//get from pool
res := server.pool.response.Get().(*Response)
res.Reset(w)
httpCtx := server.pool.context.Get().(*HttpContext)
httpCtx.Reset(res, r, server, params)
//gzip
if server.ServerConfig.EnabledGzip {
gw, err := gzip.NewWriterLevel(w, DefaultGzipLevel)
if err != nil {
panic("use gzip error -> " + err.Error())
}
grw := &gzipResponseWriter{Writer: gw, ResponseWriter: w}
res.Reset(grw)
httpCtx.SetHeader(HeaderContentEncoding, gzipScheme)
}
//增加状态计数
GlobalState.AddRequestCount(1)
//session
//if exists client-sessionid, use it
//if not exists client-sessionid, new one
if server.ServerConfig.EnabledSession {
sessionId, err := server.GetSessionManager().GetClientSessionID(r)
if err == nil && sessionId != "" {
httpCtx.SessionID = sessionId
} else {
httpCtx.SessionID = server.GetSessionManager().NewSessionID()
cookie := http.Cookie{
Name: server.sessionManager.CookieName,
Value: url.QueryEscape(httpCtx.SessionID),
Path: "/",
}
httpCtx.WriteCookieObj(cookie)
}
}
//hijack处理
if isHijack {
_, hijack_err := httpCtx.Hijack()
if hijack_err != nil {
//输出内容
httpCtx.Response.WriteHeader(http.StatusInternalServerError)
httpCtx.Response.Header().Set(HeaderContentType, CharsetUTF8)
httpCtx.WriteString(hijack_err.Error())
}
}
startTime := time.Now()
defer func() {
var errmsg string
if err := recover(); err != nil {
errmsg = exception.CatchError("httpserver::RouterHandle", LogTarget_HttpServer, err)
//默认异常处理
if server.DotApp.ExceptionHandler != nil {
server.DotApp.ExceptionHandler(httpCtx, err)
}
//记录访问日志
headinfo := fmt.Sprintln(httpCtx.Response.Header)
logJson := LogJson{
RequestUrl: httpCtx.Request.RequestURI,
HttpHeader: headinfo,
HttpBody: errmsg,
}
logString := jsonutil.GetJsonString(logJson)
logger.Log(logString, LogTarget_HttpServer, LogLevel_Error)
//增加错误计数
GlobalState.AddErrorCount(1)
}
timetaken := int64(time.Now().Sub(startTime) / time.Millisecond)
//HttpServer Logging
logger.Log(httpCtx.Url()+" "+logString(httpCtx, timetaken), LogTarget_HttpRequest, LogLevel_Debug)
if server.ServerConfig.EnabledGzip {
var w io.Writer
w = res.Writer().(*gzipResponseWriter).Writer
w.(*gzip.Writer).Close()
}
// Return to pool
server.pool.response.Put(res)
//release context
httpCtx.release()
server.pool.context.Put(httpCtx)
}()
//处理前置Module集合
for _, module := range server.DotApp.Modules {
if module.OnBeginRequest != nil {
module.OnBeginRequest(httpCtx)
}
}
//处理用户handle
//if already set HttpContext.End,ignore user handler - fixed issue #5
if !httpCtx.IsEnd() {
handle(httpCtx)
}
//处理后置Module集合
for _, module := range server.DotApp.Modules {
if module.OnEndRequest != nil {
module.OnEndRequest(httpCtx)
}
}
}
}
//wrap HttpHandle to websocket.Handle
func (server *HttpServer) wrapWebSocketHandle(handle HttpHandle) websocket.Handler {
return func(ws *websocket.Conn) {
//get from pool
httpCtx := server.pool.context.Get().(*HttpContext)
httpCtx.Reset(nil, ws.Request(), server, nil)
httpCtx.WebSocket = &WebSocket{
Conn: ws,
}
httpCtx.IsWebSocket = true
startTime := time.Now()
defer func() {
var errmsg string
if err := recover(); err != nil {
errmsg = exception.CatchError("httpserver::WebsocketHandle", LogTarget_HttpServer, err)
//记录访问日志
headinfo := fmt.Sprintln(httpCtx.WebSocket.Conn.Request().Header)
logJson := LogJson{
RequestUrl: httpCtx.WebSocket.Conn.Request().RequestURI,
HttpHeader: headinfo,
HttpBody: errmsg,
}
logString := jsonutil.GetJsonString(logJson)
logger.Log(logString, LogTarget_HttpServer, LogLevel_Error)
//增加错误计数
GlobalState.AddErrorCount(1)
}
timetaken := int64(time.Now().Sub(startTime) / time.Millisecond)
//HttpServer Logging
logger.Log(httpCtx.Url()+" "+logString(httpCtx, timetaken), LogTarget_HttpRequest, LogLevel_Debug)
// Return to pool
server.pool.context.Put(httpCtx)
}()
handle(httpCtx)
//增加状态计数
GlobalState.AddRequestCount(1)
}
}
//get default log string
func logString(ctx *HttpContext, timetaken int64) string {
var reqbytelen, resbytelen, method, proto, status, userip string
if !ctx.IsWebSocket {
reqbytelen = convert.Int642String(ctx.Request.ContentLength)
resbytelen = convert.Int642String(ctx.Response.Size)
method = ctx.Request.Method
proto = ctx.Request.Proto
status = convert.Int2String(ctx.Response.Status)
userip = ctx.RemoteIP()
} else {
reqbytelen = convert.Int642String(ctx.Request.ContentLength)
resbytelen = "0"
method = ctx.Request.Method
proto = ctx.Request.Proto
status = "0"
userip = ctx.RemoteIP()
}
log := method + " "
log += userip + " "
log += proto + " "
log += status + " "
log += reqbytelen + " "
log += resbytelen + " "
log += convert.Int642String(timetaken)
return log
}
//check request is the websocket request
func checkIsWebSocketRequest(req *http.Request) bool {
if req.Header.Get("Connection") == "Upgrade" {
return true
}
return false
}
//check request is startwith /debug/
func checkIsDebugRequest(req *http.Request) bool {
if strings.Index(req.RequestURI, "/debug/") == 0 {
return true
}
return false
}