Files
lambdaiot-core/internal/middleware/cors.go
2026-02-07 23:34:26 +01:00

31 lines
692 B
Go

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