From c1b3d3f8fdf256050660c675c230f12b49fd856b Mon Sep 17 00:00:00 2001 From: ekumamatthew Date: Mon, 30 Dec 2024 06:08:58 +0100 Subject: [PATCH 1/3] refactor: move accept header form mux to server options --- net_http_mux.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From cf2e7bdfb18c6132637b48d6401ae600f626ba12 Mon Sep 17 00:00:00 2001 From: ekumamatthew Date: Mon, 30 Dec 2024 06:25:17 +0100 Subject: [PATCH 2/3] replace withcoresmiddleware by globalmiddleware --- server.go | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/server.go b/server.go index 6183fee3..6ad1efd2 100644 --- a/server.go +++ b/server.go @@ -46,6 +46,10 @@ type Server struct { // 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. routeOptions []func(*BaseRoute) @@ -171,22 +175,30 @@ 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 is deprecated: 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 +453,4 @@ func WithLoggingMiddleware(loggingConfig LoggingConfig) func(*Server) { s.loggingConfig.RequestIDFunc = loggingConfig.RequestIDFunc } } -} +} \ No newline at end of file From 12b86594211bd36e5441c42dbf96ef3968e65557 Mon Sep 17 00:00:00 2001 From: ekumamatthew Date: Thu, 2 Jan 2025 11:22:03 +0100 Subject: [PATCH 3/3] remove cors middle ware form server structure --- serve.go | 6 +++--- server.go | 9 +++------ 2 files changed, 6 insertions(+), 9 deletions(-) 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 6ad1efd2..b9e7ab47 100644 --- a/server.go +++ b/server.go @@ -41,11 +41,6 @@ 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 @@ -196,7 +191,9 @@ func WithGlobalMiddleware(middlewares ...func(http.Handler) http.Handler) func(* } } -// WithCorsMiddleware is deprecated: Use WithGlobalMiddleware instead. +// WithCorsMiddleware adds CORS middleware to the server. +// +// Deprecated: Please use [WithGlobalMiddleware] instead. func WithCorsMiddleware(corsMiddleware func(http.Handler) http.Handler) func(*Server) { return WithGlobalMiddleware(corsMiddleware) }