Difegue / LANraragi

Web application for archival and reading of manga/doujinshi. Lightweight and Docker-ready for NAS/servers.
https://lrr.tvc-16.science
MIT License
2.18k stars 153 forks source link

Add base URL feature #1052

Closed alethiophile closed 1 month ago

alethiophile commented 1 month ago

This is toward adding a base URL feature, as discussed in https://github.com/Difegue/LANraragi/issues/363.

This works as follows: you can configure a reverse proxy server to forward traffic to LANraragi only under a given path. The proxy must be configured to strip the path prefix from the forwarded requests (e.g. in Caddy this is the handle_path directive, as opposed to handle). Once this is configured, LANraragi can be viewed as usual using subdirectories of that path. E.g. if a top-level deployment had the URL manga.example.com, a subdirectory deployment might have the URL example.com/manga. LANraragi can be used as normal, with all its internal URLs rewritten to be under the /manga subdirectory.

Internally, this works as follows: the reverse proxy strips the path prefix from requests as they pass through. Thus, LANraragi still sees ordinary requests to /api, /js, etc. However, all internal links generated by LANraragi are generated relative to the new path prefix. The user will click a link to /manga/reader, which will be processed into /reader by the reverse proxy. The main server-side system for this is the Mojo url_for helper (https://docs.mojolicious.org/Mojolicious/Controller#url_for). url_for constructs URLs relative to the incoming request URL's base member. Thus, when a base URL is configured, the before_dispatch hook alters the URL base to include that path prefix. This makes all URLs generated via url_for include it.

For the Javascript frontend, the base URL will be passed to the frontend via code in the template. Then, if configured, all API calls will be rewritten to include the base URL. This is not yet finished as I submit this draft.

alethiophile commented 1 month ago

The updates to the Javascript frontend and the test suite are now complete. My local testing shows this is working.

To use the feature, configure your reverse proxy appropriately, then set the variable base_url_path in lrr.conf. The base_url_path value is an empty string by default; this maintains the old behavior. If it is set, it must be a valid URL path fragment, beginning with a slash but with no trailing slash.

It also remains to update the documentation, since this is a user-visible feature.

alethiophile commented 1 month ago

Regarding documentation, I'm happy to write a docs update. However, I can't immediately find where the documentation source is. Is it hiding in some subdirectory I'm missing?

Difegue commented 1 month ago

The docs are hidden in https://github.com/Difegue/LANraragi/tree/dev/tools/Documentation -- Rest of the PR looks good to me as-is.

alethiophile commented 1 month ago

I've added some docs. I put it alongside the reverse proxy configuration, since it's tied in with that system and that's where I looked when I was trying to find this a few weeks ago.

Difegue commented 1 month ago

Makes sense to me. Docs looks good, I'll be merging this.
Thanks again!

holopin-bot[bot] commented 1 month ago

Congratulations @alethiophile, the maintainer of this repository has issued you a holobyte! Here it is: https://holopin.io/holobyte/cm0060y0803220clb1p38dtoy

This badge can only be claimed by you, so make sure that your GitHub account is linked to your Holopin account. You can manage those preferences here: https://holopin.io/account. Or if you're new to Holopin, you can simply sign up with GitHub, which will do the trick!