Skip to content

Commit

Permalink
cors middleware for api
Browse files Browse the repository at this point in the history
  • Loading branch information
iuwqyir committed Oct 9, 2024
1 parent 4fe86e4 commit d1a6e07
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions internal/middleware/cors.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit d1a6e07

Please sign in to comment.