Skip to content

Commit ac5804b

Browse files
committed
cors middleware for api
1 parent 0f54f0d commit ac5804b

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

cmd/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func RunApi(cmd *cobra.Command, args []string) {
5252
root := r.Group("/:chainId")
5353
{
5454
root.Use(middleware.Authorization)
55+
root.Use(middleware.Cors)
5556
// wildcard queries
5657
root.GET("/transactions", handlers.GetTransactions)
5758
root.GET("/events", handlers.GetLogs)

internal/middleware/cors.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package middleware
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
)
6+
7+
func Cors(c *gin.Context) {
8+
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
9+
c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
10+
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")
11+
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
12+
13+
if c.Request.Method == "OPTIONS" {
14+
c.AbortWithStatus(200)
15+
return
16+
}
17+
c.Next()
18+
}

0 commit comments

Comments
 (0)