add HTTP middleware for logging
This commit is contained in:
parent
b6a421e97b
commit
3318172c03
1
go.mod
1
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
|
||||
)
|
||||
|
2
go.sum
2
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=
|
||||
|
@ -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() {
|
||||
|
Loading…
Reference in New Issue
Block a user