-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
263 lines (194 loc) · 5.03 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
package happyngine
import (
"encoding/json"
"github.com/wayt/happyngine/env"
"github.com/wayt/happyngine/log"
"net/http"
"os"
"regexp"
"strconv"
"strings"
)
var Hostname = ""
func init() {
var err error
Hostname, err = os.Hostname()
if err != nil || Hostname == "" {
Hostname = env.Get("NODE_NAME")
}
}
type Context struct {
Request *http.Request `json:"-"`
Response http.ResponseWriter `json:"-"`
API *API `json:"-"`
Session *Session `json:"-"`
ResponseStatusCode int `json:"-"` // Because we can't retrieve the status from http.ResponseWriter
ResponseLength int `json:"-"`
Errors map[string]string `json:"-"`
ErrorCode int `json:"-"`
currentMiddleware int
middlewares []MiddlewareHandler
action ActionHandler
}
func NewContext(req *http.Request, resp http.ResponseWriter, api *API) *Context {
c := new(Context)
c.Request = req
c.Response = resp
c.API = api
c.ResponseStatusCode = 200
c.Errors = make(map[string]string)
c.currentMiddleware = -1
return c
}
func (c *Context) Next() {
c.currentMiddleware += 1
if len(c.middlewares) <= c.currentMiddleware {
// Process action
action := c.action(c)
if action.IsValid() {
action.Run()
}
} else {
next := c.middlewares[c.currentMiddleware]
next(c)
}
if c.ResponseLength == 0 {
errors, code := c.GetErrors()
if len(errors) != 0 {
response := `{"error":["` + strings.Join(errors, `","`) + `"]}`
c.Send(code, response)
}
}
}
// Session may be nil
func (c *Context) FetchSession(name string) *Session {
sess := GetSession(c.Request, name)
c.Session = sess
return sess
}
func (c *Context) NewSession(name string) *Session {
secure := env.Get("SECURE_COOKIE")
c.Session = NewSession(name, &SessionOptions{
Path: "/",
MaxAge: env.GetInt("SESSION_MAX_AGE"),
HttpOnly: true,
Secure: secure != "false",
})
return c.Session
}
func (c *Context) GetParam(key string) string {
return c.Request.FormValue(key)
}
func (c *Context) GetIntParam(key string) int {
value, err := strconv.Atoi(c.Request.FormValue(key))
if err != nil {
return 0
}
return value
}
func (c *Context) GetInt64Param(key string) int64 {
value, err := strconv.ParseInt(c.Request.FormValue(key), 10, 64)
if err != nil {
return 0
}
return value
}
func (c *Context) GetURLParam(key string) string {
return c.Request.URL.Query().Get(key)
}
func (c *Context) GetURLIntParam(key string) int {
value, err := strconv.Atoi(c.GetURLParam(key))
if err != nil {
return 0
}
return value
}
func (c *Context) GetURLInt64Param(key string) int64 {
value, err := strconv.ParseInt(c.GetURLParam(key), 10, 64)
if err != nil {
return 0
}
return value
}
func (c *Context) JSON(code int, obj interface{}, headers ...string) {
data, err := json.Marshal(obj)
if err != nil {
panic(err)
}
c.SendByte(code, data, append(headers, "Content-Type: application/json")...)
}
func (c *Context) Send(code int, text string, headers ...string) {
c.SendByte(code, []byte(text), headers...)
}
func (c *Context) SendByte(code int, data []byte, headers ...string) {
hasMime := false
for _, header := range headers {
array := strings.Split(header, ":")
if len(array) != 2 {
continue
}
c.Response.Header().Add(array[0], array[1])
if array[0] == "Content-Type" {
hasMime = true
}
}
if Hostname != "" {
c.Response.Header().Add("X-happyngine-node", Hostname)
}
if !hasMime {
c.Response.Header().Add("Content-Type", "application/json")
}
for k, v := range c.API.Headers {
matchs := regexp.MustCompile(`^{(.*)}$`).FindStringSubmatch(v)
if len(matchs) != 0 {
header := matchs[1]
if v = c.Request.Header.Get(header); len(v) == 0 {
continue
}
}
c.Response.Header().Add(k, v)
}
if c.Session != nil {
if c.Session.Changed() {
c.Session.Save(c.Request, c.Response)
}
}
c.Response.WriteHeader(code)
c.ResponseLength, _ = c.Response.Write(data)
c.ResponseStatusCode = code
}
func (c *Context) RemoteIP() string {
ipStr := strings.SplitN(c.Request.RemoteAddr, ":", 2)[0]
if header := c.Request.Header.Get("X-Forwarded-For"); len(header) != 0 {
// Because of google http load balancer
// X-Forwarded-For: <client IP(s)>, <global forwarding rule external IP> (requests only)
ipStr = strings.Split(header, ",")[0]
}
return strings.Trim(ipStr, " ")
}
func (c *Context) Debugln(args ...interface{}) {
log.Debugln(args)
}
func (c *Context) Warningln(args ...interface{}) {
log.Warningln(args)
}
func (c *Context) Errorln(args ...interface{}) {
log.Errorln(args)
}
func (c *Context) Criticalln(args ...interface{}) {
log.Criticalln(args)
}
func (c *Context) AddError(code int, text string) {
c.ErrorCode = code
c.Errors[text] = text
}
func (c *Context) GetErrors() ([]string, int) {
errs := make([]string, 0)
for _, err := range c.Errors {
errs = append(errs, err)
}
return errs, c.ErrorCode
}
func (c *Context) HasErrors() bool {
return len(c.Errors) != 0
}