Files
Nicholas Wiersma d932d50232 chore: lint code with golangci-lint (#1828)
* feat: add golangci-lint linting

* chore: fix linter issues

* feat: add linting into the workflow

* docs: update lint docs

* fix: cr suggestions

* fix: remove old formatting and vetting scripts

* fix: add docker make target

* fix: action go caching

* fix: depreciated actions checkout version

* fix: cr suggestion

* fix: cr suggestions

---------

Co-authored-by: Manu Gupta <manugupt1@gmail.com>
2023-02-24 20:39:17 -08:00

30 lines
777 B
Go

package middleware
import (
"net/http"
"github.com/gomods/athens/pkg/log"
"github.com/gomods/athens/pkg/requestid"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
)
// LogEntryMiddleware builds a log.Entry, setting the request fields
// and storing it in the context to be used throughout the stack.
func LogEntryMiddleware(lggr *log.Logger) mux.MiddlewareFunc {
return func(h http.Handler) http.Handler {
f := func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ent := lggr.WithFields(logrus.Fields{
"http-method": r.Method,
"http-path": r.URL.Path,
"request-id": requestid.FromContext(ctx),
})
ctx = log.SetEntryInContext(ctx, ent)
r = r.WithContext(ctx)
h.ServeHTTP(w, r)
}
return http.HandlerFunc(f)
}
}