添加跨域支持
continuous-integration/drone/push Build is passing Details

main
Kelvin 3 years ago
parent eae4e78f39
commit acaaa1eddb

@ -687,6 +687,24 @@ func checkKeyMid() gin.HandlerFunc {
}
}
func Cors() gin.HandlerFunc {
return func(c *gin.Context) {
method := c.Request.Method
origin := c.Request.Header.Get("Origin")
if origin != "" {
c.Header("Access-Control-Allow-Origin", "*") // 可将将 * 替换为指定的域名
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
c.Header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization")
c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Cache-Control, Content-Language, Content-Type")
c.Header("Access-Control-Allow-Credentials", "true")
}
if method == "OPTIONS" {
c.AbortWithStatus(http.StatusNoContent)
}
c.Next()
}
}
func main() {
// 禁用控制台颜色,将日志写入文件时不需要控制台颜色。
@ -703,6 +721,8 @@ func main() {
r := gin.Default()
r.Use(Cors())
r.GET("/dashboard/billing/credit_grants", checkKeyMid(), balance)
r.GET("/v1/models", handleGetModels)

Loading…
Cancel
Save