Skip to content

Commit

Permalink
feat: move cors configuration to root mux (#2901)
Browse files Browse the repository at this point in the history
  • Loading branch information
hspitzley-czi authored Jan 5, 2024
1 parent 81f446f commit 380dd21
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 5 deletions.
1 change: 1 addition & 0 deletions api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ require (
github.com/ogen-go/ogen v0.81.0
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.18.0
github.com/rs/cors v1.10.1
github.com/samber/lo v1.39.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/viper v1.18.2
Expand Down
2 changes: 2 additions & 0 deletions api/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,8 @@ github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJ
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rs/cors v1.10.1 h1:L0uuZVXIKlI1SShY2nhFfo44TYvDPQ1w4oFkUJNfhyo=
github.com/rs/cors v1.10.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
Expand Down
6 changes: 5 additions & 1 deletion api/pkg/api/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/chanzuckerberg/happy/api/pkg/setup"
"github.com/chanzuckerberg/happy/api/pkg/store"
"github.com/gofiber/fiber/v2/middleware/adaptor"
"github.com/rs/cors"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -45,5 +46,8 @@ func MakeAPIApplication(ctx context.Context, cfg *setup.Configuration, db *store
}

func (a *APIApplication) Listen() error {
return http.ListenAndServe(fmt.Sprintf(":%d", a.cfg.Api.Port), a.mux)
c := cors.New(cors.Options{
AllowedHeaders: []string{"Authorization", "Content-Type", "x-aws-access-key-id", "x-aws-secret-access-key", "x-aws-session-token", "baggage", "sentry-trace"},
})
return http.ListenAndServe(fmt.Sprintf(":%d", a.cfg.Api.Port), c.Handler(a.mux))
}
4 changes: 0 additions & 4 deletions api/pkg/api/app_fiber.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/getsentry/sentry-go"
"github.com/gofiber/contrib/fibersentry"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/requestid"
"github.com/gofiber/swagger"
Expand All @@ -38,9 +37,6 @@ func MakeFiberServer(cfg *setup.Configuration) *FiberServer {
func MakeFiberApp(ctx context.Context, cfg *setup.Configuration, db *store.DB) *FiberServer {
apiApp := MakeFiberServer(cfg).WithDatabase(db)
apiApp.FiberApp.Use(requestid.New())
apiApp.FiberApp.Use(cors.New(cors.Config{
AllowHeaders: "Authorization,Content-Type,x-aws-access-key-id,x-aws-secret-access-key,x-aws-session-token,baggage,sentry-trace",
}))
apiApp.configureLogger(cfg.Api)
apiApp.FiberApp.Use(func(c *fiber.Ctx) error {
err := request.VersionCheckHandlerFiber(c)
Expand Down

0 comments on commit 380dd21

Please sign in to comment.