Add simple requests logger (in style of Apache Common log format).

Fix Gopkg.lock integrity error.
This commit is contained in:
ErickSkrauch 2020-04-02 19:34:39 +03:00
parent 280a55d553
commit 532f2206da
No known key found for this signature in database
GPG Key ID: 669339FCBB30EE0E
3 changed files with 27 additions and 2 deletions

1
Gopkg.lock generated
View File

@ -318,6 +318,7 @@
"github.com/mediocregopher/radix.v2/redis", "github.com/mediocregopher/radix.v2/redis",
"github.com/mediocregopher/radix.v2/util", "github.com/mediocregopher/radix.v2/util",
"github.com/mono83/slf", "github.com/mono83/slf",
"github.com/mono83/slf/params",
"github.com/mono83/slf/rays", "github.com/mono83/slf/rays",
"github.com/mono83/slf/recievers/sentry", "github.com/mono83/slf/recievers/sentry",
"github.com/mono83/slf/recievers/statsd", "github.com/mono83/slf/recievers/statsd",

View File

@ -2,6 +2,7 @@ package eventsubscribers
import ( import (
"net" "net"
"net/http"
"net/url" "net/url"
"syscall" "syscall"
@ -17,10 +18,29 @@ type Logger struct {
} }
func (l *Logger) ConfigureWithDispatcher(d dispatcher.EventDispatcher) { 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:usernames:after_call", l.createMojangTexturesErrorHandler("usernames"))
d.Subscribe("mojang_textures:textures:after_call", l.createMojangTexturesErrorHandler("textures")) 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) { func (l *Logger) createMojangTexturesErrorHandler(provider string) func(identity string, result interface{}, err error) {
providerParam := wd.NameParam(provider) providerParam := wd.NameParam(provider)
return func(identity string, result interface{}, err error) { return func(identity string, result interface{}, err error) {

View File

@ -89,8 +89,10 @@ type Skinsystem struct {
} }
func (ctx *Skinsystem) CreateHandler() *mux.Router { func (ctx *Skinsystem) CreateHandler() *mux.Router {
requestEventsMiddleware := CreateRequestEventsMiddleware(ctx.Emitter, "skinsystem")
router := mux.NewRouter().StrictSlash(true) 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("/skins/{username}", ctx.Skin).Methods(http.MethodGet)
router.HandleFunc("/cloaks/{username}", ctx.Cape).Methods(http.MethodGet).Name("cloaks") 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/id:{id:[0-9]+}", ctx.DeleteSkinByUserId).Methods(http.MethodDelete)
apiRouter.HandleFunc("/skins/{username}", ctx.DeleteSkinByUsername).Methods(http.MethodDelete) apiRouter.HandleFunc("/skins/{username}", ctx.DeleteSkinByUsername).Methods(http.MethodDelete)
// 404 // 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 return router
} }