2020-09-30 06:35:07 +00:00
|
|
|
package httpserver
|
|
|
|
|
2020-09-30 07:48:56 +00:00
|
|
|
import (
|
2020-10-03 07:22:23 +00:00
|
|
|
"net/http"
|
2020-10-03 05:40:33 +00:00
|
|
|
"time"
|
|
|
|
|
2020-10-04 05:46:48 +00:00
|
|
|
basicauth "github.com/99designs/basicauth-go"
|
2020-10-01 04:59:20 +00:00
|
|
|
sentryhttp "github.com/getsentry/sentry-go/http"
|
2020-10-03 05:40:33 +00:00
|
|
|
"github.com/go-chi/chi"
|
|
|
|
"github.com/go-chi/chi/middleware"
|
2020-10-03 07:22:23 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2020-10-04 05:46:48 +00:00
|
|
|
"github.com/spf13/viper"
|
2020-09-30 07:48:56 +00:00
|
|
|
)
|
2020-09-30 06:35:07 +00:00
|
|
|
|
|
|
|
func (s *server) routes() {
|
2020-10-03 05:45:09 +00:00
|
|
|
|
2020-10-03 05:40:33 +00:00
|
|
|
s.router = chi.NewRouter()
|
2020-09-30 07:48:56 +00:00
|
|
|
|
2020-10-03 05:45:09 +00:00
|
|
|
// the mux .Use() takes a http.Handler wrapper func, like most
|
|
|
|
// things that deal with "middlewares" like alice et c, and will
|
|
|
|
// call ServeHTTP on it. These middlewares applied by the mux (you
|
|
|
|
// can .Use() more than one) will be applied to every request into
|
|
|
|
// the service.
|
2020-10-03 05:40:33 +00:00
|
|
|
|
2020-10-04 05:46:48 +00:00
|
|
|
s.router.Use(middleware.Recoverer)
|
2020-10-03 05:40:33 +00:00
|
|
|
s.router.Use(middleware.RequestID)
|
|
|
|
s.router.Use(s.LoggingMiddleware())
|
2020-10-04 05:46:48 +00:00
|
|
|
|
|
|
|
// add metrics middleware only if we can serve them behind auth
|
|
|
|
if viper.GetString("METRICS_USERNAME") != "" {
|
|
|
|
s.router.Use(s.MetricsMiddleware())
|
|
|
|
}
|
2020-10-03 05:45:09 +00:00
|
|
|
|
2020-10-03 07:22:23 +00:00
|
|
|
// CHANGEME to suit your needs, or pull from config.
|
2020-10-04 05:46:48 +00:00
|
|
|
// timeout for request context; your handlers must finish within
|
2020-10-03 05:45:09 +00:00
|
|
|
// this window:
|
2020-10-03 05:40:33 +00:00
|
|
|
s.router.Use(middleware.Timeout(60 * time.Second))
|
|
|
|
|
2020-10-03 05:45:09 +00:00
|
|
|
// this adds a sentry reporting middleware if and only if sentry is
|
2020-10-04 05:46:48 +00:00
|
|
|
// enabled via setting of SENTRY_DSN in env.
|
2020-10-03 05:40:33 +00:00
|
|
|
if s.sentryEnabled {
|
|
|
|
// Options docs at
|
|
|
|
// https://docs.sentry.io/platforms/go/guides/http/
|
2020-10-04 05:46:48 +00:00
|
|
|
// we set sentry to repanic so that all panics bubble up to the
|
|
|
|
// Recoverer chi middleware above.
|
|
|
|
sentryHandler := sentryhttp.New(sentryhttp.Options{
|
|
|
|
Repanic: true,
|
|
|
|
})
|
2020-10-03 05:40:33 +00:00
|
|
|
s.router.Use(sentryHandler.Handle)
|
|
|
|
}
|
2020-09-30 08:12:59 +00:00
|
|
|
|
2020-10-03 05:40:33 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// ROUTES
|
|
|
|
// complete docs: https://github.com/go-chi/chi
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
2020-10-03 05:45:09 +00:00
|
|
|
|
2020-10-03 05:40:33 +00:00
|
|
|
s.router.Get("/", s.handleIndex())
|
2020-09-30 08:12:59 +00:00
|
|
|
|
|
|
|
// if you want to use a general purpose middleware (http.Handler
|
|
|
|
// wrapper) on a specific HandleFunc route, you need to take the
|
2020-09-30 08:30:21 +00:00
|
|
|
// .ServeHTTP of the http.Handler to get its HandleFunc, viz:
|
2020-10-03 05:40:33 +00:00
|
|
|
authMiddleware := s.AuthMiddleware()
|
|
|
|
s.router.Get(
|
2020-10-01 05:25:33 +00:00
|
|
|
"/login",
|
|
|
|
authMiddleware(s.handleLogin()).ServeHTTP,
|
2020-10-03 05:40:33 +00:00
|
|
|
)
|
2020-09-30 08:12:59 +00:00
|
|
|
|
2020-10-01 04:59:20 +00:00
|
|
|
// route that panics for testing
|
|
|
|
// CHANGEME remove this
|
2020-10-03 05:40:33 +00:00
|
|
|
s.router.Get(
|
2020-10-01 05:25:33 +00:00
|
|
|
"/panic",
|
|
|
|
s.handlePanic(),
|
2020-10-03 05:40:33 +00:00
|
|
|
)
|
2020-10-01 04:59:20 +00:00
|
|
|
|
2020-10-03 07:22:23 +00:00
|
|
|
s.router.Get(
|
2020-10-04 05:46:48 +00:00
|
|
|
"/.well-known/healthcheck.json",
|
|
|
|
s.handleHealthCheck(),
|
2020-10-03 07:22:23 +00:00
|
|
|
)
|
2020-10-04 05:46:48 +00:00
|
|
|
|
|
|
|
// 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,
|
|
|
|
)
|
|
|
|
}
|
2020-09-30 06:35:07 +00:00
|
|
|
}
|