markbates / pkger

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

Cannot compile assets #84

Open sundowndev opened 4 years ago

sundowndev commented 4 years ago

Hi, here's my setup

pkger.Include("/client/dist")

    router.StaticFS("/static", pkger.Dir("/client/dist"))

    router.GET("/", func(c *gin.Context) {
        f, _ := pkger.Open("/client/dist/index.html")
        sl, _ := ioutil.ReadAll(f)

        c.Header("Content-Type", "text/html; charset=utf-8")
        c.Writer.WriteHeader(http.StatusOK)
        c.Writer.Write([]byte(sl))
        c.Abort()
    })
$GOPATH/bin/pkger list

github.com/sundowndev/phoneinfoga
 > github.com/sundowndev/phoneinfoga:/client/dist
 > github.com/sundowndev/phoneinfoga:/client/dist/css
 > github.com/sundowndev/phoneinfoga:/client/dist/css/bootstrap-vue.min.css
 > github.com/sundowndev/phoneinfoga:/client/dist/css/bootstrap.min.css
 > github.com/sundowndev/phoneinfoga:/client/dist/icon.svg
 > github.com/sundowndev/phoneinfoga:/client/dist/img
 > github.com/sundowndev/phoneinfoga:/client/dist/img/logo.36eb3a60.svg
 > github.com/sundowndev/phoneinfoga:/client/dist/index.html
 > github.com/sundowndev/phoneinfoga:/client/dist/js
 > github.com/sundowndev/phoneinfoga:/client/dist/js/app.8f4dfe68.js
 > github.com/sundowndev/phoneinfoga:/client/dist/js/app.8f4dfe68.js.map
 > github.com/sundowndev/phoneinfoga:/client/dist/js/chunk-vendors.e2e708b8.js
 > github.com/sundowndev/phoneinfoga:/client/dist/js/chunk-vendors.e2e708b8.js.map
$GOPATH/bin/pkger
go build -o run
./run serve
# assets are not served by the HTTP server :(
# runtime error: invalid memory address or nil pointer dereference

It works fine with go run main.go serve btw

Thanks for the help

xDuck commented 4 years ago

Here is how I serve files, its a little bit tailored to my use case (Single-page angular9 app w/ router). I also change the prefix of where files are stored in pkger and the URL I serve them at.

import (
    "mime"
    "net"
    "net/http"
    "path/filepath"
)

// Starts a server and serves all assets as needed
func startServer() string {
    ln, err := net.Listen("tcp", "127.0.0.1:0")
    if err != nil {
        log.Fatal(err)
    }
    go func() {
        defer ln.Close()

        http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
            path := filepath.Join(externalWebPrefix, r.URL.Path)
            if f, err := pkger.Open(path); err != nil {
                // Serve index.html as default and let angular router handle it
                w.Header().Add("Content-Type", mime.TypeByExtension(".html"))
                f, err = pkger.Open(defaultWebPage)
                if err != nil {
                    log.Println(err)
                    return
                }
                io.Copy(w, f)
                f.Close()
            } else {
                w.Header().Add("Content-Type", mime.TypeByExtension(filepath.Ext(path)))
                io.Copy(w, f)
                f.Close()
            }
        })
        log.Fatal(http.Serve(ln, nil))
    }()
    return "http://" + ln.Addr().String()
}
c9s commented 3 years ago

I have the same issue with gin

c9s commented 3 years ago

I have a exported next js app, which I wanted to be mounted from /, however, wildcard conflict issue happens

r.StaticFS("/", pkger.Dir("/frontend/out"))