2023-10-08 15:59:42 +07:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
|
|
|
|
"furtuna-be/internal/common/logger"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func Cors() gin.HandlerFunc {
|
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
|
c.Header("Access-Control-Allow-Origin", "*")
|
2024-07-30 16:35:45 +07:00
|
|
|
c.Header("Access-Control-Allow-Origin", "http://localhost:3000")
|
2023-10-08 15:59:42 +07:00
|
|
|
c.Header("Access-Control-Allow-Credentials", "true")
|
|
|
|
|
c.Header("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, Accept, origin, Referer, Cache-Control, X-Requested-With")
|
|
|
|
|
c.Header("Access-Control-Allow-Methods", "POST,HEAD,PATCH, OPTIONS, GET, PUT, DELETE")
|
|
|
|
|
c.Header("Vary", "Origin")
|
|
|
|
|
|
|
|
|
|
if c.Request.Method == "OPTIONS" {
|
|
|
|
|
c.AbortWithStatus(204)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.Next()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func LogCorsError() gin.HandlerFunc {
|
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
|
c.Next()
|
|
|
|
|
|
|
|
|
|
// check if the request was blocked due to CORS
|
|
|
|
|
if c.Writer.Status() == http.StatusForbidden && c.Writer.Header().Get("Access-Control-Allow-Origin") == "" {
|
|
|
|
|
logger.GetLogger().Error(fmt.Sprintf("CORS error: %s", c.Writer.Header().Get("Access-Control-Allow-Origin")))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|