httprouter
httprouter copied to clipboard
Fails with "embed" package
An adaptation of the following snippet for httprouter fails with multiple attempts.
import (
"embed"
"io/fs"
"net/http"
)
//go:embed public
var content embed.FS
func handler() http.Handler {
fsys := fs.FS(content)
html, _ := fs.Sub(fsys, "public")
return http.FileServer(http.FS(html))
}
func main() {
mux := http.NewServeMux()
mux.Handle("/", handler())
http.ListenAndServe(":8080", mux)
}
Can you please share a working snippet with httprouter ? or is it even feasible..
This works no problem for me:
//go:embed static
var content embed.FS
mux := httprouter.New()
if static, err := fs.Sub(content, "static"); err == nil {
mux.Handler("GET", "/static/*path", http.StripPrefix("/static", http.FileServer(http.FS(static))))
} else {
panic(err)
}
http.ListenAndServe(":8080", mux)
Would be helpful if you could next time share exactly what are you trying to do and how :)