From 27cd64640f56de5791eff4a5b840b6e75ed49eb9 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Fri, 27 Sep 2024 21:13:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=B7=A8=E5=9F=9F=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/cross.go | 33 +++++++++++++++++++-------------- main.go | 4 ++-- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/handler/cross.go b/handler/cross.go index 176eae5..0aec704 100644 --- a/handler/cross.go +++ b/handler/cross.go @@ -2,26 +2,31 @@ package handler import ( "github.com/gin-gonic/gin" - "net/http" //"net/http" ) // 跨域访问:cross origin resource share func CrosHandler() gin.HandlerFunc { - return func(c *gin.Context) { - c.Writer.Header().Set("Access-Control-Allow-Origin", "*") // 或者设置为具体的源 - c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE") - c.Writer.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Length, X-CSRF-Token, Token, Session, X-Requested-With, Accept, Origin, Host, Connection, Accept-Encoding, Accept-Language, DNT, X-CustomHeader, Keep-Alive, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Pragma, Token, OpenId, OpenToken") - c.Writer.Header().Set("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Cache-Control, Content-Language, Content-Type, Expires, Last-Modified, Pragma, FooBar") - c.Writer.Header().Set("Access-Control-Max-Age", "172800") - // 如果需要发送cookies,请确保设置了具体的源而不是*,并将Allow-Credentials设置为true - // c.Writer.Header().Set("Access-Control-Allow-Credentials", "true") + return func(context *gin.Context) { + //method := context.Request.Method + context.Writer.Header().Set("Access-Control-Allow-Origin", "*") + context.Header("Access-Control-Allow-Origin", "*") // 设置允许访问所有域 + context.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE,UPDATE") + context.Header("Access-Control-Allow-Headers", "Authorization, Content-Length, X-CSRF-Token, Token,session,X_Requested_With,Accept, Origin, Host, Connection, Accept-Encoding, Accept-Language,DNT, X-CustomHeader, Keep-Alive, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Pragma,token,openid,opentoken") + context.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers,Cache-Control,Content-Language,Content-Type,Expires,Last-Modified,Pragma,FooBar") + context.Header("Access-Control-Max-Age", "172800") + context.Header("Access-Control-Allow-Credentials", "false") + context.Set("content-type", "application/json") //设置返回格式是json - if c.Request.Method == "OPTIONS" { - c.AbortWithStatus(http.StatusOK) - return - } + // if method == "OPTIONS" { + // context.JSON(http.StatusOK, gin.H{ + // "code":1, + // "message":"error", + // "data":"request error", + // }) + // } - c.Next() + //处理请求 + context.Next() } } diff --git a/main.go b/main.go index 471a34b..52466a9 100644 --- a/main.go +++ b/main.go @@ -13,8 +13,8 @@ import ( ) func main() { - r := gin.Default() gin.SetMode(gin.ReleaseMode) + r := gin.Default() err := dao.Init() if err != nil { panic("failed to connect database:" + err.Error()) @@ -31,9 +31,9 @@ func main() { handler.SetUpIMGroup(r) // IM handler.SetUpCIDGroup(r) // CID,持续集成、部署 handler.SetUpToolGroup(r) // Tool - r.Run(":8083") // listen and serve on 0.0.0.0:8083 defer dao.Close() defer worker.CloseRedis() + r.Run(":8083") // listen and serve on 0.0.0.0:8083 } func init() { // 创建cid的目录