CORS frontend hotfix
This commit is contained in:
@@ -114,6 +114,7 @@ func main() {
|
||||
// Gin setup
|
||||
r := gin.New()
|
||||
r.Use(gin.Recovery())
|
||||
r.Use(middleware.CORS())
|
||||
r.Use(middleware.GinLogger())
|
||||
|
||||
// Public routes
|
||||
|
||||
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