-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontext.go
94 lines (78 loc) · 1.51 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
package td
import (
"net/http"
"net/url"
)
type (
// Context
Context struct {
// Request
Request *http.Request
// ResponseWriter
ResponseWriter http.ResponseWriter
// index
index int
// http server
server *Server
// server plugins || handler
plugins Plugins
}
)
// Plugin
type Plugin func(ctx *Context)
// Plugins
type Plugins []Plugin
// Next
func (c *Context) Next() {
c.index += 1
if c.index <= len(c.plugins) {
c.plugins[c.index-1](c)
} else {
c.index = 1
c.plugins[0](c)
}
}
// Head
func (c *Context) Head(key string) string {
return c.Request.Header.Get(key)
}
// HeadSet
func (c *Context) HeadSet(key, value string) {
c.ResponseWriter.Header().Set(key, value)
}
// ContentType
func (c *Context) ContentType() string {
return c.Request.Header.Get("Content-Type")
}
// ContentType
func (c *Context) ContentTypeSet(value string) {
c.ResponseWriter.Header().Set("Content-Type", value)
}
// GetRemoteIP
func (c *Context) GetRemoteIP() string {
return c.Request.RemoteAddr
}
// RemoteAddr
func (c *Context) RemoteAddr() string {
return c.Request.RemoteAddr
}
// RequestURI
func (c *Context) RequestURI() string {
return c.Request.RequestURI
}
// QueryValues
func (c *Context) QueryValues() url.Values {
return c.Request.URL.Query()
}
// Query
func (c *Context) Query(key string) string {
return c.Request.URL.Query().Get(key)
}
// Method
func (c *Context) Method() string {
return c.Request.Method
}
// UserAgent
func (c *Context) UserAgent() string {
return c.Request.UserAgent()
}