-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathzin.go
165 lines (131 loc) · 4.24 KB
/
zin.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
/* (C)2023 Rayark Inc. - All Rights Reserved
* Rayark Confidential
*
* NOTICE: The intellectual and technical concepts contained herein are
* proprietary to or under control of Rayark Inc. and its affiliates.
* The information herein may be covered by patents, patents in process,
* and are protected by trade secret or copyright law.
* You may not disseminate this information or reproduce this material
* unless otherwise prior agreed by Rayark Inc. in writing.
*/
package zin
import (
"net/http"
"path"
"sync"
"github.com/julienschmidt/httprouter"
"github.com/rayark/zin/v2/middleware"
)
type StdMiddlware func(http.Handler) http.Handler
type StdFuncMiddlware func(http.HandlerFunc) http.HandlerFunc
type Middleware func(httprouter.Handle) httprouter.Handle
func WrapM(sm StdFuncMiddlware) Middleware {
return func(h httprouter.Handle) httprouter.Handle {
var params httprouter.Params
stdh := func(w http.ResponseWriter, r *http.Request) {
h(w, r, params)
}
stdh2 := sm(stdh)
h2 := func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
params = p
stdh2(w, r)
}
return h2
}
}
func WrapS(sm StdMiddlware) Middleware {
return func(h httprouter.Handle) httprouter.Handle {
var params httprouter.Params
stdh := func(w http.ResponseWriter, r *http.Request) {
h(w, r, params)
}
stdh2 := sm(http.HandlerFunc(stdh))
h2 := func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
params = p
stdh2.ServeHTTP(w, r)
}
return h2
}
}
func WrapF(f http.HandlerFunc) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
f(w, r)
}
}
func WrapH(h http.Handler) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
h.ServeHTTP(w, r)
}
}
type MuxGroup struct {
basePath string
middlewares []Middleware
}
func NewGroup(basePath string, middlewares ...Middleware) *MuxGroup {
return &MuxGroup{
basePath: basePath,
middlewares: middlewares,
}
}
// Deprecated. Use Group or Pack.
// https://stackoverflow.com/questions/53572736/append-to-a-new-slice-affect-original-slice
func (g *MuxGroup) Use(middlewares ...Middleware) {
g.middlewares = append(g.middlewares, middlewares...)
}
type RegisterFunc func(path string, handle httprouter.Handle)
func (g *MuxGroup) R(r RegisterFunc, p string, handle httprouter.Handle) {
route := g.Path(p)
m := safeAppend(g.middlewares, middleware.AddRouteToContext(route))
r(route, makePooledHandle(m, handle))
}
func (g *MuxGroup) Path(p string) string {
return pathJoin(g.basePath, p)
}
func (g *MuxGroup) NotFound(h http.Handler) http.Handler {
handle := makePooledHandle(g.middlewares, WrapH(h))
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
handle(w, r, nil)
})
}
func makePooledHandle(middlewares []Middleware, handle httprouter.Handle) httprouter.Handle {
pool := sync.Pool{}
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
obj := pool.Get()
var h httprouter.Handle
if obj != nil {
h = obj.(httprouter.Handle)
} else {
h = makeHandle(middlewares, handle)
}
h(w, r, p)
pool.Put(h)
}
}
func makeHandle(middlewares []Middleware, handle httprouter.Handle) httprouter.Handle {
var h = handle
for _, m := range middlewares {
h = m(h)
}
return h
}
func pathJoin(base string, r string) string {
path := path.Join(base, r)
if len(r) > 0 && r[len(r)-1] == '/' && path[len(r)-1] != '/' {
path = path + "/"
}
return path
}
// Group returns new MuxGroup with appending inputs middlewares to the end of current muxgroup's middleware
func (g *MuxGroup) Group(path string, middlewares ...Middleware) *MuxGroup {
return NewGroup(pathJoin(g.basePath, path), safeAppend(g.middlewares, middlewares...)...)
}
// Pack returns new MuxGroup with appending current muxgroup's middlewares to the end of input middlewarse
func (g *MuxGroup) Pack(path string, middlewares ...Middleware) *MuxGroup {
return NewGroup(pathJoin(g.basePath, path), safeAppend(middlewares, g.middlewares...)...)
}
func safeAppend(middlewaresA []Middleware, middlewaresB ...Middleware) []Middleware {
mws := make([]Middleware, 0, len(middlewaresA)+len(middlewaresB))
mws = append(mws, middlewaresA...)
mws = append(mws, middlewaresB...)
return mws
}