-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.go
141 lines (115 loc) · 3.34 KB
/
path.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
package pochi
import (
"net/http"
"strings"
"sync/atomic"
"github.com/lestrrat-go/pochi/middleware"
)
type PathSpec struct {
pattern string
// list of middlewares that were actually applied to this path
directMiddlewares []Middleware
// list of middlewares that were inherited from the parent path
inheritedMiddlewares []Middleware
// set to true if the object's handler has been "compiled" with all of
// the appropriate middlewares
compiled atomic.Bool
rawHandler http.Handler
handler http.Handler
inheritMiddlewares bool
}
// Path creates a new PathSpec object with the given pattern.
func Path(pattern string) *PathSpec {
return &PathSpec{
pattern: pattern,
inheritMiddlewares: true,
}
}
func MountPath(prefix string, path *PathSpec) *PathSpec {
prefix = strings.TrimSuffix(prefix, "/")
p := Path(prefix + "/" + strings.TrimPrefix(path.pattern, "/"))
p.rawHandler = path.rawHandler
p.inheritMiddlewares = path.inheritMiddlewares
if l := len(path.directMiddlewares); l > 0 {
p.directMiddlewares = make([]Middleware, l)
copy(p.directMiddlewares, path.directMiddlewares)
}
return p
}
func (p *PathSpec) HasHandler() bool {
return p.rawHandler != nil
}
func (p *PathSpec) Pattern() string {
return p.pattern
}
func (p *PathSpec) Use(middlewares ...Middleware) *PathSpec {
p.directMiddlewares = append(p.directMiddlewares, middlewares...)
return p
}
// Inherit specifies whether the middlewares from the parent path should be inherited.
func (p *PathSpec) Inherit(v bool) *PathSpec {
p.inheritMiddlewares = v
return p
}
func (p *PathSpec) InheritMiddlewares(middlewares ...Middleware) *PathSpec {
p.inheritedMiddlewares = append(p.inheritedMiddlewares, middlewares...)
return p
}
func (p *PathSpec) ServeHTTP(w http.ResponseWriter, req *http.Request) {
p.compile()
p.handler.ServeHTTP(w, req)
}
func (p *PathSpec) invalidate() {
p.compiled.Store(false)
if len(p.inheritedMiddlewares) > 0 {
p.inheritedMiddlewares = p.inheritedMiddlewares[:0]
}
}
func (p *PathSpec) compile() {
if p.compiled.Load() {
return
}
p.handler = p.rawHandler
if l := len(p.directMiddlewares); l > 0 {
for i := l - 1; i >= 0; i-- {
p.handler = p.directMiddlewares[i].Wrap(p.handler)
}
}
if l := len(p.inheritedMiddlewares); l > 0 {
for i := l - 1; i >= 0; i-- {
p.handler = p.inheritedMiddlewares[i].Wrap(p.handler)
}
}
p.compiled.Store(true)
}
func (p *PathSpec) Method(m string, h http.Handler) *PathSpec {
p.rawHandler = middleware.RestrictMethod(m).Wrap(h)
return p
}
func (p *PathSpec) Get(h http.Handler) *PathSpec {
return p.Method(http.MethodGet, h)
}
func (p *PathSpec) Head(h http.Handler) *PathSpec {
return p.Method(http.MethodHead, h)
}
func (p *PathSpec) Post(h http.Handler) *PathSpec {
return p.Method(http.MethodPost, h)
}
func (p *PathSpec) Put(h http.Handler) *PathSpec {
return p.Method(http.MethodPut, h)
}
func (p *PathSpec) Patch(h http.Handler) *PathSpec {
return p.Method(http.MethodPatch, h)
}
func (p *PathSpec) Delete(h http.Handler) *PathSpec {
return p.Method(http.MethodDelete, h)
}
func (p *PathSpec) Connect(h http.Handler) *PathSpec {
return p.Method(http.MethodConnect, h)
}
func (p *PathSpec) Options(h http.Handler) *PathSpec {
return p.Method(http.MethodOptions, h)
}
func (p *PathSpec) Trace(h http.Handler) *PathSpec {
return p.Method(http.MethodTrace, h)
}