I generally use /static/* for our static file hosting (when we host them via echo) and I usually have embedded FS for these files. This works fine usually but today something weird happened.
I changed the path to, for example, /x/static/* with everything else being the exact same and the static handler raises a 404 for the files. If I remove the /x it works just fine again.
The logs show the path is correct, the only difference is the leading /x .
Checklist
[x] Dependencies installed
[x] No typos
[x] Searched existing issues and docs
Expected behaviour
Static files are served regardless of static url path
Actual behaviour
Doesn't actually serve regardless of path
Steps to reproduce
Create an embed FS containing static files and a http FileServer to handle them. Wrap them in echo and watch them work (or not)
Working code to debug
// embed.go
var (
//go:embed static
StaticFS embed.FS
)
// main.go (static files served correctly)
package main
func main() {
e := echo.New()
staticHandler := http.FileServer(http.FS(gohome.StaticFS))
d := fmt.Sprintf("/static/*", config.StaticURL)
e.GET(d, echo.WrapHandler(staticHandler))
e.Start("127.0.0.1:8000")
}
OK this was my error/misunderstanding. I didn't realize that http.FileServer depends on request PATH to match file system path. The solution was to use http.StripPrefix.
Issue Description
I generally use
/static/*
for our static file hosting (when we host them via echo) and I usually have embedded FS for these files. This works fine usually but today something weird happened.I changed the path to, for example,
/x/static/*
with everything else being the exact same and the static handler raises a 404 for the files. If I remove the/x
it works just fine again.The logs show the path is correct, the only difference is the leading
/x
.Checklist
Expected behaviour
Static files are served regardless of static url path
Actual behaviour
Doesn't actually serve regardless of path
Steps to reproduce
Create an embed FS containing static files and a http FileServer to handle them. Wrap them in echo and watch them work (or not)
Working code to debug
// embed.go
// main.go (static files served correctly)
// ALT main.go (only 404's on static files)
Version/commit
May be user error on my side but I can't find where that error is.