Created
November 6, 2018 02:31
-
-
Save geekKeen/e4b4fe05e922e65939f0135610cc1e8c to your computer and use it in GitHub Desktop.
Golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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