cleanups:
continuous-integration/drone/pr Build is passing Details
continuous-integration/drone/push Build is passing Details

* move metrics endpoint protection middleware to correct file
* move /metrics route to Chi route group
* update readme
This commit is contained in:
Jeffrey Paul 2020-10-03 23:03:00 -07:00
parent 687e9accf8
commit a26c0b2b47
3 changed files with 31 additions and 19 deletions

View File

@ -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)

View File

@ -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"),
},
},
)
}

View File

@ -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))
})
}
}