-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.go
45 lines (42 loc) · 1.13 KB
/
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package routex
import (
"context"
"net/http"
)
// Middleware is a function alias that can be used to handle and work on a request
// before it is handled to the assigned Mux function (or default function).
//
// Middlewares applied to Routes will be applied AFTER the global Mux Middleware.
//
// The returned boolean can be used to interrupt the call stack before handling
// back control to implement features such as redirects or authentication.
type Middleware func(context.Context, http.ResponseWriter, *Request) bool
// Middleware adds the supplied Middleware functions to the Mux.
// These are ran before control is passed until the Handler.
//
// And empty function is considered a NOP.
func (m *Mux) Middleware(w ...Middleware) {
if len(w) == 0 {
return
}
if m.wares == nil {
m.wares = &wares{w: w}
return
}
m.wares.lock.Lock()
m.wares.w = append(m.wares.w, w...)
m.wares.lock.Unlock()
}
func (h *handler) Middleware(w ...Middleware) Route {
if len(w) == 0 {
return h
}
if h.wares == nil {
h.wares = &wares{w: w}
return h
}
h.wares.lock.Lock()
h.wares.w = append(h.wares.w, w...)
h.wares.lock.Unlock()
return h
}