mirror of
https://github.com/notherealmarco/WASAPhoto.git
synced 2025-03-14 14:16:15 +01:00
38 lines
1.2 KiB
Go
38 lines
1.2 KiB
Go
|
package api
|
||
|
|
||
|
import (
|
||
|
"github.com/notherealmarco/WASAPhoto/service/api/reqcontext"
|
||
|
"github.com/gofrs/uuid"
|
||
|
"github.com/julienschmidt/httprouter"
|
||
|
"github.com/sirupsen/logrus"
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
// httpRouterHandler is the signature for functions that accepts a reqcontext.RequestContext in addition to those
|
||
|
// required by the httprouter package.
|
||
|
type httpRouterHandler func(http.ResponseWriter, *http.Request, httprouter.Params, reqcontext.RequestContext)
|
||
|
|
||
|
// wrap parses the request and adds a reqcontext.RequestContext instance related to the request.
|
||
|
func (rt *_router) wrap(fn httpRouterHandler) func(http.ResponseWriter, *http.Request, httprouter.Params) {
|
||
|
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||
|
reqUUID, err := uuid.NewV4()
|
||
|
if err != nil {
|
||
|
rt.baseLogger.WithError(err).Error("can't generate a request UUID")
|
||
|
w.WriteHeader(http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
var ctx = reqcontext.RequestContext{
|
||
|
ReqUUID: reqUUID,
|
||
|
}
|
||
|
|
||
|
// Create a request-specific logger
|
||
|
ctx.Logger = rt.baseLogger.WithFields(logrus.Fields{
|
||
|
"reqid": ctx.ReqUUID.String(),
|
||
|
"remote-ip": r.RemoteAddr,
|
||
|
})
|
||
|
|
||
|
// Call the next handler in chain (usually, the handler function for the path)
|
||
|
fn(w, r, ps, ctx)
|
||
|
}
|
||
|
}
|