labstack / echo

High performance, minimalist Go web framework
https://echo.labstack.com
MIT License
29.62k stars 2.22k forks source link

Weirdness with wrapped handler and embed FS (static files) #2566

Closed petersanchez closed 9 months ago

petersanchez commented 9 months ago

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

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")
}
2023-12-27T18:07:10-06:00 method=GET, uri=/static/css/chota.min.css?8546540, status=200 remote_ip=127.0.0.1
app="gohome 8546540"
2023-12-27T18:07:10-06:00 method=GET, uri=/static/css/style.css?8546540, status=200 remote_ip=127.0.0.1 app="gohome 8546540"

// ALT main.go (only 404's on static files)

package main

func main() {
    e := echo.New()
    staticHandler := http.FileServer(http.FS(gohome.StaticFS))
    d := fmt.Sprintf("/yz/static/*", config.StaticURL)
    e.GET(d, echo.WrapHandler(staticHandler))
    e.Start("127.0.0.1:8000")
}
2023-12-27T18:07:38-06:00 method=GET, uri=/yz/static/css/chota.min.css?8546540, status=404 remote_ip=127.0.0.1 app="gohome 8546540"
2023-12-27T18:07:38-06:00 method=GET, uri=/yz/static/css/style.css?8546540, status=404 remote_ip=127.0.0.1 app="gohome 8546540"

Version/commit

$ grep echo go.sum
github.com/labstack/echo/v4 v4.11.3 h1:Upyu3olaqSHkCjs1EJJwQ3WId8b8b1hxbogyommKktM=

May be user error on my side but I can't find where that error is.

petersanchez commented 9 months ago

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.