mirror of
https://github.com/coaidev/coai.git
synced 2025-05-20 05:20:15 +09:00
35 lines
879 B
Go
35 lines
879 B
Go
package middleware
|
|
|
|
import (
|
|
"chat/utils"
|
|
"github.com/gin-gonic/gin"
|
|
"net/http"
|
|
)
|
|
|
|
var allowedOrigins = []string{
|
|
"https://fystart.cn",
|
|
"https://www.fystart.cn",
|
|
"https://deeptrain.net",
|
|
"https://www.deeptrain.net",
|
|
"http://localhost",
|
|
}
|
|
|
|
func CORSMiddleware() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
origin := c.Request.Header.Get("Origin")
|
|
if utils.Contains(origin, allowedOrigins) {
|
|
c.Writer.Header().Set("Access-Control-Allow-Origin", origin)
|
|
c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
|
|
c.Writer.Header().Set("Access-Control-Allow-Headers", "Origin, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
|
|
|
|
if c.Request.Method == "OPTIONS" {
|
|
c.Writer.Header().Set("Access-Control-Max-Age", "3600")
|
|
c.AbortWithStatus(http.StatusOK)
|
|
return
|
|
}
|
|
}
|
|
|
|
c.Next()
|
|
}
|
|
}
|