Skip to content

Instantly share code, notes, and snippets.

@geekKeen
Created November 6, 2018 02:31
Show Gist options
  • Save geekKeen/e4b4fe05e922e65939f0135610cc1e8c to your computer and use it in GitHub Desktop.
Save geekKeen/e4b4fe05e922e65939f0135610cc1e8c to your computer and use it in GitHub Desktop.
Golang
type APIHandler func(http.ResponseWriter, req *http.Request, ps httprouter.Params)
type Decorator func(APIHandler) APIHandler
func Log(logf lg.AppLogFunc) Decorator{
return func(f APIHandler) APIHandler{
return func(w http.ResponseWriter, req *http.Request, ps httprouter.Parms){
start = time.Now()
response, err = f(w, req, ps)
elapsed = time.Since(Start)
status := 200
# ...
logf(lg.INFO, "%d %s %s (%s) %s",
status, req.Method, req.URL.RequestURI(), req.RemoteAddr, elapsed)
}
}
}
func Decorate(f APIHandler, ds ...Decorator) httprouter.Handler{
decorated := f
for _, decorate := range ds {
decoreated = decorate(decorated)
}
return func(w http.ResponseWriter, req *http.Request, ps httprouter.Params){
decoreated(w, req, ps)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment