From 3318172c038008b0d3d9576d9acc93e78ad451f5 Mon Sep 17 00:00:00 2001 From: m5r Date: Sun, 28 Jul 2024 10:55:21 +0200 Subject: [PATCH] add HTTP middleware for logging --- go.mod | 1 + go.sum | 2 ++ http/server.go | 25 +++++++++++++++++++++---- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 58a96d3..b2d2056 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/rs/zerolog v1.33.0 github.com/spf13/cobra v1.8.1 github.com/spf13/viper v1.19.0 + github.com/urfave/negroni v1.0.0 golang.org/x/net v0.27.0 gopkg.in/natefinch/lumberjack.v2 v2.2.1 ) diff --git a/go.sum b/go.sum index 40e07a1..f18f5ec 100644 --- a/go.sum +++ b/go.sum @@ -78,6 +78,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= +github.com/urfave/negroni v1.0.0 h1:kIimOitoypq34K7TG7DUaJ9kq/N4Ofuwi1sjz0KipXc= +github.com/urfave/negroni v1.0.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30= diff --git a/http/server.go b/http/server.go index e54bad2..9e6cc46 100644 --- a/http/server.go +++ b/http/server.go @@ -10,10 +10,20 @@ import ( "strings" "time" + "github.com/urfave/negroni" "local-ip.sh/utils" ) -func newHttpMux() *http.ServeMux { +var flyRegion = os.Getenv("FLY_REGION") + +func loggingMiddleware(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) { + start := time.Now() + next(w, r) + response := w.(negroni.ResponseWriter) + utils.Logger.Debug().Str("FLY_REGION", flyRegion).Msgf("%s %s %d %s", r.Method, r.URL.Path, response.Status(), time.Since(start)) +} + +func newHttpMux() http.Handler { mux := http.NewServeMux() mux.HandleFunc("GET /server.key", func(w http.ResponseWriter, r *http.Request) { @@ -48,7 +58,9 @@ func newHttpMux() *http.ServeMux { http.ServeFile(w, r, "./http/static/index.html") }) - return mux + n := negroni.New(negroni.HandlerFunc(loggingMiddleware)) + n.UseHandler(mux) + return n } func serveHttp() *http.Server { @@ -116,7 +128,7 @@ func redirectHttpToHttps() { host = net.JoinHostPort(host, strconv.FormatUint(uint64(config.HttpsPort), 10)) } - url.Host = r.Host + url.Host = host url.Scheme = "https" http.Redirect(w, r, url.String(), http.StatusMovedPermanently) }), @@ -133,7 +145,12 @@ func serveHttps() { Handler: mux, } utils.Logger.Info().Str("https_address", httpsServer.Addr).Msg("Starting up HTTPS server") - go httpsServer.ListenAndServeTLS("./.lego/certs/root/server.pem", "./.lego/certs/root/server.key") + go func() { + err := httpsServer.ListenAndServeTLS("./.lego/certs/root/server.pem", "./.lego/certs/root/server.key") + if err != http.ErrServerClosed { + utils.Logger.Fatal().Err(err).Msg("Unexpected error received from HTTPS server") + } + }() } func ServeHttp() {