-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
180 lines (156 loc) · 3.61 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
package tgin
import (
"bytes"
"encoding/json"
"fmt"
"mime/multipart"
"net/http"
)
const formMaxMemory = 32 << 20 // 32 MB
type Context struct {
Method string
Request *http.Request
Writer http.ResponseWriter
aborted bool
served bool
values map[string]interface{}
middlewares []RouteHandler
index int
mux *http.ServeMux
}
func newContext(w http.ResponseWriter, r *http.Request) *Context {
return &Context{
Method: r.Method,
Request: r,
Writer: w,
aborted: false,
served: false,
index: 0,
middlewares: nil,
values: make(map[string]interface{}),
}
}
func (c *Context) json(code int, val interface{}, indented bool) {
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
if indented {
enc.SetIndent("", " ")
}
err := enc.Encode(val)
if err != nil {
c.Text(500, fmt.Sprintf("Server Error!\n%v", err))
return
}
w := c.Writer
hdr := w.Header()
hdr.Set("Content-Type", "application/json; charset=utf-8")
hdr.Set("Content-Length", fmt.Sprintf("%d", buf.Len()))
w.WriteHeader(code)
buf.WriteTo(w)
}
func (c *Context) BindJSON(obj interface{}) error {
dec := json.NewDecoder(c.Request.Body)
return dec.Decode(obj)
}
func (c *Context) JSON(code int, val interface{}) {
c.json(code, val, false)
}
func (c *Context) IndentedJSON(code int, val interface{}) {
c.json(code, val, true)
}
func (c *Context) Text(code int, message string) {
w := c.Writer
hdr := w.Header()
hdr.Set("Content-Type", "text/plain; charset=utf-8")
hdr.Set("Content-Length", fmt.Sprintf("%d", len(message)))
w.WriteHeader(code)
w.Write([]byte(message))
}
func (c *Context) String(code int, format string, values ...interface{}) {
body := fmt.Sprintf(format, values...)
c.Text(code, body)
}
func (c *Context) GetQuery(key string) (string, bool) {
ret := c.Request.URL.Query().Get(key)
if ret == "" {
return ret, false
}
return ret, true
}
func (c *Context) GetQueryArray(key string) ([]string, bool) {
val := c.Request.URL.Query()[key]
if len(val) == 0 {
return []string{}, false
}
return val, true
}
func (c *Context) GetHeader(key string) string {
return c.Request.Header.Get(key)
}
func (c *Context) File(filePath string) {
http.ServeFile(c.Writer, c.Request, filePath)
}
func (c *Context) Abort() {
c.aborted = true
}
func (c *Context) AbortWithStatus(code int) {
w := c.Writer
w.WriteHeader(code)
c.Abort()
}
func (c *Context) Set(key string, obj interface{}) {
c.values[key] = obj
}
func (c *Context) Get(key string) (interface{}, bool) {
obj, have := c.values[key]
return obj, have
}
func (c *Context) Header(key, value string) {
if value == "" {
c.Writer.Header().Del(key)
return
}
c.Writer.Header().Set(key, value)
}
func (c *Context) Redirect(code int, location string) {
http.Redirect(c.Writer, c.Request, location, code)
}
func (c *Context) PostForm(key string) string {
ret := c.PostFormArray(key)
if len(ret) == 0 {
return ""
}
return ret[0]
}
func (c *Context) PostFormArray(key string) []string {
if c.Request.Form == nil {
c.Request.ParseMultipartForm(formMaxMemory)
}
vals := c.Request.Form[key]
if len(vals) == 0 {
return []string{}
}
return vals
}
func (c *Context) FormFile(key string) (*multipart.FileHeader, error) {
_, header, err := c.Request.FormFile(key)
return header, err
}
func (c *Context) Next() {
numMw := len(c.middlewares)
if c.index < numMw {
for c.index < numMw {
m := c.middlewares[c.index]
c.index++
m(c)
if c.aborted {
break
}
}
}
// Middleware execute all We should serve it
if !c.served && !c.aborted {
c.served = true
c.mux.ServeHTTP(c.Writer, c.Request)
}
}