This is my boilerplate for a Go http server project. Feedback and suggestions are encouraged!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
gohttpserver/httpserver/routes.go

48 lines
1.3 KiB

package httpserver
import (
sentryhttp "github.com/getsentry/sentry-go/http"
"github.com/gorilla/mux"
)
func (s *server) routes() {
s.router = mux.NewRouter()
authMiddleware := s.AuthMiddleware()
s.router.HandleFunc("/", s.handleIndex()).Methods("GET")
// if you want to use a general purpose middleware (http.Handler
// wrapper) on a specific HandleFunc route, you need to take the
// .ServeHTTP of the http.Handler to get its HandleFunc, viz:
s.router.HandleFunc(
"/login",
authMiddleware(s.handleLogin()).ServeHTTP
).Methods("GET")
s.router.HandleFunc(
"/.well-known/healthcheck.json",
s.handleHealthCheck(),
).Methods("GET")
// route that panics for testing
// CHANGEME remove this
s.router.HandleFunc(
"/panic",
s.handlePanic()
).Methods("GET")
// the Gorilla 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.
s.router.Use(s.LoggingMiddleware())
// this adds a sentry reporting middleware if and only if
// sentry is enabled via setting of SENTRY_DSN in env.
if s.sentryEnabled {
sentryHandler := sentryhttp.New(sentryhttp.Options{})
s.router.Use(sentryHandler.Handle)
}
}