From a26c0b2b47ba1606b6a349d43b4cfbe961a857e0 Mon Sep 17 00:00:00 2001 From: sneak Date: Sat, 3 Oct 2020 23:03:00 -0700 Subject: [PATCH] cleanups: * move metrics endpoint protection middleware to correct file * move /metrics route to Chi route group * update readme --- README.md | 14 +++++++++++--- httpserver/middlewares.go | 15 +++++++++++++++ httpserver/routes.go | 21 +++++---------------- 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index f17f281..f13ad5b 100644 --- a/README.md +++ b/README.md @@ -25,15 +25,18 @@ Alternately, even just feedback is great: * Stub Authentication middleware * Helper functions for encoding/decoding json * Healthcheck route -* No global state +* Prometheus metrics endpoint +* No global state of our own + * some deps have some, such as the metrics collector and Sentry # Design Decisions * TLS is terminated somewhere else, like on a sidecar or reverse proxy. * logging: [rs/zerolog](https://github.com/rs/zerolog) * configuration: [spf13/viper](https://github.com/spf13/viper) -* mux/router: [go-chi/chi](https://github.com/go-chi/chi) -* prometheus-style metrics via [slok/go-http-metrics](https://github.com/slok/go-http-metrics) + * used as a wrapper around env vars +* router is Chi: [go-chi/chi](https://github.com/go-chi/chi) +* Prometheus-style metrics via [slok/go-http-metrics](https://github.com/slok/go-http-metrics) * database: TBD (thinking about [go-gorm/gorm](https://github.com/go-gorm/gorm)) * templating: TBD (suggestions welcome) @@ -44,6 +47,11 @@ Alternately, even just feedback is great: * sync.Once example for precompiling templates * Bundling Static Assets Into Binary +# Known Bugs (more TODO) + +* Chi recovery middleware logs non-json when in non-tty stdout mode, + breaking validity of stdout as a json stream + # Author * [sneak@sneak.berlin](mailto:sneak@sneak.berlin) diff --git a/httpserver/middlewares.go b/httpserver/middlewares.go index e5995c2..70267a6 100644 --- a/httpserver/middlewares.go +++ b/httpserver/middlewares.go @@ -5,10 +5,13 @@ import ( "net/http" "time" + basicauth "github.com/99designs/basicauth-go" "github.com/go-chi/chi/middleware" metrics "github.com/slok/go-http-metrics/metrics/prometheus" ghmm "github.com/slok/go-http-metrics/middleware" + "github.com/slok/go-http-metrics/middleware/std" + "github.com/spf13/viper" ) // the following is from @@ -90,3 +93,15 @@ func (s *server) MetricsMiddleware() func(http.Handler) http.Handler { return std.Handler("", mdlw, next) } } + +func (s *server) MetricsAuthMiddleware() func(http.Handler) http.Handler { + return basicauth.New( + "metrics", + map[string][]string{ + viper.GetString("METRICS_USERNAME"): { + viper.GetString("METRICS_PASSWORD"), + }, + }, + ) + +} diff --git a/httpserver/routes.go b/httpserver/routes.go index f71e32e..7ab9ba6 100644 --- a/httpserver/routes.go +++ b/httpserver/routes.go @@ -4,7 +4,6 @@ import ( "net/http" "time" - basicauth "github.com/99designs/basicauth-go" sentryhttp "github.com/getsentry/sentry-go/http" "github.com/go-chi/chi" "github.com/go-chi/chi/middleware" @@ -79,20 +78,10 @@ func (s *server) routes() { // set up authenticated /metrics route: if viper.GetString("METRICS_USERNAME") != "" { - metricsAuthMiddleware := basicauth.New( - "metrics", - map[string][]string{ - viper.GetString("METRICS_USERNAME"): { - viper.GetString("METRICS_PASSWORD"), - }, - }, - ) - - s.router.Get( - "/metrics", - metricsAuthMiddleware( - http.HandlerFunc(promhttp.Handler().ServeHTTP), - ).ServeHTTP, - ) + s.router.Group(func(r chi.Router) { + r.Use(s.MetricsAuthMiddleware()) + r.Get("/metrics", http.HandlerFunc(promhttp.Handler().ServeHTTP)) + }) } + }