-
Notifications
You must be signed in to change notification settings - Fork 2
/
ext.go
41 lines (34 loc) · 855 Bytes
/
ext.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
package zebra
import (
"net/http"
"strings"
)
type mount struct {
prefix string
middleware Middleware
}
// Mount 函数用于将一个 Middleware 挂载到一个路径前缀上。
// 该 Middleware 只会在指定 prefix 的URL子段下执行。
func Mount(prefix string, m Middleware) Middleware {
if "/" == prefix {
return m
}
return &mount{prefix, m}
}
func (m *mount) Excute(cxt *Context) bool {
// TODO 这个判断不对
if has := strings.HasPrefix(cxt.Urlpath, m.prefix); has {
return m.middleware.Excute(cxt)
}
return true
}
type middlewareWrapper struct {
handler http.Handler
}
func Wrap(handler http.Handler) Middleware {
return &middlewareWrapper{handler}
}
func (wrapper *middlewareWrapper) Excute(cxt *Context) bool {
wrapper.handler.ServeHTTP(cxt.OriginalResponseWriter(), cxt.OriginalRequest())
return true
}