feat: add skip func

master v0.1.1
Evan Chen 2021-11-19 16:45:06 +08:00
parent 6793e8ef1d
commit e27c6e7f45
2 changed files with 9 additions and 1 deletions

View File

@ -18,6 +18,7 @@ type Guard struct {
AllowIPNet *net.IPNet `json:"allow_ipnet"`
basicAuth string
Skip func(r *http.Request) bool
}
func New() *Guard {
@ -91,6 +92,9 @@ func (g *Guard) Guard(next http.Handler) http.Handler {
} else {
cl = l.Info()
}
if g.Skip != nil && g.Skip(r) {
return
}
cl.
Str("method", r.Method).
Str("ip", ip).

View File

@ -28,5 +28,9 @@ func TestGuard(t *testing.T) {
mux.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
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))
}