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

fix: Better CORS handling #61

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ type MyResponse struct {
func main() {
s := fuego.NewServer(
fuego.WithPort(":8088"),
fuego.WithCors(cors.Default()),
)

fuego.Use(s, cors.Default().Handler)

fuego.Use(s, chiMiddleware.Compress(5, "text/html", "text/css"))

// Fuego 🔥 handler with automatic OpenAPI generation, validation, (de)serialization and error handling
Expand Down
6 changes: 5 additions & 1 deletion examples/basic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ type MyResponse struct {
func main() {
s := fuego.NewServer(
fuego.WithPort(":8088"),
fuego.WithCors(cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE"},
AllowedHeaders: []string{"*"},
})),
)

fuego.Use(s, cors.Default().Handler)
fuego.Use(s, chiMiddleware.Compress(5, "text/html", "text/css"))

// Fuego 🔥 handler with automatic OpenAPI generation, validation, (de)serialization and error handling
Expand Down
7 changes: 7 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/getkin/kin-openapi/openapi3"
"github.com/golang-jwt/jwt/v5"
"github.com/rs/cors"
)
Copy link
Member

Choose a reason for hiding this comment

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

rs/cors is a middleware, I don't want to add it to fuego : users should be free to chose the middlewares they want (some users don't need cors)


type OpenapiConfig struct {
Expand Down Expand Up @@ -114,6 +115,12 @@ func NewServer(options ...func(*Server)) *Server {
return s
}

func WithCors(cors *cors.Cors) func(*Server) {
return func(s *Server) {
s.Server.Handler = cors.Handler(s.Mux)
}
}

// WithTemplateFS sets the filesystem used to load templates.
// To be used with [WithTemplateGlobs] or [WithTemplates].
// For example:
Expand Down
2 changes: 0 additions & 2 deletions serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ func (s *Server) Run() error {
slog.Debug("Server started in "+elapsed.String(), "info", "time between since server creation (fuego.NewServer) and server startup (fuego.Run). Depending on your implementation, there might be things that do not depend on fuego slowing start time")
slog.Info("Server running ✅ on http://localhost"+s.Server.Addr, "started in", elapsed.String())

s.Server.Handler = s.Mux

return s.Server.ListenAndServe()
}

Expand Down
Loading