go-app
go-app copied to clipboard
LocalDir not working as shown in the examples.
From the documentation it seems LocalDir
is intended to allow you to specify some directory on the local filesystem to serve under web/
for static assets. There is an example for this showing /tmp/web
as an example destination but this does not seem to work. Here's my main.go
:
package main
import (
"log"
"net/http"
"github.com/maxence-charriere/go-app/v9/pkg/app"
)
type hello struct {
app.Compo
}
func (h *hello) Render() app.UI {
return app.H1().Text("Hello World!")
}
func main() {
app.Route("/", &hello{})
app.RunWhenOnBrowser()
http.Handle("/", &app.Handler{
Name: "Hello",
Description: "An Hello World! example",
Resources: app.LocalDir("/tmp/goappweb"),
})
if err := http.ListenAndServe(":8000", nil); err != nil {
log.Fatal(err)
}
}
I ran the following to build the app:
mkdir -p /tmp/goappweb
GOARCH=wasm GOOS=js go build -o /tmp/goappweb/app.wasm .
Here's a screenshot of the result in my browser.
Hello @voxeljorge,
Thank you for bringing this.
To compile the wasm file into your local_dir/web
, you can run the following command:
GOARCH=wasm GOOS=js go build -o /tmp/goappweb/web/app.wasm .
I agree that the documentation could do with more clarity on this matter. I'll work on updating it to include this information. Thank you for pointing this out, and please don't hesitate to reach out if you encounter any more issues or have additional suggestions!
Hm this is what I did, I have compiled app.wasm
and it currently exists in that directory.
jorge@jorge-devbox:~/tmp/go-app-bug$ tree /tmp/goappweb/
/tmp/goappweb/
└── app.wasm
0 directories, 1 file
I think the issue is that somehow /tmp/goappweb
is ending up in the url that the browser is using to fetch static assets rather than /web
Ahh I see now, you mean that I need a web
subdirectory in there ok, I will try that thanks!
In the screenshot it looks like the browser is attempting to load content from locahost:8000/tmp/goappweb
which I'm guessing might be incorrect, but it might be just some weird behavior triggered by the app.wasm missing.
So with this change, I see a few new errors in the web console:
jorge@jorge-devbox:~/tmp/go-app-bug$ tree /tmp/goappweb/
/tmp/goappweb/
└── web
└── app.wasm
1 directory, 1 file
The following change to the original code here seems to get this working:
package main
import (
"log"
"net/http"
"github.com/maxence-charriere/go-app/v9/pkg/app"
)
type hello struct {
app.Compo
}
func (h *hello) Render() app.UI {
return app.H1().Text("Hello World!")
}
type localDir struct {
http.Handler
}
func (l *localDir) Package() string {
return ""
}
func (l *localDir) Static() string {
return ""
}
func (l *localDir) AppWASM() string {
return "/web/app.wasm"
}
func newLocalDir(rootdir string) app.ResourceProvider {
return &localDir{http.StripPrefix("/web", http.FileServer(http.Dir(rootdir)))}
}
func main() {
app.Route("/", &hello{})
app.RunWhenOnBrowser()
http.Handle("/", &app.Handler{
Name: "Hello",
Description: "An Hello World! example",
Resources: newLocalDir("/tmp/goappweb"),
})
if err := http.ListenAndServe(":8000", nil); err != nil {
log.Fatal(err)
}
}
I also never got app.LocalDir()
working for me. Is it broken @maxence-charriere? Writing you own handler should not really be the solution for that?
Made some changes in v10 about Resource. Please reopen if this is still an issue.