-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcontroller.go
More file actions
173 lines (140 loc) · 3.87 KB
/
controller.go
File metadata and controls
173 lines (140 loc) · 3.87 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
// Copyright 2012 by sdm. All rights reserved.
// license that can be found in the LICENSE file.
package wk
import (
"github.com/sdming/kiss/gotype"
"reflect"
"strings"
)
// ActionSubscriber is interface that subscrib controller events
type ActionSubscriber interface {
OnActionExecuting(action *ActionContext)
OnActionExecuted(action *ActionContext)
OnException(action *ActionContext)
}
// ActionContext is data that pass to ActionSubscriber
type ActionContext struct {
// Name is action method name
Name string
Context *HttpContext
Controller reflect.Value
Result HttpResult
Err error
}
// Controller is wrap of controller handler
type Controller struct {
// Handler
Handler reflect.Value
//Methods is cache of method reflect info
Methods map[string]*gotype.MethodInfo
// actionSubscriber
actionSubscriber ActionSubscriber
//server
//server *HttpServer
}
// newController
func newController(x interface{}) (c *Controller, err error) {
handler := reflect.ValueOf(x)
handlerType := handler.Type()
c = &Controller{Handler: handler, Methods: make(map[string]*gotype.MethodInfo)}
c.actionSubscriber, _ = x.(ActionSubscriber)
for i := 0; i < handler.NumMethod(); i++ {
method := handlerType.Method(i)
methodValue := handler.Method(i)
if !methodValue.IsValid() || methodValue.Kind() != reflect.Func || method.Name == "" {
continue
}
// not export
if method.PkgPath != "" {
continue
}
methdoInfo := gotype.GetMethodInfo(method)
c.Methods[strings.ToLower(methdoInfo.Method.Name)] = methdoInfo
}
return c, nil
}
// findAction
func (c *Controller) findAction(ctx *HttpContext) (method *gotype.MethodInfo, err error) {
actionName, ok := ctx.RouteData[_action]
if !ok || actionName == "" {
switch ctx.Method {
case HttpVerbsConnect, HttpVerbsOptions, HttpVerbsTrace:
actionName = "_NotSupport" + ctx.Method
case HttpVerbsGet, HttpVerbsDelete, HttpVerbsPost:
actionName = ctx.Method
case HttpVerbsHead:
actionName = "Get"
}
}
actionName = strings.ToLower(actionName)
if method, ok = c.Methods[actionName]; ok {
return
}
actionName = actionName + strings.ToLower(ctx.Method)
if method, ok = c.Methods[actionName]; ok {
return
}
actionName = _defaultAction
if method, ok = c.Methods[actionName]; ok {
return
}
err = errNoAction
return
}
// Execute call action method
func (c *Controller) Execute(ctx *HttpContext) {
ctx.Result, ctx.Error = c.invoke(ctx)
}
// invoke
func (c *Controller) invoke(ctx *HttpContext) (result HttpResult, err error) {
method, err := c.findAction(ctx)
if err != nil {
return nil, err
}
if LogLevel >= LogDebug {
Logger.Println("controller dispatch request to action", method.Method.Name)
}
var actionContext *ActionContext
actionContext = &ActionContext{
Controller: c.Handler,
Name: method.Method.Name,
Context: ctx,
Err: nil,
}
//c.server.Fire(_route, _eventStartAction, c, actionContext, ctx)
ctx.Server.Fire(_route, _eventStartAction, c, actionContext, ctx)
if c.actionSubscriber != nil {
c.actionSubscriber.OnActionExecuting(actionContext)
}
//var handle func(*HttpContext) (HttpResult, error)
in := make([]reflect.Value, 2)
var out []reflect.Value
in[0] = c.Handler
in[1] = reflect.ValueOf(ctx)
out, err = safeCall(method.Func, in)
if err == nil {
if out[0].IsNil() {
result = nil
} else {
result = out[0].Interface().(HttpResult)
}
if !out[1].IsNil() {
err = out[1].Interface().(error)
} else {
err = nil
}
} else {
result = nil
}
actionContext.Err = err
actionContext.Result = result
if err != nil && c.actionSubscriber != nil {
c.actionSubscriber.OnException(actionContext)
}
if c.actionSubscriber != nil {
c.actionSubscriber.OnActionExecuted(actionContext)
}
//c.server.Fire(_route, _eventEndAction, c, actionContext, ctx)
ctx.Server.Fire(_route, _eventEndAction, c, actionContext, ctx)
return result, err
}