/
httpcontext.go
165 lines (144 loc) 路 4.33 KB
/
httpcontext.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
package context
import (
"errors"
"github.com/yoyofx/yoyogo/utils/cast"
"github.com/yoyofx/yoyogo/web/actionresult"
"github.com/yoyofx/yoyogo/web/binding"
"github.com/yoyofxteam/dependencyinjection"
"net/http"
"sync"
)
const (
defaultTagName = "param"
jsonTagName = "json"
)
var (
defaultMaxMemory int64 = 32 << 20 // 32 MB
)
type H = map[string]interface{}
type HttpContext struct {
Input *Input
Output Output
RequiredServices dependencyinjection.IServiceProvider
store map[string]interface{}
storeMutex *sync.RWMutex
Result interface{}
}
func NewContext(w http.ResponseWriter, r *http.Request, maxRequestSizeMemory int64, sp dependencyinjection.IServiceProvider) *HttpContext {
if maxRequestSizeMemory <= defaultMaxMemory {
maxRequestSizeMemory = defaultMaxMemory
}
ctx := &HttpContext{}
ctx.init(w, r, maxRequestSizeMemory, sp)
return ctx
}
func (ctx *HttpContext) init(w http.ResponseWriter, r *http.Request, maxRequestSizeMemory int64, sp dependencyinjection.IServiceProvider) {
ctx.storeMutex = new(sync.RWMutex)
ctx.Input = NewInput(r, maxRequestSizeMemory)
ctx.Output = Output{Response: &CResponseWriter{w, 0, 0, nil}}
ctx.RequiredServices = sp
ctx.storeMutex.Lock()
ctx.store = make(map[string]interface{})
ctx.storeMutex.Unlock()
binding.SetRequestMaxMemory(maxRequestSizeMemory)
}
//SetItem Set data in context.
func (ctx *HttpContext) SetItem(key string, val interface{}) {
ctx.storeMutex.Lock()
if ctx.store == nil {
ctx.store = make(map[string]interface{})
}
ctx.store[key] = val
ctx.storeMutex.Unlock()
}
// GetItem Get data in context.
func (ctx *HttpContext) GetItem(key string) interface{} {
ctx.storeMutex.RLock()
v := ctx.store[key]
ctx.storeMutex.RUnlock()
return v
}
// Get JWT UserInfo
func (ctx *HttpContext) GetUser() map[string]interface{} {
v := ctx.GetItem("userinfo")
if v != nil {
return v.(map[string]interface{})
}
return nil
}
// Bind BootStrap Binding
func (ctx *HttpContext) Bind(i interface{}) (err error) {
req := ctx.Input.Request
contentType := req.Header.Get(HeaderContentType)
if req.Body == nil {
err = errors.New("request body can't be empty")
return err
}
bind := binding.Default(req.Method, contentType)
err = bind.Bind(req, i)
return err
}
//BindWithUri is a special bind
func (ctx *HttpContext) BindWithUri(i interface{}) (err error) {
err = binding.Uri.BindUri(ctx.Input.QueryStrings(), i)
return err
}
func (ctx *HttpContext) BindWithRouteData(i interface{}) (err error) {
err = binding.Path.BindUri(ctx.Input.RouterData, i)
return err
}
// BindWith Use Binding By Name
func (ctx *HttpContext) BindWith(i interface{}, bindEnum binding.Binding) (err error) {
req := ctx.Input.Request
switch bindEnum.Name() {
case binding.JSON.Name():
err = binding.JSON.Bind(req, i)
case binding.XML.Name():
err = binding.XML.Bind(req, i)
case binding.Query.Name():
err = binding.Query.Bind(req, i)
case binding.YAML.Name():
err = binding.YAML.Bind(req, i)
case binding.FormMultipart.Name():
err = binding.FormMultipart.Bind(req, i)
case binding.ProtoBuf.Name():
err = binding.ProtoBuf.Bind(req, i)
case binding.MsgPack.Name():
err = binding.MsgPack.Bind(req, i)
case binding.Header.Name():
err = binding.Header.Bind(req, i)
default: // case MIMEPOSTForm:
return binding.Form.Bind(req, i)
}
return err
}
// Query2Number Query String to number with default value
func Query2Number[N cast.Number](ctx *HttpContext, key string, defaultVal string) N {
str := ctx.Input.QueryDefault(key, defaultVal)
num, _ := cast.Str2Number[N](str)
return num
}
// Redirect redirects the request
func (ctx *HttpContext) Redirect(code int, url string) {
http.Redirect(ctx.Output.GetWriter(), ctx.Input.GetReader(), url, code)
}
// Render actionresult writes the response headers and calls render.actionresult to render data.
func (ctx *HttpContext) Render(code int, r actionresult.IActionResult) {
if !bodyAllowedForStatus(code) {
r.WriteContentType(ctx.Output.GetWriter())
ctx.Output.SetStatusCodeNow()
return
}
if err := r.Render(ctx.Output.GetWriter()); err != nil {
panic(err)
}
ctx.Output.SetStatusCode(code)
}
func (ctx *HttpContext) GetSession() *Session {
sessionId := ctx.GetItem("sessionId").(string)
if sessionId == "" {
return nil
}
mgr := ctx.GetItem("sessionMgr").(ISessionManager)
return NewSession(sessionId, mgr)
}