mirror of
https://github.com/screego/server.git
synced 2026-10-31 10:32:16 +08:00
Log HTTP requests
This commit is contained in:
@@ -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")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user