Open sundowndev opened 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()
}
I have the same issue with gin
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"))
Hi, here's my setup
It works fine with
go run main.go serve
btwThanks for the help