-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.go
31 lines (26 loc) · 981 Bytes
/
middleware.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
package go_http_kit
import "net/http"
// Middleware represents a function signature for HTTP middleware.
type Middleware func(next http.HandlerFunc) http.HandlerFunc
// Middleware adds middleware to be used by all routes.
func (hk *HttpKit) Middleware(middlewares ...Middleware) *HttpKit {
hk.mu.Lock()
defer hk.mu.Unlock()
hk.middlewares = append(hk.middlewares, middlewares...)
return hk
}
// Middleware method for Group adds one or more middleware functions to the group.
// Middleware functions are applied to all routes within the group, affecting the entire group's behavior.
func (g *Group) Middleware(middlewares ...Middleware) *Group {
g.hk.mu.Lock()
defer g.hk.mu.Unlock()
g.middlewares = append(g.middlewares, middlewares...)
return g
}
// Middleware adding middleware functionality for Route
func (r *Route) Middleware(middlewares ...Middleware) *Route {
r.mu.Lock()
defer r.mu.Unlock()
r.middlewares = append(r.middlewares, middlewares...)
return r
}