diff --git a/net_http_mux.go b/net_http_mux.go index 7450dd97..67d48cce 100644 --- a/net_http_mux.go +++ b/net_http_mux.go @@ -232,4 +232,4 @@ var _ Registerer[string, any] = netHttpRouteRegisterer[string, any]{} func (a netHttpRouteRegisterer[T, B]) Register() Route[T, B] { return *Register(a.s, a.route, a.controller) -} +} \ No newline at end of file diff --git a/serve.go b/serve.go index 33e76e11..9411ca64 100644 --- a/serve.go +++ b/serve.go @@ -33,9 +33,9 @@ func (s *Server) setup() { s.printStartupMessage() s.Server.Handler = s.Mux - if s.corsMiddleware != nil { - s.Server.Handler = s.corsMiddleware(s.Server.Handler) - } + for _, middleware := range s.globalMiddlewares { + s.Server.Handler = middleware(s.Server.Handler) + } } func (s *Server) printStartupMessage() { diff --git a/server.go b/server.go index 6183fee3..b9e7ab47 100644 --- a/server.go +++ b/server.go @@ -41,10 +41,9 @@ type Server struct { // [http.ServeMux.Handle] can also be used to register routes. Mux *http.ServeMux - // Not stored with the other middlewares because it is a special case : - // it applies on routes that are not registered. - // For example, it allows OPTIONS /foo even if it is not declared (only GET /foo is declared). - corsMiddleware func(http.Handler) http.Handler + // globalMiddlewares is used to store the options + // that will be applied on ALL routes. + globalMiddlewares []func(http.Handler) http.Handler // routeOptions is used to store the options // that will be applied of the route. @@ -171,22 +170,32 @@ func WithTemplateFS(fs fs.FS) func(*Server) { return func(c *Server) { c.fs = fs } } -// WithCorsMiddleware registers a middleware to handle CORS. -// It is not handled like other middlewares with [Use] because it applies routes that are not registered. -// For example: + +// WithGlobalMiddleware adds middleware(s) that will be executed on ALL requests, +// even those that don't match any registered routes. +// For example, to add CORS middleware: // // import "github.com/rs/cors" // // s := fuego.NewServer( -// WithCorsMiddleware(cors.New(cors.Options{ +// WithGlobalMiddleware(cors.New(cors.Options{ // AllowedOrigins: []string{"*"}, // AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}, // AllowedHeaders: []string{"*"}, // AllowCredentials: true, -// }).Handler) +// }).Handler), // ) +func WithGlobalMiddleware(middlewares ...func(http.Handler) http.Handler) func(*Server) { + return func(c *Server) { + c.globalMiddlewares = append(c.globalMiddlewares, middlewares...) + } +} + +// WithCorsMiddleware adds CORS middleware to the server. +// +// Deprecated: Please use [WithGlobalMiddleware] instead. func WithCorsMiddleware(corsMiddleware func(http.Handler) http.Handler) func(*Server) { - return func(c *Server) { c.corsMiddleware = corsMiddleware } + return WithGlobalMiddleware(corsMiddleware) } // WithGlobalResponseTypes adds default response types to the server. @@ -441,4 +450,4 @@ func WithLoggingMiddleware(loggingConfig LoggingConfig) func(*Server) { s.loggingConfig.RequestIDFunc = loggingConfig.RequestIDFunc } } -} +} \ No newline at end of file