diff --git a/cors.go b/cors.go new file mode 100644 index 0000000..e9a98e7 --- /dev/null +++ b/cors.go @@ -0,0 +1,21 @@ +package main + +import ( + "github.com/gin-gonic/gin" +) + +// Middleware function to handle CORS requests +func corsMiddleware() gin.HandlerFunc { + return func(c *gin.Context) { + c.Writer.Header().Set("Access-Control-Allow-Origin", "*") + c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, PATCH") + c.Writer.Header().Set("Access-Control-Allow-Headers", "Origin, Authorization, Content-Type") + + if c.Request.Method == "OPTIONS" { + c.AbortWithStatus(200) + return + } + + c.Next() + } +} diff --git a/main.go b/main.go index 27d8884..7717bcd 100644 --- a/main.go +++ b/main.go @@ -89,6 +89,9 @@ func main() { }) }) + // CORS handler + engine.Use(corsMiddleware()) + // get authorization config from db db.Take(&authConfig, "key = ?", "authorization")