问题描述
我研究了与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/",})
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
解决方法
tus.io 正在向服务器发送一堆标头,因此您需要将这些标头添加到您的 cors 配置中。错误消息表示不允许使用名为 tus-resumable
的标头,您需要将此标头与 tus.io 发送的其他标头一起添加。并公开一些标头,以便 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","Tus-Version","Tus-Max-Size","Tus-Extension","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
}