CORS frontend hotfix
This commit is contained in:
30
internal/middleware/cors.go
Normal file
30
internal/middleware/cors.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// CORS adds permissive demo CORS headers for any origin.
|
||||
func CORS() gin.HandlerFunc {
|
||||
allowedMethods := "GET, POST, PUT, DELETE, OPTIONS"
|
||||
allowedHeaders := "Authorization, Content-Type"
|
||||
|
||||
return func(c *gin.Context) {
|
||||
if c.GetHeader("Origin") != "" {
|
||||
headers := c.Writer.Header()
|
||||
headers.Set("Access-Control-Allow-Origin", "*")
|
||||
headers.Set("Access-Control-Allow-Methods", allowedMethods)
|
||||
headers.Set("Access-Control-Allow-Headers", allowedHeaders)
|
||||
}
|
||||
|
||||
if strings.EqualFold(c.Request.Method, http.MethodOptions) {
|
||||
c.AbortWithStatus(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user