forked from zhyueh/figo
/
controller.go
161 lines (134 loc) · 3.1 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
package figo
import (
"fmt"
"github.com/zhyueh/figo/cache"
"github.com/zhyueh/figo/log"
"github.com/zhyueh/figo/orm"
"github.com/zhyueh/figo/toolkit"
"golang.org/x/net/websocket"
"net/http"
"reflect"
"strings"
)
const (
HttpMode = 0
WebsocketMode = 1
)
const (
SingleRouterMode = 0
AutoRouterMode = 1
)
type ControllerInterface interface {
//phase II
Init(w http.ResponseWriter, r *http.Request)
Preload() error
GetConnectMode() int8
Get()
Post()
Flush()
SetLogger(*log.DataLogger)
SetCache(*cache.Cache)
SetOrm(*orm.Orm)
SetWebsocketConnection(*websocket.Conn)
GetControllerName() string
//phase II
GetRouteMode() int8
//Route()
HandleRequestPathNotFound()
HandleRequestParseError(error)
}
type Controller struct {
Req *Requester
Resp *Response
ControllerName string
Logger *log.DataLogger
Cache *cache.Cache
Orm *orm.Orm
WebsocketConn *websocket.Conn
}
func NewController() *Controller {
re := &Controller{}
re.ControllerName = ""
return re
}
func (this *Controller) Preload() error {
return nil
}
func (this *Controller) GetConnectMode() int8 {
return HttpMode
}
func (this *Controller) SetWebsocketConnection(conn *websocket.Conn) {
this.WebsocketConn = conn
}
func (this *Controller) SetCache(cache *cache.Cache) {
this.Cache = cache
}
func (this *Controller) SetLogger(logger *log.DataLogger) {
this.Logger = logger
}
func (this *Controller) SetOrm(orm *orm.Orm) {
this.Orm = orm
}
func (this *Controller) GetControllerName() string {
return this.ControllerName
}
func (this *Controller) Init(w http.ResponseWriter, r *http.Request) {
this.Req = NewRequester(r)
this.Resp = NewResponse(w)
}
func (this *Controller) Flush() {
this.Resp.Flush()
}
func (this *Controller) Get() {
fmt.Println("not implement get")
}
func (this *Controller) Post() {
}
func (this *Controller) GetRouteMode() int8 {
//default single router mode
return SingleRouterMode
}
func ControllerHandleFunc(this ControllerInterface, httpMethod, path string) {
if this.GetRouteMode() == SingleRouterMode {
switch httpMethod {
case "POST":
this.Post()
default:
this.Get()
}
} else {
autoRouteFunc(this, httpMethod, path)
}
}
func autoRouteFunc(this ControllerInterface, httpMethod, path string) {
comps := toolkit.SplitString(path, "/")
if len(comps) < 2 {
this.HandleRequestPathNotFound()
return
}
var methodName string
for _, v := range comps[1:] {
subComps := strings.Split(v, "-")
for _, vv := range subComps {
methodName += strings.Title(vv)
}
}
if len(methodName) == 0 {
this.HandleRequestPathNotFound()
return
}
methodName = strings.Title(strings.ToLower(httpMethod)) + methodName
value := reflect.ValueOf(this)
method := value.MethodByName(methodName)
if !method.IsValid() {
this.HandleRequestPathNotFound()
return
}
method.Call(nil)
}
func (this *Controller) HandleRequestParseError(err error) {
this.Resp.WriteString(fmt.Sprintf("parse err %v", err))
}
func (this *Controller) HandleRequestPathNotFound() {
this.Resp.WriteString(fmt.Sprintf("path not found %s", this.Req.BaseHttpRequest.URL.Path))
}