-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
executable file
·565 lines (487 loc) · 15.2 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
package gweb
import (
"errors"
"fmt"
"github.com/gin-gonic/gin/binding"
"github.com/gorilla/mux"
"github.com/nbvghost/glog"
"github.com/nbvghost/gweb/cache"
"github.com/nbvghost/gweb/conf"
"github.com/nbvghost/tool/encryption"
"reflect"
"runtime/debug"
"time"
"net/http"
"regexp"
"strings"
)
var removeSeparatorRegexp = regexp.MustCompile("/+")
func fixPath(path string) string {
return removeSeparatorRegexp.ReplaceAllString(path, "/")
}
type handler struct {
call func(context *Context) (Result, error)
}
func (h *handler) Handle(context *Context) (Result, error) {
return h.call(context)
}
type Context struct {
Response http.ResponseWriter //
Request *http.Request //
Session *Session //
PathParams map[string]string //
RoutePath string //route 的路径,相当于router根目录,请求request path remove restful path
Function *Function //
}
func (c *Context) Clone() Context {
return Context{
Response: c.Response,
Request: c.Request,
Session: c.Session,
PathParams: c.PathParams,
RoutePath: c.RoutePath,
//Data: c.Data,
}
}
type IController interface {
DefaultHandle(call IHandler) IController
NotFoundHandler(call IHandler) IController
AddHandler(routePath string, call IHandler) IController
AddInterceptor(value Interceptor) IController
NewController(actionName string) IController
}
/*
MethodGet = "GET"
MethodHead = "HEAD"
MethodPost = "POST"
MethodPut = "PUT"
MethodPatch = "PATCH" // RFC 5789
MethodDelete = "DELETE"
MethodConnect = "CONNECT"
MethodOptions = "OPTIONS"
MethodTrace = "TRACE"
*/
type IHandlerGet interface {
IHandler
HandleGet(context *Context) (Result, error)
}
type IHandlerPost interface {
IHandler
HandlePost(context *Context) (Result, error)
}
type IHandlerHead interface {
IHandler
HandleHead(context *Context) (Result, error)
}
type IHandlerPut interface {
IHandler
HandlePut(context *Context) (Result, error)
}
type IHandlerPatch interface {
IHandler
HandlePatch(context *Context) (Result, error)
}
type IHandlerDelete interface {
IHandler
HandleDelete(context *Context) (Result, error)
}
type IHandlerConnect interface {
IHandler
HandleConnect(context *Context) (Result, error)
}
type IHandlerOptions interface {
IHandler
HandleOptions(context *Context) (Result, error)
}
type IHandlerTrace interface {
IHandler
HandleTrace(context *Context) (Result, error)
}
type IHandler interface {
Handle(context *Context) (Result, error)
}
type NotFoundHandler struct {
function *Function
}
func (h *NotFoundHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
if h.function.controller.RoutePath != "/" {
h.function.RoutePath = "/" + strings.ReplaceAll(request.URL.Path, h.function.controller.RoutePath, "")
}
h.function.ServeHTTP(writer, request)
}
type Function struct {
RoutePath string
Handler IHandler
controller *Controller
}
func (function *Function) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var startTime = time.Now().UnixNano()
defer func() {
if r := recover(); r != nil {
glog.Trace(r)
debug.PrintStack()
}
//context.Request.Method, context.Request.URL
glog.Debug(fmt.Sprintf("%10v\t%10v\t%10v", r.Method, fmt.Sprintf("%vms", float64(time.Now().UnixNano()-startTime)/float64(time.Millisecond)), r.URL))
}()
var session *Session
cookie, err := r.Cookie("GLSESSIONID")
var GLSESSIONID string
if err != nil || strings.EqualFold(cookie.Value, "") {
GLSESSIONID = encryption.CipherEncrypter(encryption.NewSecretKey(conf.Config.SecureKey), fmt.Sprintf("%s", time.Now().Format("2006-01-02 15:04:05")))
http.SetCookie(w, &http.Cookie{Name: "GLSESSIONID", Value: GLSESSIONID, Path: "/", MaxAge: conf.Config.SessionExpires})
session = &Session{Attributes: &Attributes{}, CreateTime: time.Now().Unix(), LastOperationTime: time.Now().Unix(), Token: GLSESSIONID}
Sessions.AddSession(GLSESSIONID, session)
} else {
session = Sessions.GetSession(cookie.Value)
if session == nil {
session = &Session{Attributes: &Attributes{}, CreateTime: time.Now().Unix(), LastOperationTime: time.Now().Unix(), Token: cookie.Value}
Sessions.AddSession(cookie.Value, session)
}
session.LastOperationTime = time.Now().Unix()
}
session.LastRequestURL = r.URL
//c.Lock()
w.Header().Add("Server-Name", conf.Config.Name)
w.Header().Add("Server-Ver", conf.Config.Ver)
//c.Unlock()
pathParams := mux.Vars(r)
var context = &Context{Response: w, Request: r, Session: session, PathParams: pathParams, Function: function}
//context.RootPath=
if function.controller != nil {
if function.controller.Router == nil {
context.RoutePath = function.controller.RoutePath
} else {
//todo:这个地方修改没有测试,原逻辑会导致路由被修改而异常
context.RoutePath = function.controller.RoutePath
varsRegexp := regexp.MustCompile("\\{(.*?)+\\}")
if varsRegexp.MatchString(context.RoutePath) {
context.RoutePath = varsRegexp.ReplaceAllStringFunc(context.RoutePath, func(s string) string {
ss := s[1 : len(s)-1]
return pathParams[ss]
})
}
}
interceptor := function.controller.Interceptors
if interceptor == nil {
result, err := function.controller.doAction(context, function)
if err != nil {
result = NewErrorResult(err)
}
result.Apply(context)
} else {
isContinue, beforeResult := interceptor.ActionBefore(context)
if isContinue == false {
if beforeResult != nil {
beforeResult.Apply(context)
}
return
}
serviceConfig := interceptor.ActionService(context)
if serviceConfig.CacheConfig.EnableHTMLCache {
var fullPath = context.Request.URL.Path
if strings.EqualFold(context.Request.URL.RawQuery, "") == false {
fullPath = fullPath + "?" + context.Request.URL.RawQuery
}
fullPathMd5 := encryption.Md5ByString(fullPath)
cacheItem, err := cache.Read(fmt.Sprintf("cache/%v/%v", serviceConfig.CacheConfig.PrefixName, fullPathMd5))
if err == nil {
context.Response.Header().Set("Content-Type", "text/html; charset=utf-8")
context.Response.Write(cacheItem.Byte)
return
}
}
result, err := function.controller.doAction(context, function)
if err != nil {
result = NewErrorResult(err)
result.Apply(context)
return
}
interceptorResult := interceptor.ActionAfter(context, result)
if interceptorResult == nil {
interceptorResult = result
}
if serviceConfig.CacheConfig.EnableHTMLCache {
if htmlResult, ok := interceptorResult.(*HTMLResult); ok {
interceptorResult = &cacheHTMLResult{
HTMLResult: htmlResult,
ServiceName: serviceConfig.CacheConfig.PrefixName,
}
}
}
interceptorResult.Apply(context)
}
} else {
result, err := (&Controller{}).doAction(context, function)
if err != nil {
result = NewErrorResult(err)
}
result.Apply(context)
}
}
func NewFunction(RoutePath string, call IHandler) *Function {
function := &Function{}
function.RoutePath = RoutePath
function.Handler = call
return function
}
var AppRouter = mux.NewRouter()
var _ IController = &Controller{}
type Controller struct {
RoutePath string //定义路由的路径
Interceptors *Interceptors //
ParentController *Controller //
Router *mux.Router //dir
ViewSubDir string //
}
func (c *Controller) AddInterceptor(value Interceptor) IController {
c.Interceptors.AddInterceptor(value)
return c
}
func (c *Controller) DefaultHandle(call IHandler) IController {
c.Router.NotFoundHandler = &NotFoundHandler{function: &Function{
Handler: call,
controller: c,
}}
return c
}
func (c *Controller) NotFoundHandler(call IHandler) IController {
c.Router.NotFoundHandler = &NotFoundHandler{function: &Function{
Handler: call,
controller: c,
}}
return c
}
func (c *Controller) NewController(actionName string) IController {
path := fmt.Sprintf("/%s/", strings.Trim(actionName, "/"))
/* h := c.Router.HandleFunc("/"+strings.Trim(actionName, "/"), func(writer http.ResponseWriter, request *http.Request) {
//c := reflect.ValueOf(controller).Elem().FieldByName("Controller")
function := &Function{
RoutePath: "*",
Handler: &handler{call: controller.DefaultHandle},
controller: c.Addr().Interface().(*Controller),
}
function.ServeHTTP(writer, request)
})
glog.Panic(h.GetError())*/
route := c.Router.PathPrefix(path)
glog.Panic(route.GetError())
router := route.Subrouter()
controller := &Controller{
RoutePath: path,
Interceptors: &Interceptors{},
ParentController: c,
Router: router,
ViewSubDir: c.ViewSubDir,
}
return controller
/*v := reflect.ValueOf(controller)
RoutePathValue := v.Elem().FieldByName("RoutePath")
RoutePathValue.SetString(c.RoutePath + strings.Trim(path, "/") + "/")
RouteValue := v.Elem().FieldByName("Router")
RouteValue.Set(reflect.ValueOf(router))
ParentControllerValue := v.Elem().FieldByName("ParentController")
ParentControllerValue.Set(reflect.ValueOf(c))
ViewSubDirValue := v.Elem().FieldByName("ViewSubDir")
ViewSubDirValue.Set(reflect.ValueOf(c.ViewSubDir))
return controller*/
}
func (c *Controller) AddHandler(routePath string, call IHandler) IController {
function := NewFunction(routePath, call)
if strings.EqualFold(function.RoutePath, "") {
panic(errors.New("不允许有空的路由"))
return c
}
function.controller = c
var methods []string
if _, ok := call.(IHandlerGet); ok {
methods = append(methods, http.MethodGet)
}
if _, ok := call.(IHandlerPost); ok {
methods = append(methods, http.MethodPost)
}
if _, ok := call.(IHandlerHead); ok {
methods = append(methods, http.MethodHead)
}
if _, ok := call.(IHandlerPut); ok {
methods = append(methods, http.MethodPut)
}
if _, ok := call.(IHandlerPatch); ok {
methods = append(methods, http.MethodPatch)
}
if _, ok := call.(IHandlerDelete); ok {
methods = append(methods, http.MethodDelete)
}
if _, ok := call.(IHandlerConnect); ok {
methods = append(methods, http.MethodConnect)
}
if _, ok := call.(IHandlerOptions); ok {
methods = append(methods, http.MethodOptions)
}
if _, ok := call.(IHandlerTrace); ok {
methods = append(methods, http.MethodTrace)
}
if len(methods) == 0 {
methods = append(methods,
http.MethodGet,
http.MethodHead,
http.MethodPost,
http.MethodPut,
http.MethodPatch,
http.MethodDelete,
http.MethodConnect,
http.MethodOptions,
http.MethodTrace,
)
}
h := c.Router.Handle("/"+strings.Trim(function.RoutePath, "/"), function).Methods(methods...)
glog.Panic(h.GetError())
return c
}
func (c *Controller) AddStaticHandler(function *Function) {
if strings.EqualFold(function.RoutePath, "") {
panic(errors.New("不允许有空的路由"))
return
}
function.controller = c
p := c.Router.PathPrefix("/" + strings.Trim(function.RoutePath, "/") + "/")
glog.Panic(p.GetError())
h := p.Handler(function)
glog.Panic(h.GetError())
h.Methods(http.MethodGet)
}
func (c *Controller) doAction(context *Context, f *Function) (Result, error) {
var result Result
var err error
if f == nil {
result = &ViewActionMappingResult{}
} else {
ht := reflect.TypeOf(f.Handler).Elem()
numField := ht.NumField()
var paramFieldName string
if v, ok := ht.FieldByName(context.Request.Method); ok {
paramFieldName = v.Name
} else {
for i := 0; i < numField; i++ {
if v, ok := ht.Field(i).Tag.Lookup("param"); ok {
if strings.EqualFold(context.Request.Method, v) {
paramFieldName = ht.Field(i).Name
break
}
}
}
}
if len(paramFieldName) > 0 {
paramV := reflect.ValueOf(f.Handler).Elem().FieldByName(paramFieldName)
if paramV.Kind() != reflect.Ptr {
paramV = paramV.Addr()
}
if len(context.Request.URL.Query()) > 0 {
if err = binding.Query.Bind(context.Request, paramV.Interface()); err != nil {
return nil, err
}
}
b := binding.Default(context.Request.Method, context.Request.Header.Get("Content-Type"))
if err = b.Bind(context.Request, paramV.Interface()); err != nil {
return nil, err
}
}
switch context.Request.Method {
case http.MethodGet:
if handler, ok := f.Handler.(IHandlerGet); ok {
result, err = handler.HandleGet(context)
}
case http.MethodHead:
if handler, ok := f.Handler.(IHandlerHead); ok {
result, err = handler.HandleHead(context)
}
case http.MethodPost:
if handler, ok := f.Handler.(IHandlerPost); ok {
result, err = handler.HandlePost(context)
}
case http.MethodPut:
if handler, ok := f.Handler.(IHandlerPut); ok {
result, err = handler.HandlePut(context)
}
case http.MethodPatch:
if handler, ok := f.Handler.(IHandlerPatch); ok {
result, err = handler.HandlePatch(context)
}
case http.MethodDelete:
if handler, ok := f.Handler.(IHandlerDelete); ok {
result, err = handler.HandleDelete(context)
}
case http.MethodConnect:
if handler, ok := f.Handler.(IHandlerConnect); ok {
result, err = handler.HandleConnect(context)
}
case http.MethodOptions:
if handler, ok := f.Handler.(IHandlerOptions); ok {
result, err = handler.HandleOptions(context)
}
case http.MethodTrace:
if handler, ok := f.Handler.(IHandlerTrace); ok {
result, err = handler.HandleTrace(context)
}
default:
result, err = f.Handler.Handle(context)
}
if result == nil {
result, err = f.Handler.Handle(context)
}
if result == nil {
glog.Error(errors.New("Action:" + context.Request.URL.String() + "-> 返回视图类型为空"))
}
}
return result, err
}
// NewController 根目录控制器,非具体的handler
func NewController(rootPath, viewSubDir string) IController {
var path string
trimPath := strings.Trim(rootPath, "/")
if strings.EqualFold(trimPath, "") {
path = "/"
} else {
path = fmt.Sprintf("/%s/", trimPath)
}
route := AppRouter.PathPrefix(path)
glog.Panic(route.GetError())
router := route.Subrouter()
controller := &Controller{
RoutePath: path,
Interceptors: &Interceptors{},
ParentController: nil,
Router: router,
ViewSubDir: strings.Trim(viewSubDir, "/"),
}
return controller
}
func NewStaticController(rootPath, dir string) {
AppRouter.PathPrefix(fmt.Sprintf("/%s/", rootPath)).HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Access-Control-Allow-Origin", "*")
http.StripPrefix(fmt.Sprintf("/%s/", rootPath), http.FileServer(http.Dir(dir))).ServeHTTP(writer, request)
}) //.Handler(http.StripPrefix(fmt.Sprintf("/%s/",rootPath), http.FileServer(http.Dir(dir))))
/*path := "/" + strings.Trim(actionName, "/") + "/"
if strings.EqualFold(actionName, "/") || strings.EqualFold(actionName, "") {
path = "/"
} else {
path = "/" + strings.Trim(actionName, "/") + "/"
}
route := AppRouter.PathPrefix(path)
glog.Panic(route.GetError())
h := route.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
c := reflect.ValueOf(controller).Elem().FieldByName("Controller")
cAddr := c.Addr()
function := &Function{
RoutePath: "*",
Handler: &handler{call: controller.DefaultHandle},
controller: cAddr.Interface().(*Controller),
}
function.ServeHTTP(writer, request)
})
glog.Panic(h.GetError())
v := reflect.ValueOf(controller)
RoutePathValue := v.Elem().FieldByName("RoutePath")
RoutePathValue.SetString(path)*/
return
}