Skip to content
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

Feat/#issue284 #325

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why such a change?

You could add a commit if it's about formatting files

6 changes: 3 additions & 3 deletions serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
s.printStartupMessage()

s.Server.Handler = s.Mux
if s.corsMiddleware != nil {
s.Server.Handler = s.corsMiddleware(s.Server.Handler)
}
for _, middleware := range s.globalMiddlewares {

Check failure on line 36 in serve.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not properly formatted (gci)
s.Server.Handler = middleware(s.Server.Handler)
}
}

func (s *Server) printStartupMessage() {
Expand Down
31 changes: 20 additions & 11 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@
// [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.
Expand Down Expand Up @@ -171,22 +170,32 @@
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:

Check failure on line 173 in server.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not properly formatted (gci)
// 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 Expand Up @@ -441,4 +450,4 @@
s.loggingConfig.RequestIDFunc = loggingConfig.RequestIDFunc
}
}
}
}
Loading