-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.go
More file actions
179 lines (142 loc) · 3.45 KB
/
context.go
File metadata and controls
179 lines (142 loc) · 3.45 KB
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
package mapes
import (
"context"
"encoding/json"
"encoding/xml"
"html/template"
"mime/multipart"
"net/http"
"reflect"
"slices"
)
type Context struct {
Writer http.ResponseWriter
Request *http.Request
m map[string]any
params map[string]string
}
func NewContext(w http.ResponseWriter, r *http.Request) *Context {
return &Context{w, r, make(map[string]any), make(map[string]string)}
}
func (c *Context) Json(code int, value any) error {
c.SetHeader("content-type", "application/json")
indent, err := json.MarshalIndent(value, "", " ")
if err != nil {
return err
}
c.Writer.WriteHeader(code)
_, err = c.Writer.Write([]byte(indent))
return err
}
func (c *Context) String(code int, value string) error {
c.Status(code)
c.SetHeader("Content-Type", "text/plain")
_, err := c.Writer.Write([]byte(value))
return err
}
func (c *Context) Status(code int) {
c.Writer.WriteHeader(code)
}
func (c *Context) None(code int) error {
c.Writer.WriteHeader(code)
return nil
}
func (c *Context) Get(key string) any {
return c.m[key]
}
func (c *Context) Set(key string, value any) {
c.m[key] = value
}
func (c *Context) Context() context.Context {
return c.Request.Context()
}
func (c *Context) Bind(dst any) error {
if slices.Contains(c.Request.Header["Content-Type"], "application/json") {
err := c.bindJson(dst)
if err != nil {
return err
}
}
if slices.Contains(c.Request.Header["Content-Type"], "application/xml") {
err := c.bindXml(dst)
if err != nil {
return err
}
}
if slices.Contains(c.Request.Header["Content-Type"], "application/x-www-form-urlencoded") {
err := c.bindForm(dst)
if err != nil {
return err
}
}
c.bindUrl(dst)
return nil
}
func (c *Context) bindJson(dst any) error {
decoder := json.NewDecoder(c.Request.Body)
return decoder.Decode(dst)
}
func (c *Context) bindXml(dst any) error {
decoder := xml.NewDecoder(c.Request.Body)
return decoder.Decode(dst)
}
func (c *Context) bindForm(dst any) error {
err := c.Request.ParseForm()
if err != nil {
return err
}
v := reflect.ValueOf(dst).Elem()
t := v.Type()
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if tag := field.Tag.Get("form"); tag != "" {
v.Field(i).SetString(c.Request.FormValue(tag))
}
}
return nil
}
func (c *Context) bindUrl(dst any) {
v := reflect.ValueOf(dst).Elem()
t := v.Type()
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if tag := field.Tag.Get("query"); tag != "" {
v.Field(i).SetString(c.Query(tag))
}
if tag := field.Tag.Get("param"); tag != "" {
v.Field(i).SetString(c.Param(tag))
}
}
}
func (c *Context) Param(key string) string {
return c.params[key]
}
func (c *Context) Query(key string) string {
return c.Request.URL.Query().Get(key)
}
func (c *Context) FormFile(key string) (*multipart.FileHeader, error) {
_, m, err := c.Request.FormFile(key)
if err != nil {
return nil, err
}
return m, nil
}
func (c *Context) MultipartForm() *multipart.Form {
return c.Request.MultipartForm
}
func (c *Context) SetHeader(key, value string) {
c.Writer.Header().Set(key, value)
}
func (c *Context) AddHeader(key, value string) {
c.Writer.Header().Add(key, value)
}
func (c *Context) FormValues(key string) string {
return c.Request.PostFormValue(key)
}
func (c *Context) SetCookie(cookie *http.Cookie) {
http.SetCookie(c.Writer, cookie)
}
func (c *Context) RenderHtml(name string, data any) error {
tmpl := template.Must(template.ParseFiles(name))
return tmpl.Execute(c.Writer, data)
}