Skip to content
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

LocalDir not working as shown in the examples. #846

Open
voxeljorge opened this issue Jul 21, 2023 · 6 comments
Open

LocalDir not working as shown in the examples. #846

voxeljorge opened this issue Jul 21, 2023 · 6 comments

Comments

@voxeljorge
Copy link

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.
Screenshot 2023-07-20 at 11 37 58 PM

@maxence-charriere
Copy link
Owner

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!

@voxeljorge
Copy link
Author

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

@voxeljorge
Copy link
Author

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.

@voxeljorge
Copy link
Author

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
Screenshot 2023-07-21 at 12 11 36 AM

@voxeljorge
Copy link
Author

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)
	}
}

@oderwat
Copy link
Sponsor Contributor

oderwat commented Jul 21, 2023

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants