switch router to chi, add middlewares and maintenance mode #3
@ -9,25 +9,30 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (s *server) routes() {
|
func (s *server) routes() {
|
||||||
|
|
||||||
s.router = chi.NewRouter()
|
s.router = chi.NewRouter()
|
||||||
|
|
||||||
// the mux .Use() takes a http.Handler wrapper func, like
|
// the mux .Use() takes a http.Handler wrapper func, like most
|
||||||
// most things that deal with "middlewares" like alice et c, and
|
// things that deal with "middlewares" like alice et c, and will
|
||||||
// will call ServeHTTP on it. These middlewares applied by the mux
|
// call ServeHTTP on it. These middlewares applied by the mux (you
|
||||||
// (you can .Use() more than one) will be applied to every request
|
// can .Use() more than one) will be applied to every request into
|
||||||
// into the service.
|
// the service.
|
||||||
|
|
||||||
s.router.Use(middleware.RequestID)
|
s.router.Use(middleware.RequestID)
|
||||||
s.router.Use(s.LoggingMiddleware())
|
s.router.Use(s.LoggingMiddleware())
|
||||||
// timeout for request context
|
|
||||||
|
// timeout for request context: your handlers must finish within
|
||||||
|
// this window:
|
||||||
|
// CHANGEME to suit your needs, or pull from config.
|
||||||
s.router.Use(middleware.Timeout(60 * time.Second))
|
s.router.Use(middleware.Timeout(60 * time.Second))
|
||||||
|
|
||||||
// this adds a sentry reporting middleware if and only if
|
// this adds a sentry reporting middleware if and only if sentry is
|
||||||
// sentry is enabled via setting of SENTRY_DSN in env.
|
// enabled via setting of SENTRY_DSN in env. this was at the
|
||||||
// this was at the bottom, but chi requires *all* middlewares
|
// bottom, but chi requires *all* middlewares applied before any
|
||||||
// applied before any routes are, so now it's up here.
|
// routes are, so now it's up here. unfortunately this cannot
|
||||||
// unfortunately this cannot coexist with the normal chi Recoverer
|
// coexist with the normal chi Recoverer handler which prints a nice
|
||||||
// handler which prints a nice stack trace to the console
|
// colorful stack trace to the console
|
||||||
|
|
||||||
if s.sentryEnabled {
|
if s.sentryEnabled {
|
||||||
// Options docs at
|
// Options docs at
|
||||||
// https://docs.sentry.io/platforms/go/guides/http/
|
// https://docs.sentry.io/platforms/go/guides/http/
|
||||||
@ -45,11 +50,13 @@ func (s *server) routes() {
|
|||||||
// ROUTES
|
// ROUTES
|
||||||
// complete docs: https://github.com/go-chi/chi
|
// complete docs: https://github.com/go-chi/chi
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
s.router.Get("/", s.handleIndex())
|
s.router.Get("/", s.handleIndex())
|
||||||
|
|
||||||
// if you want to use a general purpose middleware (http.Handler
|
// if you want to use a general purpose middleware (http.Handler
|
||||||
// wrapper) on a specific HandleFunc route, you need to take the
|
// wrapper) on a specific HandleFunc route, you need to take the
|
||||||
// .ServeHTTP of the http.Handler to get its HandleFunc, viz:
|
// .ServeHTTP of the http.Handler to get its HandleFunc, viz:
|
||||||
|
|
||||||
authMiddleware := s.AuthMiddleware()
|
authMiddleware := s.AuthMiddleware()
|
||||||
s.router.Get(
|
s.router.Get(
|
||||||
"/login",
|
"/login",
|
||||||
@ -63,6 +70,7 @@ func (s *server) routes() {
|
|||||||
|
|
||||||
// route that panics for testing
|
// route that panics for testing
|
||||||
// CHANGEME remove this
|
// CHANGEME remove this
|
||||||
|
|
||||||
s.router.Get(
|
s.router.Get(
|
||||||
"/panic",
|
"/panic",
|
||||||
s.handlePanic(),
|
s.handlePanic(),
|
||||||
|
Loading…
Reference in New Issue
Block a user