Log HTTP requests

This commit is contained in:
Jannis Mattheis
2020-10-09 13:46:37 +00:00
parent 17f82d7556
commit da8f501f15
+18
View File
@@ -2,7 +2,10 @@ package router
import (
"encoding/json"
"github.com/rs/zerolog/hlog"
"github.com/rs/zerolog/log"
"net/http"
"time"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
@@ -21,6 +24,11 @@ type UIConfig struct {
func Router(conf config.Config, rooms *ws.Rooms, users *auth.Users, version string) *mux.Router {
router := mux.NewRouter()
router.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// https://github.com/gorilla/mux/issues/416
accessLogger(r, 404, 0, 0)
})
router.Use(hlog.AccessHandler(accessLogger))
router.Use(handlers.CORS(handlers.AllowedMethods([]string{"GET", "POST"}), handlers.AllowedOriginValidator(conf.CheckOrigin)))
router.HandleFunc("/stream", rooms.Upgrade)
router.Methods("POST").Path("/login").HandlerFunc(users.Authenticate)
@@ -39,3 +47,13 @@ func Router(conf config.Config, rooms *ws.Rooms, users *auth.Users, version stri
return router
}
func accessLogger(r *http.Request, status, size int, dur time.Duration) {
log.Debug().
Str("host", r.Host).
Int("status", status).
Int("size", size).
Str("path", r.URL.Path).
Str("duration", dur.String()).
Msg("HTTP")
}