go-app icon indicating copy to clipboard operation
go-app copied to clipboard

Run app with an URL prefix

Open qin-nz opened this issue 3 years ago • 5 comments

Hi everyone, I have an http reverse proxy (e.g. nginx) before the go-app server. It can forward a fixed prefix (e.g. mygoapp) and only forward this prefix to my server.

That means only request with prefix /mygoapp can be handled.

For example,

  • home page should be http://example.com/mygoapp
  • path http://example.com/web/app.wasm should be http://example.com/mygoapp/web/app.wasm.

It's possible to run go-app in this environment ?

@maxence-charriere I tried this (modified on example in README.md), and open http://example.com/mygoapp, but got 404 page not found

	http.Handle("/mygoapp", http.StripPrefix("/mygoapp", &app.Handler{
		Name:        "Hello",
		Description: "An Hello World! example",
	}))

qin-nz avatar Nov 15 '21 01:11 qin-nz

I m not sure to understand what you are asking, Both path should be possible to use. The second one should be the natural one.

maxence-charriere avatar Nov 15 '21 02:11 maxence-charriere

I am experiencing similar problems. I want to create a jam stack using go-app and Vite.js, and the app.wasm file keeps being requested through the /web/app.wasm path.

https://go-app.dev/static-resources#setup-local-web-directory

@maxence-charriere I want to allow the user to set the path of the app.wasm file. /web/ Can I change it without being fixed to the directory?

YankeeTube avatar Nov 17 '21 13:11 YankeeTube

I would be interested to hear if either @qin-nz or @YankeeTube figured this out. I, too, am trying to run a go-app behind a reverse proxy.

I got almost there by setting my handler's Resources field to app.CustomProvider(rootDir, prefix), where rootDir is the path to my web files and prefix is "mygoapp" from "http://example.com/mygoapp/...". I found adding the "mygoapp/" prefix to the go-app/h.cachedPWAResources.Set() calls in [email protected]/pkg/app/http.go allowed the application to serve app.js, app.css, and wasm_exec.js.*

But app.wasm is the sticking point. No matter what I do I cannot figure out how to serve that file when the HTTP GETs come prefixed with /mygoapp.

.* Obviously a kludge. I am searching for the correct solution.

flyn-org avatar Apr 03 '24 03:04 flyn-org

I figured this out, and I now have my application running behind a lighttpd reverse proxy.

  1. Use http.StripPrefix, as indicated in the initial comment of this issue to strip the prefixes /P from the requests sent to the client/pre-render code by way of the handler.
  2. Set the app.Handler's Resources field to the result of app.CustomProvider(rootDir, "/P"). The value of rootDir is the directory containing the web directory, allowing the server to run with its working directory elsewhere. "/P" ensures references sent back to the browser do contain "/P".
  3. Ensure your server-side routes include /P.
  4. Ensure your client-side routes do not include /P.

Getting this to work is a little fragile, but once I figured it out it made sense. I can see why this might seem obvious in hindsight. I might tack a crack at adding this to the documentation if I can figure out a way to clearly describe it.

flyn-org avatar Apr 04 '24 02:04 flyn-org

Related: https://github.com/maxence-charriere/go-app/pull/944.

flyn-org avatar Apr 05 '24 19:04 flyn-org