feat: add handle shorthand
parent
894dbc1bbf
commit
49c5239928
|
@ -25,7 +25,7 @@ func main() {
|
||||||
mux.HandleFunc("/out", func(rw http.ResponseWriter, r *http.Request) { arr := []int{0, 1}; fmt.Print(arr[9]) })
|
mux.HandleFunc("/out", func(rw http.ResponseWriter, r *http.Request) { arr := []int{0, 1}; fmt.Print(arr[9]) })
|
||||||
log.Info("start")
|
log.Info("start")
|
||||||
|
|
||||||
err := ksrv.New().Listen("0.0.0.0:8080").Serve()
|
err := ksrv.New().Handle(mux).Listen("0.0.0.0:8080").Serve()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
18
kserver.go
18
kserver.go
|
@ -67,8 +67,10 @@ func New() *kserver {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
k := &kserver{}
|
k := &kserver{
|
||||||
k.l = l
|
l: l,
|
||||||
|
nolog: func(r *http.Request) bool { return true },
|
||||||
|
}
|
||||||
return k
|
return k
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,14 +84,18 @@ func (k *kserver) Middleware(next http.Handler) http.Handler {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (k *kserver) SetNoLogCondition(nolog func(r *http.Request) bool) {
|
||||||
|
k.nolog = nolog
|
||||||
|
}
|
||||||
|
|
||||||
func (k *kserver) catch(rw *responseWriter, r *http.Request) {
|
func (k *kserver) catch(rw *responseWriter, r *http.Request) {
|
||||||
ex := recover()
|
ex := recover()
|
||||||
if ex != nil {
|
if ex != nil {
|
||||||
Abort(rw, ex)
|
Abort(rw, ex)
|
||||||
k.l.ErrorF(log.H{"Status": rw.StatueCode, "IP": GetIP(r), "Method": r.Method, "URL": r.URL.Path}, ex)
|
k.l.ErrorF(log.H{"Status": rw.StatueCode, "IP": GetIP(r), "Method": r.Method, "URL": r.URL.Path, "Agent": r.Header.Get("User-Agent")}, ex)
|
||||||
} else if rw.StatueCode >= 500 {
|
} else if rw.StatueCode >= 500 {
|
||||||
k.l.ErrorF(log.H{"Status": rw.StatueCode, "IP": GetIP(r), "Method": r.Method, "URL": r.URL.Path}, rw.err)
|
k.l.ErrorF(log.H{"Status": rw.StatueCode, "IP": GetIP(r), "Method": r.Method, "URL": r.URL.Path, "Agent": r.Header.Get("User-Agent")}, rw.err)
|
||||||
} else {
|
} else if k.nolog != nil && k.nolog(r) {
|
||||||
k.l.InfoF(log.H{"Status": rw.StatueCode, "IP": GetIP(r), "Method": r.Method, "URL": r.URL.Path})
|
k.l.InfoF(log.H{"Status": rw.StatueCode, "IP": GetIP(r), "Method": r.Method, "URL": r.URL.Path, "Agent": r.Header.Get("User-Agent")})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,13 @@ import (
|
||||||
type kserver struct {
|
type kserver struct {
|
||||||
http.Server
|
http.Server
|
||||||
l *log.Logger
|
l *log.Logger
|
||||||
|
nolog func(r *http.Request) bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// Listen to addr
|
||||||
|
func (s *kserver) Handle(h http.Handler) *kserver {
|
||||||
|
s.Handler = h
|
||||||
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// Listen to addr
|
// Listen to addr
|
||||||
|
|
|
@ -18,7 +18,7 @@ func main() {
|
||||||
mux.HandleFunc("/out", func(rw http.ResponseWriter, r *http.Request) { arr := []int{0, 1}; fmt.Print(arr[9]) })
|
mux.HandleFunc("/out", func(rw http.ResponseWriter, r *http.Request) { arr := []int{0, 1}; fmt.Print(arr[9]) })
|
||||||
log.Info("start")
|
log.Info("start")
|
||||||
|
|
||||||
err := ksrv.New().Listen("0.0.0.0:8080").Serve()
|
err := ksrv.New().Handle(mux).Listen("0.0.0.0:8080").Serve()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue