使用通配符时无法正确提供静态文件

时间:2019-10-16 22:03:49

标签: go goa

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

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

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

例如,如果我有 ./ static / uploads / file1.jpg ,并且我请求 localhost / static / uploads / file1.jpg localhost / 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中的错误,但也许我在这里遗漏了一些东西。我在存储库中创建了一个问题以获取更多信息(#2321

1 个答案:

答案 0 :(得分:0)

按照Github问题(#2321)中的答案,看来文档中有错误,我们应该在模式中使用花括号:

  

谢谢您的报告,文档中有一个错字,设计中的路径应改为/ static / {* filepath}(通配符周围有花括号)。

相关问题