Add func to debug requests

- logging middleware
- response writer with status code

Signed-off-by: Evgeniy Kulikov <kim@nspcc.ru>
This commit is contained in:
Evgeniy Kulikov 2020-08-20 02:28:16 +03:00
parent 5df041f7d7
commit 447a255d18

View file

@ -79,6 +79,11 @@ type (
// mimeType represents various MIME type used API responses.
mimeType string
logResponseWriter struct {
http.ResponseWriter
statusCode int
}
)
const (
@ -93,6 +98,13 @@ const (
mimeXML mimeType = "application/xml"
)
var _ = logErrorResponse
func (lrw *logResponseWriter) WriteHeader(code int) {
lrw.statusCode = code
lrw.ResponseWriter.WriteHeader(code)
}
func setRequestID(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// generate random UUIDv4
@ -114,6 +126,24 @@ func setRequestID(h http.Handler) http.Handler {
})
}
func logErrorResponse(l *zap.Logger) mux.MiddlewareFunc {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
lw := &logResponseWriter{ResponseWriter: w}
// pass execution:
h.ServeHTTP(lw, r)
// Ignore <300 status codes
if lw.statusCode >= http.StatusMultipleChoices {
l.Error("something went wrong",
zap.Int("status", lw.statusCode),
zap.String("method", mux.CurrentRoute(r).GetName()))
}
})
}
}
func GetRequestID(v interface{}) string {
switch t := v.(type) {
case context.Context:
@ -128,8 +158,13 @@ func GetRequestID(v interface{}) string {
func Attach(r *mux.Router, m MaxClients, h Handler, center *auth.Center, log *zap.Logger) {
api := r.PathPrefix(SlashSeparator).Subrouter()
// Attach behaviors: RequestID, ...
api.Use(setRequestID)
api.Use(
// -- prepare request
setRequestID,
// -- logging error requests
// logErrorResponse(log),
)
// Attach user authentication for all S3 routes.
AttachUserAuth(api, center, log)