sneak/integrate-di (#17)
continuous-integration/drone/push Build is failing

moving this to use uber/fx di framework instead of the ad hoc di setup before

Co-authored-by: sneak <sneak@sneak.berlin>
Reviewed-on: #17
This commit was merged in pull request #17.
This commit is contained in:
sneak committed 2023-01-29 03:06:05 +00:00
1 parent 0c3797ec30
commit dd778174a7
31 files changed
+924 -528

No files matched your search

+20 -11
View File
@@ -12,7 +12,7 @@ import (
"github.com/spf13/viper"
)
func (s *server) routes() {
func (s *Server) SetupRoutes() {
s.router = chi.NewRouter()
// the mux .Use() takes a http.Handler wrapper func, like most
@@ -23,16 +23,16 @@ func (s *server) routes() {
s.router.Use(middleware.Recoverer)
s.router.Use(middleware.RequestID)
s.router.Use(s.LoggingMiddleware())
s.router.Use(s.mw.Logging())
// add metrics middleware only if we can serve them behind auth
if viper.GetString("METRICS_USERNAME") != "" {
s.router.Use(s.MetricsMiddleware())
s.router.Use(s.mw.Metrics())
}
// set up CORS headers. you'll probably want to configure that
// in middlewares.go.
s.router.Use(s.CORSMiddleware())
s.router.Use(s.mw.CORS())
// CHANGEME to suit your needs, or pull from config.
// timeout for request context; your handlers must finish within
@@ -57,39 +57,48 @@ func (s *server) routes() {
// complete docs: https://github.com/go-chi/chi
////////////////////////////////////////////////////////////////////////
s.router.Get("/", s.handleIndex())
s.router.Get("/", s.h.HandleIndex())
s.router.Mount("/s", http.StripPrefix("/s", http.FileServer(http.FS(static.Static))))
s.router.Route("/api/v1", func(r chi.Router) {
r.Get("/now", s.handleNow())
r.Get("/now", s.h.HandleNow())
})
// 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:
authMiddleware := s.AuthMiddleware()
auth := s.mw.Auth()
s.router.Get(
"/login",
authMiddleware(s.handleLogin()).ServeHTTP,
auth(s.h.HandleLoginGET()).ServeHTTP,
)
s.router.Get(
"/signup",
auth(s.h.HandleSignupGET()).ServeHTTP,
)
s.router.Post(
"/signup",
auth(s.h.HandleSignupPOST()).ServeHTTP,
)
// route that panics for testing
// CHANGEME remove this
s.router.Get(
"/panic",
s.handlePanic(),
s.h.HandlePanic(),
)
s.router.Get(
"/.well-known/healthcheck.json",
s.handleHealthCheck(),
s.h.HandleHealthCheck(),
)
// set up authenticated /metrics route:
if viper.GetString("METRICS_USERNAME") != "" {
s.router.Group(func(r chi.Router) {
r.Use(s.MetricsAuthMiddleware())
r.Use(s.mw.MetricsAuth())
r.Get("/metrics", http.HandlerFunc(promhttp.Handler().ServeHTTP))
})
}