New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
embed: FS root directory is always the package directory, while os.DirFS may have another root #43431
Comments
You can do something like this: //go:embed docroot/*
var root embed.FS
// myFS implements fs.FS
type myFS struct {
content embed.FS
}
func (c myFS) Open(name string) (fs.File, error) {
return c.content.Open(path.Join("docroot", name))
} Then: r.Handle("/", http.FileServer(http.FS(myFS{root}))) |
It seems to me that Since it's a new API, I'll let @rsc consider if anything more should be done. |
Thanks for the quick responses. Is there anything against embed.FS implementing |
@changkun solution would work great. I ended up adding a single file inside the docroot:
and importing that. It's not pretty, but works. (as far as I'm concerned this issue can be closed) |
@alicebob Indeed. I noticed the behavior you reported in this thread while I was trying to convert my static website here. I didn't report this simply because I don't know that is the actually expected behavior from these new set of APIs. The design draft is just a draft, as far as I partially skimmed with the ongoing discussions, the current implementation is already apart from the draft design (lack of documentation). For more instance, I'd like to directly embed everything in a repository (and of course exclude the
With the (undocumented) 37588ff, the But unfortunately, build such a simple program can result in the following compile error:
|
Am I misunderstanding or does this use of fs.Sub do what you want? https://github.com/carlmjohnson/exembed/blob/main/web/main.go |
Oh, how cloud I miss the |
@carlmjohnson awesome, yes that solves everything. I misread the output of |
What version of Go are you using (
go version
)?(that's 1.16beta1)
Does this issue reproduce with the latest release?
yes
What did you do?
I want to serve static documents in a webserver using the new
embed
package, but with an option to use local files.https://play.golang.org/p/hyBgSUBUhf3
There is a subdir
./docroot/
with an index.html file:When I use the
os.DirFS()
version (./root -docroot ./docroot
) I get theindex.html
nicely at http://localhost:6602/index.html. If I use theembed
version (./root
) it ends up at http://localhost:6602/docroot/index.htmlWhat did you expect to see?
I expect either none or both to have the
./docroot/
subdir in there. This way I can't usehttp.StripPrefix()
. But maybe I missed something else.What did you see instead?
The embed.FS and os.DirFS seem to behave inconsistently.
If embed.FS implemented fs.SubFS I think I could make this work by using
docroot.Sub("./docroot/")
The text was updated successfully, but these errors were encountered: