Initial
This commit is contained in:
@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
@ -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")
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user