-
-
Notifications
You must be signed in to change notification settings - Fork 190
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
Add -base-path flag #762
Add -base-path flag #762
Conversation
This allows self-hosting GoatCounter and reverse proxying it into a subdirectory instead of hosting it on its own subdomain. Fixes arp242#707, arp242#750
Can't we set the Path for cookies? Or am I misunderstanding how that works? With something like patch for zhttp (didn't test/commit):
And then on startup set that zhttp.BasePath from the CLI. This also fixes the error page, and avoids having to create a custom SeeOther() and MovedPermanently(). |
cmd/goatcounter/serve.go
Outdated
-base-path Base path to use in URLs sent back to the client. Default: "/". | ||
You need this if you are reverse proxying GoatCounter into a | ||
subdirectory of your website. This does not affect routing, since | ||
it is assumed the reverse proxy will strip off the base path. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This text can probably be a bit easier to understand:
Path under which GoatCounter is available. Usually GoatCounter runs on its own domain or subdomain ("stats.example.com"), but in some cases it's useful to run GoatCounter under a path ("example.com/stats"), in which case you'll beed to set this to "/stats".
And it should probably update the routing as well, unless that's a ton of effort? I think that would be a lot clearer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that makes sense. I don't think it should be too hard, I'll give it a shot. I agree it's clearer, and anyone in a position to configure their reverse proxy should be able to format the path how they like (i.e. not strip off the prefix).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in which case you'll beed to set this to "/stats".
You copied my "beed" typo 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed :)
- Trim slash from start and end of -base-path - Go back to === in help.js
Oh I think I had a mistaken assumption. I don't know much about cookies. Yes that looks perfect! I can make a PR there too. |
Didn't realize these were templates at first
This is in support of arp242/goatcounter#762.
- Remove redirect.go. Instead rely on arp242/zhttp#6. - Use -bath-path in the router so that it's actually served under that path rather than needing the reverse proxy to strip it.
Ok, I've now made it use -base-path in the router, and removed redirect.go in favor of arp242/zhttp#6. I've tested manually (using a |
How did you test it? If I run it with:
I get just "404 page not found" on http://localhost:9000/ Same if I add -base-path=/ or -base-path=/foo (http://localhost:9000/foo doesn't work). Actually I can't get it to produce anything other than that error 🤔 |
Ah, it's working for me with |
With the below patch the defaults work, and -base-path=/foo kind-of works, but doesn't render any charts.
|
Make sure it's "" for the root, not "/".
While the "./fonts/..." fallback ensures the correct font is loaded, it does give an annoying error in the console which I want to get rid of.
Ok yeah I'm also not seeing the charts. Odd since it is getting the JS files fine and there's no console errors. |
Added a commit to make it work.
and:
Also used for some other things like displaying the active tab. This should just be the regular path without baseURL prepended. |
Previously only http://localhost:9000/foo/ worked, and http://localhost:9000/foo errored.
Thanks! Confirmed I see the charts now too. Also, not sure when this runs but I added a commit to add BASE_PATH here:
|
Okay, I did a bunch of testing, and I couldn't find any further problems, so hopefully everything should be good now. Thanks for your PR! |
Heh I missed quite a few things, thanks for taking care of that. Excited to start using this on my website :) |
This allows self-hosting GoatCounter and reverse proxying it into a subdirectory of a larger website instead of hosting it on its own subdomain. (Fixes #707, #750)
I have implemented it as a
-base-path
flag provided when starting the server. This means when there are multiple sites, they must all use the same base path. An alternative would be to customize it per site, like is done for subdomains. I think this would be more flexible, but we'd have to load the site before ever sending a redirect or emitting a URL in HTML. To implement it that way, I'd need a better idea of what parts of the codebase assume the site is already loaded.I've done some basic testing, and things seem to work. I'm planning to use this for my own website. It's up to you if you want to merge it. There are a few things that would need to be addressed before merging:
Fonts don't work since they're hardcoded to '/fonts/...' in the CSS files.Base
value through the templates is not ideal/cleanest way. I just did whatever seemed easiest to make it work.If you're interested in merging, I can work on those things.