Skip to content

Commit

Permalink
Merge pull request #33 from ycvk/update_20240705
Browse files Browse the repository at this point in the history
update Cross-origin issue
  • Loading branch information
ycvk committed Jul 5, 2024
2 parents 37d7b82 + 4dbf0f0 commit c81276e
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions web/deeplx_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,28 @@ func (d *DeepLXHandler) Translate(c *gin.Context) {
}

func (d *DeepLXHandler) RegisterRoutes(engine *gin.Engine) {
engine.Use(Cors())
engine.POST(d.routePath, d.Translate)
}

// Cors 跨域中间件
func Cors() gin.HandlerFunc {
return func(c *gin.Context) {
origin := c.Request.Header.Get("Origin")
if origin != "" {
c.Header("Access-Control-Allow-Origin", origin)
c.Header("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS")
c.Header("Access-Control-Allow-Headers", "Origin, Content-Type, Accept, Authorization, X-Requested-With")
c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers")
c.Header("Access-Control-Max-Age", "86400") // 24 hours
c.Header("Access-Control-Allow-Credentials", "true")
}

if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(http.StatusNoContent)
return
}

c.Next()
}
}

0 comments on commit c81276e

Please sign in to comment.