This commit is contained in:
Eden Kirin
2024-01-15 23:33:23 +01:00
parent f220d08800
commit 8bc736114c
26 changed files with 20149 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package api
import (
"net/http"
"strings"
"github.com/gin-gonic/gin"
)
@ -35,3 +36,35 @@ func raiseInternalError(c *gin.Context, message string) {
"details": "Internal server error. We will we will fix it!",
})
}
func corsMiddleware() gin.HandlerFunc {
allowHeaders := [12]string{
"Content-Type",
"Content-Length",
"Accept-Encoding",
"X-CSRF-Token",
"Authorization",
"accept",
"origin",
"Cache-Control",
"X-Requested-With",
"x-timezone",
"Access-Control-Allow-Origin",
"Access-Control-Max-Age",
}
return func(c *gin.Context) {
allowOrigin := "*"
c.Writer.Header().Set("Access-Control-Allow-Origin", allowOrigin)
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
c.Writer.Header().Set("Access-Control-Allow-Headers", strings.Join(allowHeaders[:], ", "))
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT, DELETE")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(http.StatusNoContent)
return
}
c.Next()
}
}

View File

@ -96,6 +96,7 @@ func handleGetMachineProducts(dbConn *gorm.DB) gin.HandlerFunc {
func initRouter(dbConn *gorm.DB) *gin.Engine {
gin.SetMode(gin.ReleaseMode)
router := gin.Default()
router.Use(corsMiddleware())
routes := router.Group("/machines")
{