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 2 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 @@ var _ Registerer[string, any] = netHttpRouteRegisterer[string, any]{}

func (a netHttpRouteRegisterer[T, B]) Register() Route[T, B] {
return *Register(a.s, a.route, a.controller)
}
}
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

26 changes: 19 additions & 7 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you still need this then?

Copy link
Member

Choose a reason for hiding this comment

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

True, as it is not exposed, we can remove it, no need for deprecation notice!


// 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)
Expand Down Expand Up @@ -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.
Copy link
Member

Choose a reason for hiding this comment

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

Please use the same syntax than all other deprecated notices, the one understood by Go editors and godoc : skip line then write Deprecated: xxx.

Also, please reference [WithGlobalMiddlewares] with brackets so it can be recognized by other Go tools like Godoc :)

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 +453,4 @@ func WithLoggingMiddleware(loggingConfig LoggingConfig) func(*Server) {
s.loggingConfig.RequestIDFunc = loggingConfig.RequestIDFunc
}
}
}
}
Loading