cleanups:
* move metrics endpoint protection middleware to correct file * move /metrics route to Chi route group * update readme
This commit is contained in:
@@ -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"),
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user