saturday's cleanups #8

Manually merged
sneak merged 4 commits from saturday into master 2020-10-04 19:07:10 +00:00
3 changed files with 31 additions and 19 deletions
Showing only changes of commit a26c0b2b47 - Show all commits

View File

@ -25,15 +25,18 @@ Alternately, even just feedback is great:
* Stub Authentication middleware * Stub Authentication middleware
* Helper functions for encoding/decoding json * Helper functions for encoding/decoding json
* Healthcheck route * 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 # Design Decisions
* TLS is terminated somewhere else, like on a sidecar or reverse proxy. * TLS is terminated somewhere else, like on a sidecar or reverse proxy.
* logging: [rs/zerolog](https://github.com/rs/zerolog) * logging: [rs/zerolog](https://github.com/rs/zerolog)
* configuration: [spf13/viper](https://github.com/spf13/viper) * configuration: [spf13/viper](https://github.com/spf13/viper)
* mux/router: [go-chi/chi](https://github.com/go-chi/chi) * used as a wrapper around env vars
* prometheus-style metrics via [slok/go-http-metrics](https://github.com/slok/go-http-metrics) * 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)) * database: TBD (thinking about [go-gorm/gorm](https://github.com/go-gorm/gorm))
* templating: TBD (suggestions welcome) * templating: TBD (suggestions welcome)
@ -44,6 +47,11 @@ Alternately, even just feedback is great:
* sync.Once example for precompiling templates * sync.Once example for precompiling templates
* Bundling Static Assets Into Binary * 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 # Author
* [sneak@sneak.berlin](mailto:sneak@sneak.berlin) * [sneak@sneak.berlin](mailto:sneak@sneak.berlin)

View File

@ -5,10 +5,13 @@ import (
"net/http" "net/http"
"time" "time"
basicauth "github.com/99designs/basicauth-go"
"github.com/go-chi/chi/middleware" "github.com/go-chi/chi/middleware"
metrics "github.com/slok/go-http-metrics/metrics/prometheus" metrics "github.com/slok/go-http-metrics/metrics/prometheus"
ghmm "github.com/slok/go-http-metrics/middleware" ghmm "github.com/slok/go-http-metrics/middleware"
"github.com/slok/go-http-metrics/middleware/std" "github.com/slok/go-http-metrics/middleware/std"
"github.com/spf13/viper"
) )
// the following is from // the following is from
@ -90,3 +93,15 @@ func (s *server) MetricsMiddleware() func(http.Handler) http.Handler {
return std.Handler("", mdlw, next) 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" "net/http"
"time" "time"
basicauth "github.com/99designs/basicauth-go"
sentryhttp "github.com/getsentry/sentry-go/http" sentryhttp "github.com/getsentry/sentry-go/http"
"github.com/go-chi/chi" "github.com/go-chi/chi"
"github.com/go-chi/chi/middleware" "github.com/go-chi/chi/middleware"
@ -79,20 +78,10 @@ func (s *server) routes() {
// set up authenticated /metrics route: // set up authenticated /metrics route:
if viper.GetString("METRICS_USERNAME") != "" { if viper.GetString("METRICS_USERNAME") != "" {
metricsAuthMiddleware := basicauth.New( s.router.Group(func(r chi.Router) {
"metrics", r.Use(s.MetricsAuthMiddleware())
map[string][]string{ r.Get("/metrics", http.HandlerFunc(promhttp.Handler().ServeHTTP))
viper.GetString("METRICS_USERNAME"): { })
viper.GetString("METRICS_PASSWORD"),
},
},
)
s.router.Get(
"/metrics",
metricsAuthMiddleware(
http.HandlerFunc(promhttp.Handler().ServeHTTP),
).ServeHTTP,
)
} }
} }