-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
225 lines (192 loc) · 4.66 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
package gen
import (
"encoding/json"
"fmt"
"net"
"net/http"
"net/url"
"reflect"
"runtime"
"strings"
"sync"
"github.com/julienschmidt/httprouter"
)
const (
MIMEJSON = "application/json"
MIMEHTML = "text/html"
MIMEPlain = "text/plain"
MIMEPOSTForm = "application/x-www-form-urlencoded"
MIMEMultipart = "multipart/form-data"
)
type H map[string]any
type Context struct {
Writer http.ResponseWriter
Request *http.Request
Path string
Method string
Params httprouter.Params
handlers []HandlerFunc
index int
engine *Engine
mu sync.RWMutex // protects Keys
Keys map[string]any
StatusCode int
Errors []*error // Errors is a list of errors attached to all the handlers/middlewares
}
func newContext(w http.ResponseWriter, req *http.Request, params httprouter.Params) *Context {
return &Context{
Writer: w,
Request: req,
Path: req.URL.Path,
Method: req.Method,
Params: params,
index: -1,
}
}
// Copy returns a copy of the current context that can be safely used outside the request's scope.
// This has to be used when the context has to be passed to a goroutine.
func (c *Context) Copy() *Context {
cp := Context{
Request: c.Request,
Path: c.Path,
Method: c.Method,
index: len(c.handlers),
engine: c.engine,
}
cp.Keys = make(map[string]any, len(c.Keys))
c.mu.RLock()
for k, v := range c.Keys {
cp.Keys[k] = v
}
c.mu.RUnlock()
cp.Params = make(httprouter.Params, len(c.Params))
copy(cp.Params, c.Params)
return &cp
}
// HandlerName returns the main handler's name
func (c *Context) HandlerName() string {
len_ := len(c.handlers)
f := c.handlers[len_-1]
name := runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
return name
}
func (c *Context) Next() {
c.index++
for c.index < len(c.handlers) {
c.handlers[c.index](c)
c.index++
}
}
// Abort Note that this will not stop the current handler, often followed by `return`
func (c *Context) Abort() {
c.index = len(c.handlers)
}
func (c *Context) AbortWithStatus(code int) {
c.Status(code)
c.Abort()
}
func (c *Context) RemoteIP() string {
ip, _, _ := net.SplitHostPort(strings.TrimSpace(c.Request.RemoteAddr))
return ip
}
func (c *Context) Set(key string, value any) {
c.mu.Lock()
defer c.mu.Unlock()
if c.Keys == nil {
c.Keys = make(map[string]any)
} // lazy init
c.Keys[key] = value
}
func (c *Context) Get(key string) (value any, ok bool) {
c.mu.RLock()
defer c.mu.RUnlock()
value, ok = c.Keys[key]
return
}
func (c *Context) MustGet(key string) any {
if value, ok := c.Get(key); ok {
return value
}
panic("Key \"" + key + "\" does not exist!")
}
/**********************/
/******** INPUT *******/
/**********************/
func (c *Context) Param(key string) string {
value := c.Param(key)
return value
}
// PostForm for x-www-form-urlencoded POST
func (c *Context) PostForm(key string) string {
return c.Request.FormValue(key)
}
func (c *Context) Query(key string) string {
return c.Request.URL.Query().Get(key)
}
func (c *Context) Cookie(name string) (string, error) {
cookie, err := c.Request.Cookie(name)
if err != nil {
return "", err
}
val, _ := url.QueryUnescape(cookie.Value)
return val, nil
}
/***********************/
/******** OUTPUT *******/
/***********************/
func (c *Context) Status(code int) {
c.Writer.WriteHeader(code)
}
func (c *Context) SetHeader(key, value string) {
c.Writer.Header().Set(key, value)
}
func (c *Context) String(code int, format string, a ...any) {
c.SetHeader("Content-Type", "text/plain")
c.Status(code)
c.Writer.Write([]byte(fmt.Sprintf(format, a...)))
}
func (c *Context) JSON(code int, obj any) {
c.SetHeader("Content-Type", "application/json")
c.Status(code)
encoder := json.NewEncoder(c.Writer)
if err := encoder.Encode(obj); err != nil {
panic(err)
}
}
func (c *Context) HTML(code int, name string, data any) {
c.SetHeader("Content-Type", "text/html")
c.Status(code)
if err := c.engine.htmlTemplates.ExecuteTemplate(c.Writer, name, data); err != nil {
panic(err)
}
}
func (c *Context) Data(code int, contentType string, data []byte) {
c.SetHeader("Content-Type", contentType)
c.Status(code)
c.Writer.Write(data)
}
func (c *Context) File(filePath string) {
http.ServeFile(c.Writer, c.Request, filePath)
}
func (c *Context) Redirect(location string) {
http.Redirect(c.Writer, c.Request, location, http.StatusMovedPermanently)
}
func (c *Context) SetCookie(
name string,
value string,
maxAge int,
path string,
domain string,
secure bool,
httpOnly bool,
) {
http.SetCookie(c.Writer, &http.Cookie{
Name: name,
Value: url.QueryEscape(value),
MaxAge: maxAge,
Path: path,
Domain: domain,
Secure: secure,
HttpOnly: httpOnly,
})
}