-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathContext.go
496 lines (415 loc) · 11.2 KB
/
Context.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
package pgo
import (
"flag"
"fmt"
"net/http"
"os"
"reflect"
"strings"
"time"
"github.com/pinguo/pgo/Util"
)
type objectItem struct {
name string
rv reflect.Value
}
// Context pgo request context, context is not goroutine
// safe, copy context to use in other goroutines
type Context struct {
server *Server
response Response
input *http.Request
output http.ResponseWriter
startTime time.Time
controllerId string
actionId string
userData map[string]interface{}
plugins []IPlugin
index int
objects []objectItem
Profiler
Logger
}
// start plugin chain process
func (c *Context) process(plugins []IPlugin) {
// generate request id
logId := c.GetHeader("X-Log-Id", "")
if logId == "" {
logId = Util.GenUniqueId()
}
// reset properties
c.startTime = time.Now()
c.controllerId = ""
c.actionId = ""
c.userData = nil
c.plugins = plugins
c.index = -1
c.Profiler.reset()
c.Logger.init(App.GetName(), logId, App.GetLog())
// finish response
defer c.finish()
// process request
c.Next()
}
// finish process request
func (c *Context) finish() {
// process unhandled panic
if v := recover(); v != nil {
status := http.StatusInternalServerError
switch e := v.(type) {
case *Exception:
status = e.GetStatus()
c.End(status, []byte(App.GetStatus().GetText(status, c, e.GetMessage())))
default:
c.End(status, []byte(http.StatusText(status)))
}
c.Error("%s, trace[%s]", Util.ToString(v), Util.PanicTrace(TraceMaxDepth, false))
}
// write header if not yet
c.response.finish()
// write access log
if c.server.enableAccessLog {
c.Notice("%s %s %d %d %dms pushlog[%s] profile[%s] counting[%s]",
c.GetMethod(), c.GetPath(), c.GetStatus(), c.GetSize(), c.GetElapseMs(),
c.GetPushLogString(), c.GetProfileString(), c.GetCountingString())
}
// clean objects
c.clean()
}
// cache object in context
func (c *Context) cache(name string, rv reflect.Value) {
if App.GetMode() == ModeWeb && len(c.objects) < MaxCacheObjects {
c.objects = append(c.objects, objectItem{name, rv})
}
}
// clean all cached objects
func (c *Context) clean() {
container, num := App.GetContainer(), len(c.objects)
for i := 0; i < num; i++ {
name, rv := c.objects[i].name, c.objects[i].rv
container.Put(name, rv)
}
// reset object pool to empty
if num > 0 {
c.objects = c.objects[:0]
}
}
// Next start running plugin chain
func (c *Context) Next() {
c.index++
for num := len(c.plugins); c.index < num; c.index++ {
c.plugins[c.index].HandleRequest(c)
}
}
// Abort stop running plugin chain
func (c *Context) Abort() {
c.index = MaxPlugins
}
// Copy copy context
func (c *Context) Copy() *Context {
cp := *c
cp.Profiler.reset()
cp.userData = nil
cp.plugins = nil
cp.index = MaxPlugins
cp.objects = nil
return &cp
}
// GetElapseMs get elapsed ms since request start
func (c *Context) GetElapseMs() int {
elapse := time.Now().Sub(c.startTime)
return int(elapse.Nanoseconds() / 1e6)
}
// GetLogId get log id of current context
func (c *Context) GetLogId() string {
return c.Logger.logId
}
// GetStatus get response status
func (c *Context) GetStatus() int {
return c.response.status
}
// GetSize get response size
func (c *Context) GetSize() int {
return c.response.size
}
// SetInput
func (c *Context) SetInput(r *http.Request) {
c.input = r
}
// SetInput
func (c *Context) GetInput() *http.Request {
return c.input
}
// SetOutput
func (c *Context) SetOutput(w http.ResponseWriter) {
c.output = w
}
// GetOutput
func (c *Context) GetOutput() http.ResponseWriter {
return c.output
}
// SetControllerId
func (c *Context) SetControllerId(id string) {
c.controllerId = id
}
// GetControllerId
func (c *Context) GetControllerId() string {
return c.controllerId
}
// SetActionId
func (c *Context) SetActionId(id string) {
c.actionId = id
}
// GetActionId
func (c *Context) GetActionId() string {
return c.actionId
}
// SetUserData set user data to current context
func (c *Context) SetUserData(key string, data interface{}) {
if nil == c.userData {
c.userData = make(map[string]interface{})
}
c.userData[key] = data
}
// GetUserData get user data from current context
func (c *Context) GetUserData(key string, dft interface{}) interface{} {
if data, ok := c.userData[key]; ok {
return data
}
return dft
}
// GetMethod get request method
func (c *Context) GetMethod() string {
if c.input != nil {
return c.input.Method
}
return "CMD"
}
// GetQuery get first url query value by name
func (c *Context) GetQuery(name, dft string) string {
if c.input != nil {
v := c.input.URL.Query().Get(name)
if len(v) > 0 {
return v
}
}
return dft
}
// GetQueryAll get first value of all url queries
func (c *Context) GetQueryAll() map[string]string {
m := make(map[string]string)
if c.input != nil {
queries := c.input.URL.Query()
for k, v := range queries {
if len(v) > 0 {
m[k] = v[0]
} else {
m[k] = ""
}
}
}
return m
}
// GetPost get first post value by name
func (c *Context) GetPost(name, dft string) string {
if c.input != nil {
v := c.input.PostFormValue(name)
if len(v) > 0 {
return v
}
}
return dft
}
// GetPostAll get first value of all posts
func (c *Context) GetPostAll() map[string]string {
m := make(map[string]string)
if c.input != nil {
// make sure c.input.ParseMultipartForm has been called
c.input.PostFormValue("")
for k, v := range c.input.PostForm {
if len(v) > 0 {
m[k] = v[0]
} else {
m[k] = ""
}
}
}
return m
}
// GetParam get first param value by name, post take precedence over get
func (c *Context) GetParam(name, dft string) string {
if c.input != nil {
v := c.input.FormValue(name)
if len(v) > 0 {
return v
}
}
return dft
}
// GetParamAll get first value of all params, post take precedence over get
func (c *Context) GetParamAll() map[string]string {
m := make(map[string]string)
if c.input != nil {
// make sure c.input.ParseMultipartForm has been called
c.input.FormValue("")
for k, v := range c.input.Form {
if len(v) > 0 {
m[k] = v[0]
} else {
m[k] = ""
}
}
}
return m
}
// GetParamMap get map value from GET/POST
func (c *Context) GetParamMap(name string) map[string]string {
// TODO name[k1]=v1&name[k2]=v2
return nil
}
// GetQueryMap get map value from GET
func (c *Context) GetQueryMap(name string) map[string]string {
// TODO name[k1]=v1&name[k2]=v2
return nil
}
// GetPostMap get map value from POST
func (c *Context) GetPostMap(name string) map[string]string {
// TODO name[k1]=v1&name[k2]=v2
return nil
}
// GetParamArray get array value from GET/POST
func (c *Context) GetParamArray(name string) []string {
// TODO name[]=v1&name[]=v2
return nil
}
// GetQueryArray get array value from GET
func (c *Context) GetQueryArray(name string) []string {
// TODO name[]=v1&name[]=v2
return nil
}
// GetPostArray get array value from POST
func (c *Context) GetPostArray(name string) []string {
// TODO name[]=v1&name[]=v2
return nil
}
// GetCookie get first cookie value by name
func (c *Context) GetCookie(name, dft string) string {
if c.input != nil {
v, e := c.input.Cookie(name)
if e == nil && len(v.Value) > 0 {
return v.Value
}
}
return dft
}
// GetCookieAll get first value of all cookies
func (c *Context) GetCookieAll() map[string]string {
m := make(map[string]string)
if c.input != nil {
cookies := c.input.Cookies()
for _, cookie := range cookies {
if _, ok := m[cookie.Name]; !ok {
m[cookie.Name] = cookie.Value
}
}
}
return m
}
// GetHeader get first header value by name,name is case-insensitive
func (c *Context) GetHeader(name, dft string) string {
if c.input != nil {
v := c.input.Header.Get(name)
if len(v) > 0 {
return v
}
}
return dft
}
// GetHeaderAll get first value of all headers
func (c *Context) GetHeaderAll() map[string]string {
m := make(map[string]string)
if c.input != nil {
for k, v := range c.input.Header {
if len(v) > 0 {
m[k] = v[0]
} else {
m[k] = ""
}
}
}
return m
}
// GetPath get request path
func (c *Context) GetPath() string {
// for web
if c.input != nil {
return c.input.URL.Path
}
// for cmd
f := flag.Lookup("cmd")
if f != nil && len(f.Value.String()) > 0 {
return f.Value.String()
}
return "/"
}
// GetClientIp get client ip
func (c *Context) GetClientIp() string {
if xff := c.GetHeader("X-Forwarded-For", ""); len(xff) > 0 {
if pos := strings.IndexByte(xff, ','); pos > 0 {
return strings.TrimSpace(xff[:pos])
} else {
return xff
}
}
if ip := c.GetHeader("X-Client-Ip", ""); len(ip) > 0 {
return ip
}
if ip := c.GetHeader("X-Real-Ip", ""); len(ip) > 0 {
return ip
}
if c.input != nil && len(c.input.RemoteAddr) > 0 {
pos := strings.LastIndexByte(c.input.RemoteAddr, ':')
if pos > 0 {
return c.input.RemoteAddr[:pos]
} else {
return c.input.RemoteAddr
}
}
return ""
}
// validate query param, return string validator
func (c *Context) ValidateQuery(name string, dft ...interface{}) *StringValidator {
return ValidateString(c.GetQuery(name, ""), name, dft...)
}
// validate post param, return string validator
func (c *Context) ValidatePost(name string, dft ...interface{}) *StringValidator {
return ValidateString(c.GetPost(name, ""), name, dft...)
}
// validate get/post param, return string validator
func (c *Context) ValidateParam(name string, dft ...interface{}) *StringValidator {
return ValidateString(c.GetParam(name, ""), name, dft...)
}
// set response header, no effect if any header has sent
func (c *Context) SetHeader(name, value string) {
if c.output != nil {
c.output.Header().Set(name, value)
}
}
// convenient way to set response cookie
func (c *Context) SetCookie(cookie *http.Cookie) {
if c.output != nil {
http.SetCookie(c.output, cookie)
}
}
// send response
func (c *Context) End(status int, data []byte) {
if c.output != nil {
c.SetHeader("X-Log-Id", c.Logger.logId)
c.SetHeader("X-Cost-Time", fmt.Sprintf("%dms", c.GetElapseMs()))
c.output.WriteHeader(status)
c.output.Write(data)
} else if len(data) > 0 {
os.Stdout.Write(data)
os.Stdout.WriteString("\n")
}
}