markbates / pkger

Embed static files in Go binaries (replacement for gobuffalo/packr)
MIT License
1.19k stars 60 forks source link

filepath string ? (http.ServeFile()) #126

Open ghost opened 3 years ago

ghost commented 3 years ago

I'm sometimes in the situation where a library wants a file that is next to my executable and therefore waits for a string parameter to give it the file. I would like to take advantage of pkger. How can I get a filepath string from a pkger file to give it to functions that are waiting for a file with a string?

Example : https://golang.org/pkg/net/http/#ServeFile

phanirithvij commented 3 years ago

If it's for http servefile I had used it like

    file, err := pkger.Open(h.indexPath)
    if err != nil {
        // handle error .....
        return
    }
    http.FileServer(file).ServeHTTP(w, r)
fjogeleit commented 3 years ago

I had an redirect exception with the solution above, I get it work with:

entrypoint := "/api/public/index.html"

f, err := pkger.Open(entrypoint)
d, err := pkger.Stat(f.Info().Dir)

if err != nil {
    // handle error .....
    return
}

http.ServeContent(w, r, f.Name(), d.ModTime(), f)