Skip to content

Commit

Permalink
Fix CORs support in dataAPI (#1180)
Browse files Browse the repository at this point in the history
  • Loading branch information
pschork authored Jan 29, 2025
1 parent bc2f792 commit fefad3a
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions disperser/dataapi/v2/server_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,34 @@ func (s *ServerV2) Start() error {
}

router := gin.New()

// Add recovery middleware (best practice according to Cursor)
router.Use(gin.Recovery())

basePath := "/api/v2"
docsv2.SwaggerInfoV2.BasePath = basePath
docsv2.SwaggerInfoV2.Host = os.Getenv("SWAGGER_HOST")

// Configure CORS
config := cors.DefaultConfig()
config.AllowOrigins = s.allowOrigins
config.AllowCredentials = true
config.AllowMethods = []string{"GET", "POST", "HEAD", "OPTIONS"}
config.AllowHeaders = []string{"Origin", "Content-Type", "Accept", "Authorization"}
config.ExposeHeaders = []string{"Content-Length"}

if s.serverMode != gin.ReleaseMode {
config.AllowOrigins = []string{"*"}
}

// Apply CORS middleware before routes
router.Use(cors.New(config))

// Add OPTIONS handlers for all routes
router.OPTIONS("/*path", func(c *gin.Context) {
c.Status(http.StatusOK)
})

v2 := router.Group(basePath)
{
blobs := v2.Group("/blobs")
Expand Down Expand Up @@ -256,16 +280,6 @@ func (s *ServerV2) Start() error {
logger.WithSkipPath([]string{"/"}),
))

config := cors.DefaultConfig()
config.AllowOrigins = s.allowOrigins
config.AllowCredentials = true
config.AllowMethods = []string{"GET", "POST", "HEAD", "OPTIONS"}

if s.serverMode != gin.ReleaseMode {
config.AllowOrigins = []string{"*"}
}
router.Use(cors.New(config))

srv := &http.Server{
Addr: s.socketAddr,
Handler: router,
Expand Down

0 comments on commit fefad3a

Please sign in to comment.