34 lines
700 B
Go
34 lines
700 B
Go
package main
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func CORSMiddleware() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
|
|
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
|
|
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
|
|
c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE")
|
|
|
|
if c.Request.Method == "OPTIONS" {
|
|
c.AbortWithStatus(204)
|
|
return
|
|
}
|
|
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
r := gin.Default()
|
|
|
|
r.Use(CORSMiddleware())
|
|
|
|
r.GET("/api/data", func(c *gin.Context) {
|
|
c.JSON(200, gin.H{"message": "Custom CORS enabled"})
|
|
})
|
|
|
|
r.Run(":9999")
|
|
}
|