-
Notifications
You must be signed in to change notification settings - Fork 14
/
controller.go
161 lines (131 loc) · 3.14 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 pairec
import (
"fmt"
"io"
"net/http"
"runtime/debug"
"strings"
"github.com/alibaba/pairec/v2/log"
)
type ControllerInterface interface {
Process(http.ResponseWriter, *http.Request)
}
type ControllerRegister struct {
routeInfos map[string]*RouteInfo
Middlewares []MiddlewareFunc
}
func NewControllerRegister() *ControllerRegister {
cr := &ControllerRegister{
routeInfos: make(map[string]*RouteInfo),
}
return cr
}
func (c *ControllerRegister) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
defer func() {
if err := recover(); err != nil {
stack := string(debug.Stack())
log.Error(fmt.Sprintf("error=%v, stack=%s", err, strings.ReplaceAll(stack, "\n", "\t")))
Error(rw, 500, "server error")
}
}()
rw = NewResponseWriter(rw)
uri := req.RequestURI
if index := strings.Index(req.RequestURI, "?"); index != -1 {
uri = req.RequestURI[:index]
}
if info, exist := c.routeInfos[uri]; exist {
if info.initialize != nil {
c := info.initialize()
c.Process(rw, req)
} else if info.hf != nil {
info.hf(rw, req)
} else {
// return 404
Error(rw, 404, "controller not found")
return
}
} else {
// return 404
Error(rw, 404, "url not found route info")
return
}
}
func (c *ControllerRegister) Register(routeInfo *RouteInfo) {
c.routeInfos[routeInfo.pattern] = routeInfo
}
func (c *ControllerRegister) GetRoutePath() (paths []string) {
for p := range c.routeInfos {
paths = append(paths, p)
}
return
}
func (c *ControllerRegister) ApplyMiddlewares() {
for p, r := range c.routeInfos {
c.routeInfos[p] = applyMiddleware(r, c.Middlewares...)
}
}
func Error(rw http.ResponseWriter, code int, msg string) {
rw.WriteHeader(code)
io.WriteString(rw, msg)
}
type MiddlewareController struct {
ControllerInterface
Middlewares []MiddlewareFunc
}
func (c MiddlewareController) Process(resp http.ResponseWriter, req *http.Request) {
var hf handleFunc
for i := len(c.Middlewares) - 1; i >= 0; i-- {
if hf == nil {
hf = c.Middlewares[i](c.ControllerInterface.Process)
} else {
hf = c.Middlewares[i](hf)
}
}
hf(resp, req)
}
func applyMiddleware(info *RouteInfo, middleware ...MiddlewareFunc) *RouteInfo {
if len(middleware) == 0 {
return info
}
if info.initialize != nil {
initialize := info.initialize
info.initialize = func() ControllerInterface {
return &MiddlewareController{
ControllerInterface: initialize(),
Middlewares: middleware,
}
}
}
if info.hf != nil {
for i := len(middleware) - 1; i >= 0; i-- {
info.hf = middleware[i](info.hf)
}
}
return info
}
type ResponseWriter struct {
http.ResponseWriter
statusCode int
size int
}
func NewResponseWriter(w http.ResponseWriter) *ResponseWriter {
return &ResponseWriter{
ResponseWriter: w,
statusCode: http.StatusOK,
}
}
func (w *ResponseWriter) WriteHeader(statusCode int) {
w.statusCode = statusCode
w.ResponseWriter.WriteHeader(statusCode)
}
func (w *ResponseWriter) Write(b []byte) (n int, err error) {
n, err = w.ResponseWriter.Write(b)
w.size += n
return
}
func (w *ResponseWriter) StatusCode() int {
return w.statusCode
}
func (w *ResponseWriter) Size() int {
return w.size
}