add middlewares
This commit is contained in:
25
pkgs/middleware/recover/recover.go
Normal file
25
pkgs/middleware/recover/recover.go
Normal file
@ -0,0 +1,25 @@
|
||||
package recover
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"git.jeffthecoder.xyz/guochao/cache-proxy/pkgs/middleware/httplog"
|
||||
)
|
||||
|
||||
func recoverer(w http.ResponseWriter, r *http.Request) {
|
||||
if err := recover(); err != nil {
|
||||
httplog.Logger(r).With("error", err).Error("request panicked")
|
||||
http.Error(w, fmt.Sprint(err), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func Recover() func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
defer recoverer(w, r)
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user