-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
200 lines (175 loc) · 4.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
package web
import (
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net"
"net/http"
"strings"
)
type H map[string]any
type Context struct {
// origin objects
Writer http.ResponseWriter
Request *http.Request
// request info
Path string
Method string
params map[string]string
// response info
StatusCode int
// extra info
extras map[string]any
// middlewares and route
handlers []Handler
index int
// server pointer
server *Server
// response tag
isResponse bool
}
func newContext(w http.ResponseWriter, req *http.Request) *Context {
return &Context{
Path: req.URL.Path,
Method: req.Method,
Request: req,
Writer: w,
extras: make(map[string]any),
index: -1,
}
}
func decodeJSON(r io.Reader, obj any) error {
decoder := json.NewDecoder(r)
if err := decoder.Decode(obj); err != nil {
return err
}
return nil
}
// Next is used in middleware, it means executing the next middleware or handle
func (c *Context) Next() {
c.index++
for ; c.index < len(c.handlers); c.index++ {
c.handlers[c.index].Handle(c)
}
}
// Abort is used in middleware, it means stopping the current middleware
func (c *Context) Abort() {
c.index = len(c.handlers)
}
// Abort is used in middleware, it means stopping the current middleware
func (c *Context) isAbort() bool {
return c.index == len(c.handlers)
}
// Param is used to get the parameter at path.
func (c *Context) Param(key string) string {
value, _ := c.params[key]
return value
}
// Extra is used to get the info which set by user
func (c *Context) Extra(key string) any {
value, _ := c.extras[key]
return value
}
// SetExtra is used to set the info by user
func (c *Context) SetExtra(key string, v any) {
c.extras[key] = v
}
func (c *Context) GetHeader(key string) string {
return c.Request.Header.Get(key)
}
func (c *Context) ClientIp() string{
remoteAddr := c.Request.RemoteAddr
forwardedFor := c.GetHeader("X-Forwarded-For")
if forwardedFor != "" {
remoteAddr = strings.Split(forwardedFor, ",")[0]
}
return net.ParseIP(remoteAddr).String()
}
func (c *Context) FormData(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) BindJson(obj any) (any, error) {
err := decodeJSON(c.Request.Body, obj)
if err != nil {
return nil, err
}
return obj, nil
}
func (c *Context) Binary() ([]byte, error) {
var content []byte
var tmp = make([]byte, 128)
for {
n, err := c.Request.Body.Read(tmp)
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
content = append(content, tmp[:n]...)
}
return content, nil
}
func (c *Context) FormFile(key string) (multipart.File, *multipart.FileHeader, error) {
return c.Request.FormFile(key)
}
func (c *Context) Status(code int) {
c.StatusCode = code
c.Writer.WriteHeader(code)
}
func (c *Context) SetHeader(key string, value string) {
c.Writer.Header().Set(key, value)
}
func (c *Context) String(code int, format string, values ...any) {
if c.isResponse {
return
}
c.SetHeader("Content-Type", "text/plain")
c.Status(code)
if _, err := c.Writer.Write([]byte(fmt.Sprintf(format, values...))); err != nil {
panic(err)
}
c.isResponse = true
}
func (c *Context) JSON(code int, obj any) {
if c.isResponse {
return
}
c.SetHeader("Content-Type", "application/json")
c.Status(code)
encoder := json.NewEncoder(c.Writer)
if err := encoder.Encode(obj); err != nil {
panic(err)
}
c.isResponse = true
}
func (c *Context) Data(code int, contentType string, data []byte) {
if c.isResponse {
return
}
c.SetHeader("Content-Type", contentType)
c.Status(code)
if _, err := c.Writer.Write(data); err != nil {
panic(err)
}
c.isResponse = true
}
func (c *Context) HTML(code int, name string, data interface{}) {
if c.isResponse {
return
}
c.SetHeader("Content-Type", "text/html")
c.Status(code)
if err := c.server.htmlTemplates.ExecuteTemplate(c.Writer, name, data); err != nil {
panic(err)
}
c.isResponse = true
}
func (c *Context) Fail(code int, err string) {
c.Abort()
c.String(code, err)
}