【问题标题】:Integrating Tus Resumable File Upload Protocol with Gin-Gonic CORS issue将 Tus 可恢复文件上传协议与 Gin-Gonic CORS 问题集成
【发布时间】:2021-01-07 03:57:45
【问题描述】:

我在这里查看了与 Gin 和 Tus 的 CORS 问题相关的类似问题;没有解决我目前遇到的问题。

当前的实现通过添加一个小的包装器与标准的 net/http 包一起工作。


// The wrapping function
func enableCors(w *http.ResponseWriter) {
    (*w).Header().Set("Access-Control-Allow-Origin", "*")
}


// Simplified version of the code
composer := tusd.NewStoreComposer()
    store.UseIn(composer)

    handler, err := tusd.NewHandler(tusd.Config{
        BasePath:              "/files/",
        StoreComposer:         composer,
        NotifyCompleteUploads: true,
    })

    if err != nil {
        panic(fmt.Errorf("Unable to create handler %s", err))
    }

    go func() {
        for {
            fmt.Println("Waiting for upload to complete")
            event := <-handler.CompleteUploads
            fmt.Printf("Uploads %s finished\n", event.Upload.Storage)
        }
    }()

    http.Handle("/files/", func(next http.Handler) http.Handler {
        return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
            enableCors(&w)
            next.ServeHTTP(w, r)
        })
    }(http.StripPrefix("/files/", handler)))


    err = http.ListenAndServe(":8080", nil)
    if err != nil {
        panic(fmt.Errorf("unable to listen: %s", err))
    }
    

这是我对杜松子酒的尝试。我将处理程序包装在 gin.WrapH() 中。我添加了默认的 Gin CORS 库中间件,但 cors 错误仍然没有消失。 这不起作用

func TusHandler() http.Handler {

    store := filestore.FileStore{
        Path: "./uploads",
    }

    composer := tusd.NewStoreComposer()
    store.UseIn(composer)

    handler, err := tusd.NewHandler(tusd.Config{
        BasePath:              "/upload/tus/",
        StoreComposer:         composer,
        NotifyCompleteUploads: true,
    })

    if err != nil {
        panic(err) // This is to simplify the code
    }

    return handler

}

// The routing
import "github.com/gin-contrib/cors"

router :=  gin.Default()
router.Use(cors.Default())
router.GET("/upload/tuts", gin.WrapH(uploader.TusHandler()))

这是我的浏览器输出。 When I tried to upload a file pointing to the Gin version

杜松子酒集成一直显示 CORS 错误。这就是我要解决的问题。

【问题讨论】:

  • 您能否更具体地说明您希望社区提供哪些帮助?
  • @Cninroh 感谢您的回复。我正在尝试与 Gin 集成,但 CORS 错误不断出现。我附上的图片显示了错误消息。我正在寻找 CORS 问题的解决方案。
  • 我不是 golang 专家,但由于它没有抱怨来源,而是特别是标头,因此默认情况下它可能禁用了 HEAD 请求。您可能应该使用额外的权限扩展 CORS。 stackoverflow.com/questions/29418478/go-gin-framework-cors
  • 你解决了吗?
  • @medBouzid 不是。我使用 Go http 服务器而不是 NGINX 代理部署了应用程序。它就是这样工作的。

标签: go go-gin tus


【解决方案1】:

tus.io 正在向服务器发送一堆标头,因此您需要将这些标头添加到您的 cors 配置中。错误消息是不允许使用名为 tus-resumable 的标头,您需要将此标头与 tus.io 正在发送的其他标头一起添加。并暴露一些 header 以便 tus-js-client 可以读取它。

router.Use(cors.New(cors.Config{
        AllowAllOrigins: true,
        // AllowOrigins:  []string{"http://example.com"},
        AllowMethods:  []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"},
        AllowHeaders:  []string{"Authorization", "X-Requested-With", "X-Request-ID", "X-HTTP-Method-Override", "Upload-Length", "Upload-Offset", "Tus-Resumable", "Upload-Metadata", "Upload-Defer-Length", "Upload-Concat", "User-Agent", "Referrer", "Origin", "Content-Type", "Content-Length"},
        ExposeHeaders: []string{"Upload-Offset", "Location", "Upload-Length", "Tus-Version", "Tus-Resumable", "Tus-Max-Size", "Tus-Extension", "Upload-Metadata", "Upload-Defer-Length", "Upload-Concat", "Location", "Upload-Offset", "Upload-Length"},
    }))

另外,如果您已经有一个正在运行的应用程序,您可以使用 NewUnroutedHandler 代替 NewHandler。

handler := dTusHandler()
router.POST("/files/", gin.WrapF(handler.PostFile))
router.HEAD("/files/:id", gin.WrapF(handler.HeadFile))
router.PATCH("/files/:id", gin.WrapF(handler.PatchFile))
router.GET("/files/:id", gin.WrapF(handler.GetFile))

这里是 dTusHandler 函数:

func dTusHandler() *tusd.UnroutedHandler {
    store := filestore.FileStore{
        Path: "./uploads",
    }

    composer := tusd.NewStoreComposer()
    store.UseIn(composer)

    h, err := tusd.NewUnroutedHandler(tusd.Config{
        BasePath:              "/files/",
        StoreComposer:         composer,
        NotifyCompleteUploads: true,
    })

    if err != nil {
        panic(fmt.Errorf("Unable to create handler: %s", err))
    }

    go func() {
        for {
            event := <-h.CompleteUploads
            fmt.Printf("Upload %s finished\n", event.Upload.ID)
        }
    }()

    return h

}

【讨论】:

    猜你喜欢
    • 2019-01-14
    • 1970-01-01
    • 2017-08-09
    • 2022-08-22
    • 2019-05-18
    • 1970-01-01
    • 1970-01-01
    • 2015-10-26
    • 2017-08-15
    相关资源
    最近更新 更多