CORS frontend hotfix

This commit is contained in:
2026-02-07 23:34:26 +01:00
parent 34ad0dc2f8
commit cff05f46df
2 changed files with 31 additions and 0 deletions

View File

@@ -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

View 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()
}
}