From 532f2206dad8f5f0e973be0b717ca58fe73df70a Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Thu, 2 Apr 2020 19:34:39 +0300 Subject: [PATCH] Add simple requests logger (in style of Apache Common log format). Fix Gopkg.lock integrity error. --- Gopkg.lock | 1 + eventsubscribers/logger.go | 20 ++++++++++++++++++++ http/skinsystem.go | 8 ++++++-- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index 3706dc6..7d9c796 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -318,6 +318,7 @@ "github.com/mediocregopher/radix.v2/redis", "github.com/mediocregopher/radix.v2/util", "github.com/mono83/slf", + "github.com/mono83/slf/params", "github.com/mono83/slf/rays", "github.com/mono83/slf/recievers/sentry", "github.com/mono83/slf/recievers/statsd", diff --git a/eventsubscribers/logger.go b/eventsubscribers/logger.go index 2399ae7..f338c3f 100644 --- a/eventsubscribers/logger.go +++ b/eventsubscribers/logger.go @@ -2,6 +2,7 @@ package eventsubscribers import ( "net" + "net/http" "net/url" "syscall" @@ -17,10 +18,29 @@ type Logger struct { } func (l *Logger) ConfigureWithDispatcher(d dispatcher.EventDispatcher) { + d.Subscribe("skinsystem:after_request", l.handleAfterSkinsystemRequest) + d.Subscribe("mojang_textures:usernames:after_call", l.createMojangTexturesErrorHandler("usernames")) d.Subscribe("mojang_textures:textures:after_call", l.createMojangTexturesErrorHandler("textures")) } +func (l *Logger) handleAfterSkinsystemRequest(req *http.Request, statusCode int) { + forwardedIp := req.Header.Get("X-Forwarded-For") + if forwardedIp == "" { + forwardedIp = req.Header.Get("X-Real-Ip") + } + + l.Info( + ":ip - - \":method :path\" :statusCode \":userAgent\" \":forwardedIp\"", + wd.StringParam("ip", req.RemoteAddr), + wd.StringParam("method", req.Method), + wd.StringParam("path", req.URL.Path), + wd.IntParam("statusCode", statusCode), + wd.StringParam("userAgent", req.UserAgent()), + wd.StringParam("forwardedIp", forwardedIp), + ) +} + func (l *Logger) createMojangTexturesErrorHandler(provider string) func(identity string, result interface{}, err error) { providerParam := wd.NameParam(provider) return func(identity string, result interface{}, err error) { diff --git a/http/skinsystem.go b/http/skinsystem.go index a5c6f11..a7829a0 100644 --- a/http/skinsystem.go +++ b/http/skinsystem.go @@ -89,8 +89,10 @@ type Skinsystem struct { } func (ctx *Skinsystem) CreateHandler() *mux.Router { + requestEventsMiddleware := CreateRequestEventsMiddleware(ctx.Emitter, "skinsystem") + router := mux.NewRouter().StrictSlash(true) - router.Use(CreateRequestEventsMiddleware(ctx.Emitter, "skinsystem")) + router.Use(requestEventsMiddleware) router.HandleFunc("/skins/{username}", ctx.Skin).Methods(http.MethodGet) router.HandleFunc("/cloaks/{username}", ctx.Cape).Methods(http.MethodGet).Name("cloaks") @@ -106,7 +108,9 @@ func (ctx *Skinsystem) CreateHandler() *mux.Router { apiRouter.HandleFunc("/skins/id:{id:[0-9]+}", ctx.DeleteSkinByUserId).Methods(http.MethodDelete) apiRouter.HandleFunc("/skins/{username}", ctx.DeleteSkinByUsername).Methods(http.MethodDelete) // 404 - router.NotFoundHandler = http.HandlerFunc(NotFound) + // NotFoundHandler doesn't call for registered middlewares, so we must wrap it manually. + // See https://github.com/gorilla/mux/issues/416#issuecomment-600079279 + router.NotFoundHandler = requestEventsMiddleware(http.HandlerFunc(NotFound)) return router }