parent
6793e8ef1d
commit
e27c6e7f45
4
guard.go
4
guard.go
|
@ -18,6 +18,7 @@ type Guard struct {
|
||||||
AllowIPNet *net.IPNet `json:"allow_ipnet"`
|
AllowIPNet *net.IPNet `json:"allow_ipnet"`
|
||||||
|
|
||||||
basicAuth string
|
basicAuth string
|
||||||
|
Skip func(r *http.Request) bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func New() *Guard {
|
func New() *Guard {
|
||||||
|
@ -91,6 +92,9 @@ func (g *Guard) Guard(next http.Handler) http.Handler {
|
||||||
} else {
|
} else {
|
||||||
cl = l.Info()
|
cl = l.Info()
|
||||||
}
|
}
|
||||||
|
if g.Skip != nil && g.Skip(r) {
|
||||||
|
return
|
||||||
|
}
|
||||||
cl.
|
cl.
|
||||||
Str("method", r.Method).
|
Str("method", r.Method).
|
||||||
Str("ip", ip).
|
Str("ip", ip).
|
||||||
|
|
|
@ -28,5 +28,9 @@ func TestGuard(t *testing.T) {
|
||||||
mux.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
|
||||||
rw.Write([]byte("ok"))
|
rw.Write([]byte("ok"))
|
||||||
})
|
})
|
||||||
http.ListenAndServe(":8000", New().Guard(mux))
|
g := New()
|
||||||
|
g.Skip = func(r *http.Request) bool {
|
||||||
|
return r.URL.Path == "/skip"
|
||||||
|
}
|
||||||
|
http.ListenAndServe(":8000", g.Guard(mux))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue