httprouter icon indicating copy to clipboard operation
httprouter copied to clipboard

Fails with "embed" package

Open pnsvk opened this issue 2 years ago • 1 comments

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..

pnsvk avatar Aug 23 '22 23:08 pnsvk

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 :)

Sijisu avatar Sep 06 '22 12:09 Sijisu