【发布时间】: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.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 中的一个错误,但也许我在这里遗漏了一些东西。我在 repo 中创建了一个 issue 以获得更多信息 (#2321)
【问题讨论】: