-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.go
307 lines (271 loc) · 7.65 KB
/
controller.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package fastgo
import (
"github.com/valyala/fasthttp"
"strconv"
"encoding/json"
"html/template"
"mime/multipart"
"path"
"strings"
"bytes"
"github.com/phachon/fasthttpsession"
"time"
)
type ControllerInterface interface {
Init(ctx *fasthttp.RequestCtx, controllerName string, actionName string)
Before()
After()
}
func NewController() *Controller {
return &Controller{}
}
type Controller struct {
Ctx *fasthttp.RequestCtx
ControllerName string
ActionName string
Data map[string]interface{}
Session fasthttpsession.SessionStore
}
// init controller
func (controller *Controller) Init(ctx *fasthttp.RequestCtx, controllerName string, actionName string) {
controller.Ctx = ctx
controller.ControllerName = controllerName
controller.ActionName = actionName
controller.Data = map[string]interface{}{}
var err error
controller.Session, err = Session.Start(ctx)
if err != nil {
panic("session start error, "+err.Error())
}
}
// controller before
func (controller *Controller) Before() {
//todo controller before
}
// controller after
func (controller *Controller) After() {
// todo controller after
err := controller.Session.Save(controller.Ctx)
if err != nil {
panic("session save error, "+err.Error())
}
}
// render view response
func (controller *Controller) Render(tpl string) {
controller.Ctx.SetContentType("text/html;charset=utf-8")
t, err := template.ParseFiles(ViewPath +"/"+ tpl + TemplateSuffix)
if err != nil {
Log.Errorf("template %s parese error %s", ViewPath+"/"+tpl, err.Error())
controller.Ctx.SetBodyString("template parese error, "+err.Error())
return
}
t.Execute(controller.Ctx.Response.BodyWriter(), controller.Data)
}
// layout render view response
func (controller *Controller) LayoutRender(layout string, tpl string) {
controller.Ctx.SetContentType("text/html;charset=utf-8")
var buf bytes.Buffer
t, err := template.ParseFiles(ViewPath +"/"+ tpl + TemplateSuffix)
if err != nil {
Log.Errorf("template %s parese error %s", ViewPath+"/"+tpl, err.Error())
controller.Ctx.SetBodyString("template parese error, "+err.Error())
return
}
t.Execute(&buf, controller.Data)
controller.Data["LayoutContent"] = template.HTML(buf.String())
t, err = template.ParseFiles(ViewPath +"/"+ layout + TemplateSuffix)
if err != nil {
Log.Errorf("template %s parese error %s", ViewPath+"/"+layout, err.Error())
controller.Ctx.SetBodyString("template parese error, " +err.Error())
return
}
t.Execute(controller.Ctx.Response.BodyWriter(), controller.Data)
}
// return json
func (controller *Controller) ReturnJson(body interface{}) {
controller.Ctx.SetContentType("application/json;charset=utf-8")
jsonByte, err := json.Marshal(body)
if err != nil {
controller.Ctx.SetBodyString(err.Error())
} else {
controller.Ctx.SetBody(jsonByte)
}
}
// return error
func (controller *Controller) ReturnError(status int, message string) {
controller.Ctx.SetStatusCode(status)
controller.Ctx.SetBodyString(message)
}
// get request ctx string
func (controller *Controller) GetString(key string, def string) string {
if string(controller.Ctx.FormValue(key)) == "" {
return def
}else {
return string(controller.Ctx.FormValue(key))
}
}
// get request ctx bool
func (controller *Controller) GetBool(key string) bool {
str := string(controller.Ctx.FormValue(key))
if str == "1" {
return true
}
return false
}
// get request ctx int
func (controller *Controller) GetInt(key string, def int) (int, error) {
str := string(controller.Ctx.FormValue(key))
if str == "" {
return def, nil
}
i, err := strconv.Atoi(str)
return i, err
}
// get request ctx int8
func (controller *Controller) GetInt8(key string, def int8) (int8, error) {
str := string(controller.Ctx.FormValue(key))
if str == "" {
return def, nil
}
i64, err := strconv.ParseInt(str, 10, 8)
return int8(i64), err
}
// get request ctx uint8
func (controller *Controller) GetUInt8(key string, def uint8) (uint8, error) {
str := string(controller.Ctx.FormValue(key))
if str == "" {
return def, nil
}
i64, err := strconv.ParseInt(str, 10, 8)
return uint8(i64), err
}
// get request ctx int16
func (controller *Controller) GetInt16(key string, def int16) (int16, error) {
str := string(controller.Ctx.FormValue(key))
if str == "" {
return def, nil
}
i64, err := strconv.ParseInt(str, 10, 8)
return int16(i64), err
}
// get request ctx uint16
func (controller *Controller) GetUInt16(key string, def uint16) (uint16, error) {
str := string(controller.Ctx.FormValue(key))
if str == "" {
return def, nil
}
i64, err := strconv.ParseInt(str, 10, 8)
return uint16(i64), err
}
// get request ctx int32
func (controller *Controller) GetInt32(key string, def int32) (int32, error) {
str := string(controller.Ctx.FormValue(key))
if str == "" {
return def, nil
}
i64, err := strconv.ParseInt(str, 10, 8)
return int32(i64), err
}
// get request ctx uint32
func (controller *Controller) GetUInt32(key string, def uint32) (uint32, error) {
str := string(controller.Ctx.FormValue(key))
if str == "" {
return def, nil
}
i64, err := strconv.ParseInt(str, 10, 8)
return uint32(i64), err
}
// get request ctx int64
func (controller *Controller) GetInt64(key string, def int64) (int64, error) {
str := string(controller.Ctx.FormValue(key))
if str == "" {
return def, nil
}
i64, err := strconv.ParseInt(str, 10, 8)
return i64, err
}
// get request ctx uint64
func (controller *Controller) GetUInt64(key string, def uint64) (uint64, error) {
str := string(controller.Ctx.FormValue(key))
if str == "" {
return def, nil
}
i64, err := strconv.ParseInt(str, 10, 8)
return uint64(i64), err
}
// get request content text float64
func (controller *Controller) GetCtxFloat64(key string, def float64) (float64, error) {
str := string(controller.Ctx.FormValue(key))
i, err := strconv.Atoi(str)
return float64(i), err
}
// get form file by key
func (controller *Controller) GetFile(key string) (*multipart.FileHeader, error) {
return controller.Ctx.FormFile(key)
}
// request is ajax
func (controller *Controller) IsAjax() bool {
return string(controller.Ctx.Request.Header.Peek("X-Requested-With")) == "XMLHttpRequest"
}
func (controller *Controller) SetCookie(name string, value string, others ...interface{}) {
cookie := fasthttp.AcquireCookie()
defer fasthttp.ReleaseCookie(cookie)
cookie.SetKey(name)
cookie.SetHTTPOnly(true)
// 1. expires
if len(others) > 0 {
var expires int64
switch v := others[0].(type) {
case int:
expires = int64(v)
case int32:
expires = int64(v)
case int64:
expires = v
}
switch {
case expires > 0:
cookie.SetExpire(time.Now().Add(time.Duration(expires) * time.Second))
case expires < 0:
cookie.SetExpire(fasthttp.CookieExpireUnlimited)
}
}
// 2. path
if len(others) > 1 {
if v, ok := others[1].(string); ok && len(v) > 0 {
cookie.SetPath(v)
}
} else {
cookie.SetPath("/")
}
// 3. domain
if len(others) > 2 {
if v, ok := others[2].(string); ok && len(v) > 0 {
cookie.SetDomain(v)
}
}
// 4. secure
if len(others) > 3 {
if v, ok := others[2].(bool); ok && v && controller.Ctx.IsTLS() {
controller.Ctx.IsTLS()
}
}
cookie.SetValue(value)
controller.Ctx.Response.Header.SetCookie(cookie)
}
func (controller *Controller) GetCookie(key string) []byte {
return controller.Ctx.Request.Header.Cookie(key)
}
func (controller *Controller) GetCookieString(key string) string {
return string(controller.Ctx.Request.Header.Cookie(key))
}
func (controller *Controller) UserAgent() string {
return string(controller.Ctx.Request.Header.UserAgent())
}
// static file
func (controller *Controller) Static() {
baseName := path.Base(StaticPath)
dir := strings.TrimRight(StaticPath, baseName)
fsHandler := fasthttp.FSHandler(dir, 0)
fsHandler(controller.Ctx)
}