diff --git a/cmd/api.go b/cmd/api.go index 0fc3be5..b66575b 100644 --- a/cmd/api.go +++ b/cmd/api.go @@ -52,6 +52,7 @@ func RunApi(cmd *cobra.Command, args []string) { root := r.Group("/:chainId") { root.Use(middleware.Authorization) + root.Use(middleware.Cors) // wildcard queries root.GET("/transactions", handlers.GetTransactions) root.GET("/events", handlers.GetLogs) diff --git a/internal/middleware/cors.go b/internal/middleware/cors.go new file mode 100644 index 0000000..4235d65 --- /dev/null +++ b/internal/middleware/cors.go @@ -0,0 +1,18 @@ +package middleware + +import ( + "github.com/gin-gonic/gin" +) + +func Cors(c *gin.Context) { + c.Writer.Header().Set("Access-Control-Allow-Origin", "*") + c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS") + c.Writer.Header().Set("Access-Control-Allow-Headers", "Origin, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With") + c.Writer.Header().Set("Access-Control-Allow-Credentials", "true") + + if c.Request.Method == "OPTIONS" { + c.AbortWithStatus(200) + return + } + c.Next() +}