-
Notifications
You must be signed in to change notification settings - Fork 0
/
cors_middleware.go
22 lines (18 loc) · 981 Bytes
/
cors_middleware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package middlewares
import "github.com/gin-gonic/gin"
// CORSMiddleware is a middleware function that adds Cross-Origin Resource Sharing (CORS) headers to the response.
// It allows requests from any origin and includes common headers and methods used in HTTP requests.
// If the request method is OPTIONS, it responds with a 204 No Content status code.
// This middleware should be used to enable CORS support in your application.
func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With, x-api-key, Content-Language, X-Country")
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT, DELETE, PATCH")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}
c.Next()
}
}