feat: Access Logging on the user server

Closes: #98

Co-authored-by: Roberto Abdelkader Martínez Pérez <robertomartinezp@gmail.com>
This commit is contained in:
pancho horrillo
2020-12-16 17:51:22 +01:00
parent 41be4f9344
commit 4d05223224
7 changed files with 93 additions and 4 deletions
+31 -1
View File
@@ -21,6 +21,7 @@ import (
"github.com/gorilla/mux"
"github.com/BBVA/kapow/internal/logger"
"github.com/BBVA/kapow/internal/server/model"
)
@@ -30,6 +31,35 @@ func gorillize(rs []model.Route, buildHandler func(model.Route) http.Handler) *m
for _, r := range rs {
m.Handle(r.Pattern, buildHandler(r)).Methods(r.Method)
}
m.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
logger.LogAccess(
r.RemoteAddr,
"-",
"-",
r.Method,
r.RequestURI,
r.Proto,
http.StatusNotFound,
0,
r.Header.Get("Referer"),
r.Header.Get("User-Agent"),
)
})
m.MethodNotAllowedHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusMethodNotAllowed)
logger.LogAccess(
r.RemoteAddr,
"-",
"-",
r.Method,
r.RequestURI,
r.Proto,
http.StatusMethodNotAllowed,
0,
r.Header.Get("Referer"),
r.Header.Get("User-Agent"),
)
})
return m
}