26 lines
793 B
Go
26 lines
793 B
Go
package middleware
|
|
|
|
import "github.com/gin-gonic/gin"
|
|
|
|
func CORSMiddleware() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
c.Writer.Header().Set("Access-Control-Allow-Origin", "http://127.0.0.1:5173")
|
|
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
|
|
c.Writer.Header().Set(
|
|
"Access-Control-Allow-Headers",
|
|
"Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, "+
|
|
"authorization, Authorization, accept, origin, Cache-Control, "+
|
|
"X-Requested-With, Cookie",
|
|
)
|
|
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT, DELETE, PATCH")
|
|
c.Writer.Header().Set("Access-Control-Expose-Headers", "Access-Token, Uid, Authorization")
|
|
|
|
if c.Request.Method == "OPTIONS" {
|
|
c.AbortWithStatus(204)
|
|
return
|
|
}
|
|
|
|
c.Next()
|
|
}
|
|
}
|