Skip to content

Replace WithCorsMiddleware by WithGlobalMiddleware #325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion net_http_mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,4 @@

func (a netHttpRouteRegisterer[T, B]) Register() Route[T, B] {
return *Register(a.s, a.route, a.controller)
}
}

Check failure on line 235 in net_http_mux.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not properly formatted (gci)
5 changes: 3 additions & 2 deletions serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ func (s *Server) setup() error {
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)
}

return nil
Expand Down
28 changes: 18 additions & 10 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

*Engine

Expand Down Expand Up @@ -177,22 +176,31 @@ 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.
Expand Down
Loading