Simplify default middlewares setup

This commit is contained in:
Deluan 2024-01-20 19:17:21 -05:00
parent 34c29a156f
commit 3ca4f44118
2 changed files with 10 additions and 13 deletions

View File

@ -116,6 +116,7 @@ func compressMiddleware() func(http.Handler) http.Handler {
"text/plain", "text/plain",
"text/css", "text/css",
"text/javascript", "text/javascript",
"text/event-stream",
) )
} }

View File

@ -160,7 +160,7 @@ func (s *Server) initRoutes() {
r := chi.NewRouter() r := chi.NewRouter()
middlewares := chi.Middlewares{ defaultMiddlewares := chi.Middlewares{
secureMiddleware(), secureMiddleware(),
corsHandler(), corsHandler(),
middleware.RequestID, middleware.RequestID,
@ -170,30 +170,26 @@ func (s *Server) initRoutes() {
robotsTXT(ui.BuildAssets()), robotsTXT(ui.BuildAssets()),
serverAddressMiddleware, serverAddressMiddleware,
clientUniqueIDMiddleware, clientUniqueIDMiddleware,
compressMiddleware(),
loggerInjector,
authHeaderMapper,
jwtVerifier,
} }
// Mount the Native API /events endpoint with all middlewares, except the compress and request logger, // Mount the Native API /events endpoint with all default middlewares, adding the authentication middlewares
// adding the authentication middlewares
if conf.Server.DevActivityPanel { if conf.Server.DevActivityPanel {
r.Group(func(r chi.Router) { r.Group(func(r chi.Router) {
r.Use(middlewares...) r.Use(defaultMiddlewares...)
r.Use(loggerInjector)
r.Use(authHeaderMapper)
r.Use(jwtVerifier)
r.Use(Authenticator(s.ds)) r.Use(Authenticator(s.ds))
r.Use(JWTRefresher) r.Use(JWTRefresher)
r.Handle(path.Join(conf.Server.BasePath, consts.URLPathNativeAPI, "events"), s.broker) r.Handle(path.Join(conf.Server.BasePath, consts.URLPathNativeAPI, "events"), s.broker)
}) })
} }
// Configure the router with the default middlewares // Configure the router with the default middlewares and requestLogger
r.Group(func(r chi.Router) { r.Group(func(r chi.Router) {
r.Use(middlewares...) r.Use(defaultMiddlewares...)
r.Use(compressMiddleware())
r.Use(loggerInjector)
r.Use(requestLogger) r.Use(requestLogger)
r.Use(authHeaderMapper)
r.Use(jwtVerifier)
s.router = r s.router = r
}) })
} }