Skip to content

Commit

Permalink
add request logging
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasf committed Feb 6, 2022
1 parent ff7662e commit b50c79b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
1 change: 0 additions & 1 deletion deploy
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
./run \
-log.debug \
-log.file.name /tmp/natbwmon.log \
-log.file.maxsize=10 \
"${@}"
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
require (
github.com/google/go-cmp v0.5.7 // indirect
github.com/josharian/native v1.0.0 // indirect
github.com/justinas/alice v1.2.0
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/mdlayher/netlink v1.6.0 // indirect
github.com/mdlayher/socket v0.1.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ github.com/jsimonetti/rtnetlink v0.0.0-20201220180245-69540ac93943/go.mod h1:z4c
github.com/jsimonetti/rtnetlink v0.0.0-20210122163228-8d122574c736/go.mod h1:ZXpIyOK59ZnN7J0BV99cZUPmsqDRZ3eq5X+st7u/oSA=
github.com/jsimonetti/rtnetlink v0.0.0-20210212075122-66c871082f2b/go.mod h1:8w9Rh8m+aHZIG69YPGGem1i5VzoyRC8nw2kA8B+ik5U=
github.com/jsimonetti/rtnetlink v0.0.0-20210525051524-4cc836578190/go.mod h1:NmKSdU4VGSiv1bMsdqNALI4RSvvjtz65tTMCnD05qLo=
github.com/justinas/alice v1.2.0 h1:+MHSA/vccVCF4Uq37S42jwlkvI2Xzl7zTPCN5BnZNVo=
github.com/justinas/alice v1.2.0/go.mod h1:fN5HRH/reO/zrUflLfTN43t3vXvKzvZIENsNEe7i7qA=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
Expand Down
31 changes: 25 additions & 6 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import (
"os/exec"
"strings"
"sync"
"time"

"github.com/benbjohnson/hashfs"
"github.com/justinas/alice"
"github.com/rs/zerolog/hlog"
"github.com/some-programs/natbwmon/internal/clientstats"
"github.com/some-programs/natbwmon/internal/log"
"github.com/some-programs/natbwmon/internal/mon"
Expand Down Expand Up @@ -38,18 +41,34 @@ type Server struct {
}

func (s *Server) Routes() *http.ServeMux {
c := alice.New()
c = c.Append(
hlog.NewHandler(log.Logger),
hlog.RequestIDHandler("req_id", "Request-Id"),
hlog.AccessHandler(func(r *http.Request, status, size int, duration time.Duration) {
l := log.WithIDWithoutCaller(r.Context()).Logger()
l.Info().
Str("caller", "http").
Str("method", r.Method).
Stringer("url", r.URL).
Int("status", status).
Int("size", size).
Dur("duration", duration).
Msg("")
}),
)

mux := http.NewServeMux()

mux.Handle("/", s.Clients())
mux.Handle("/conntrack", s.Conntrack())
mux.Handle("/v1/stats/", s.StatsV1())
mux.Handle("/", c.Then(s.Clients()))
mux.Handle("/conntrack", c.Then(s.Conntrack()))
mux.Handle("/v1/stats/", c.Then(s.StatsV1()))
if s.nmapEnabled {
mux.Handle("/v0/nmap/", s.NmapV0())
mux.Handle("/v0/nmap/", c.Then(s.NmapV0()))
}
mux.Handle("/static/", hashfs.FileServer(StaticHashFS))
mux.Handle("/static/", c.Then(hashfs.FileServer(StaticHashFS)))

return mux

}

func (s *Server) Clients() AppHandler {
Expand Down

0 comments on commit b50c79b

Please sign in to comment.