Files
openai-api-route/cors.go
2023-09-15 12:48:18 +08:00

32 lines
853 B
Go

package main
import (
"github.com/gin-gonic/gin"
)
// Middleware function to handle CORS requests
func handleCORS(c *gin.Context) {
if c.Request.Method == "OPTIONS" {
header := c.Writer.Header()
header.Set("Access-Control-Allow-Origin", "*")
header.Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, PATCH")
header.Set("Access-Control-Allow-Headers", "Origin, Authorization, Content-Type")
c.AbortWithStatus(200)
return
}
c.Next()
header := c.Writer.Header()
header.Del("Access-Control-Allow-Origin")
header.Del("Access-Control-Allow-Methods")
header.Del("Access-Control-Allow-Headers")
header.Set("Access-Control-Allow-Origin", "*")
header.Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, PATCH")
header.Set("Access-Control-Allow-Headers", "Origin, Authorization, Content-Type")
}