【问题标题】:static files not served correctly when using wildcard使用通配符时无法正确提供静态文件
【发布时间】:2019-10-16 22:03:49
【问题描述】:

使用GOA,我定义了一个使用通配符提供静态文件的服务(如the documentation 中所述):

var _ = Service("static", func() { 
    Files("/static/*filepath", "./static/")
})

但是当我运行服务时,端点总是检索它在 ./static/ 目录中找到的所有内容,它似乎没有考虑到通配符部分。

例如,如果我有 ./static/uploads/file1.jpg 并且我请求 localhost/static/uploads/file1.jpglocalhost/ static/anything ,然后服务检索以下内容:

<pre>
<a href="uploads/">uploads/</a>
</pre>

深入代码,我认为问题出在生成的/gen/http/static/server/server.go文件中:

// Mount configures the mux to serve the static endpoints.
func Mount(mux goahttp.Muxer, h *Server) {
    MountCORSHandler(mux, h.CORS)
    MountStatic(mux, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        http.ServeFile(w, r, "./static/")
    }))
}

// MountStatic configures the mux to serve GET request made to
// "/static/*filepath".
func MountStatic(mux goahttp.Muxer, h http.Handler) {
    mux.Handle("GET", "/static/*filepath", handleStaticOrigin(h).ServeHTTP)
}

就我所见,生成的代码无论如何都服务于我们作为基本路径传递的内容,它根本没有考虑我们是否配置了通配符(它只使用它来匹配请求,而不是自定义我们将提供的文件)。

我相信这在 v2 中工作正常,我在迁移到 v3 的过程中发现了这个问题。

正如我所说,这似乎是 GOA 中的一个错误,但也许我在这里遗漏了一些东西。我在 repo 中创建了一个 issue 以获得更多信息 (#2321)

【问题讨论】:

    标签: go goa


    【解决方案1】:

    根据 Github 问题 (#2321) 中的答案,文档中似乎存在错误,我们应该在模式中使用花括号:

    感谢您的报告,文档中有错字,设计中的路径需要改为 /static/{*filepath} (用大括号包围通配符)。

    【讨论】:

      猜你喜欢
      • 2018-11-19
      • 1970-01-01
      • 2016-10-13
      • 2022-11-10
      • 2017-05-10
      • 2021-06-03
      • 1970-01-01
      • 1970-01-01
      • 2011-07-22
      相关资源
      最近更新 更多