-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzebra.go
211 lines (180 loc) · 4.04 KB
/
zebra.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
package zebra
import (
"log"
"net/http"
"os"
"strings"
)
type Zebra struct {
name string
middlewares []Middleware
server *http.Server
logger *log.Logger // 框架自身使用的logger
}
func New() *Zebra {
return NewWithServer(&http.Server{Addr: ":3000"})
}
func NewWithServer(server *http.Server) *Zebra {
return &Zebra{
name: "zebra",
middlewares: make([]Middleware, 0),
server: server,
logger: log.New(os.Stdout, "zebra ", log.LstdFlags|log.Lshortfile),
}
}
func (z *Zebra) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if len(z.middlewares) > 0 {
c := newContext(w, r)
c.zebra = z
var index int = 0
for _, middleware := range z.middlewares {
index++
if !middleware.Excute(c) {
break
}
}
for index--; index >= 0; index-- {
if v, ok := z.middlewares[index].(MiddlewareCallback); ok {
v.Callback(c)
}
}
}
}
func (z *Zebra) Use(m interface{}) {
switch v := m.(type) {
case http.Handler:
z.middlewares = append(z.middlewares, Wrap(v))
case Middleware:
z.middlewares = append(z.middlewares, v)
}
}
func (z *Zebra) Name() string {
return z.name
}
func (z *Zebra) SetName(name string) {
z.name = name
}
func (z *Zebra) SetLogger(logger *log.Logger) {
z.logger = logger
}
func (z *Zebra) UseFullFeatures() {
}
func (z *Zebra) Run() {
z.server.Handler = z
z.server.ListenAndServe()
}
func (z *Zebra) RunOnAddr(addr string) {
z.server.Handler = z
z.server.Addr = addr
z.server.ListenAndServe()
}
// Context 是 Middleware 的上下文对象
type Context struct {
Method int // Http Method
Urlpath string // *http.Request 中的 URL Path
res http.ResponseWriter
req *http.Request
zebra *Zebra
response *responseAdapter
Transfer map[string]interface{}
}
// 创建 Context
func newContext(w http.ResponseWriter, r *http.Request) *Context {
c := Context{
res: w,
req: r,
response: &responseAdapter{
w: w,
wroteHeader: false,
},
Transfer: make(map[string]interface{}),
}
if r != nil {
c.Method = HttpMethodCode(r.Method)
c.Urlpath = r.URL.Path
}
return &c
}
// 获取原始的 http.ResponseWriter
func (c *Context) OriginalResponseWriter() http.ResponseWriter {
return c.res
}
func (c *Context) ResponseWriter() http.ResponseWriter {
return c.response
}
// 获取原始的 *http.Request
func (c *Context) OriginalRequest() *http.Request {
return c.req
}
// TODO
func (c *Context) Request() *http.Request {
return c.req
}
func (c *Context) Zebra() *Zebra {
return c.zebra
}
func (c *Context) Logger() *log.Logger {
return c.zebra.logger
}
/*
func (c *Context) Put(key string, val interface{}) {
c.transfer[key] = val
}
func (c *Context) Get(key string) interface{} {
return c.transfer[key]
}*/
// zebra中的一切组件均为一个 Middleware.
type Middleware interface {
// Excute 用来执行当前 Middleware,
// 返回 true 表示继续执行后续 Middleware,
// 返回 false 阻止后续 Middleware 的执行。
Excute(*Context) bool
}
type MiddlewareCallback interface {
Middleware
Callback(*Context)
}
// 一组表示Http Method的常量。其中Any表示任意一种Http Method.
const (
MethodAny int = iota
MethodGet
MethodPost
MethodPut
MethodDelete
MethodHead
MethodOptions
)
var httpMethodCode = map[string]int{
"GET": MethodGet,
"POST": MethodPost,
"PUT": MethodPut,
"OPTIONS": MethodOptions,
"DELETE": MethodDelete,
"HEAD": MethodHead,
}
// 通过请求的 HTTP Method 获取对应的 code.
func HttpMethodCode(method string) int {
return httpMethodCode[strings.ToUpper(method)]
}
// 适配 server 端的 response, 中间件内部使用
type responseAdapter struct {
status int
wroteHeader bool
w http.ResponseWriter
}
func (r *responseAdapter) Header() http.Header {
return r.w.Header()
}
func (r *responseAdapter) Write(data []byte) (int, error) {
if !r.wroteHeader {
r.WriteHeader(http.StatusOK)
}
return r.w.Write(data)
}
func (r *responseAdapter) WriteHeader(code int) {
r.status = code
if !r.wroteHeader {
r.w.WriteHeader(code)
r.wroteHeader = true
}
}