lovasoa / SQLpage

Fast SQL-only data application builder. Automatically build a UI on top of SQL queries.
https://sql.datapage.app
MIT License
1.56k stars 89 forks source link

Add URL prefix config option #279

Closed dwhitemv25 closed 4 months ago

dwhitemv25 commented 6 months ago

SQLpage needs a configuration option to specify a path prefix on internally-generated URLs (/sqlpage, /apexcharts, /tabler-icons*, etc.).

Currently when SQLpage needs to reference additional resources in generated HTML, like JS or CSS, it generates a random filename. These URLs always reference the root, for example /sqlpage.fa7fbf3ecfa4e3dd.js or /apexcharts.cf35a7dfce60cfa4.js. This causes problems when trying to run SQLpage through a reverse proxy like nginx, and the SQLpage pages are in a subdirectory. The special names have to be rewritten to get them to route to the reverse proxy location. This makes it impossible to have more than one SQLpage instance in one Web server instance, and/or neatly attach a SQLpage instance to an existing website.

The solution is to provide a configuration option to set a prefix on these special pages, i.e., set the prefix to /sp and the generated pages reference /sp/sqlpage.fa7fbf3ecfa4e3dd.js. When SQLpage processes the proxied request, it strips off the prefix, or matches it against a known one.

For example, here is my current SQLpage reverse proxy configuration, with the SQLpage pages under www.example.com/sp:

        location /sqlpage { rewrite ^(/sqlpage.*) /sp/$1 last; }
        location /apexcharts { rewrite ^(/apexcharts.*) /sp/$1 last; }
        location /tabler-icons { rewrite ^(/tabler-icons.*) /sp/$1 last; }
        location /sp/ {
          proxy_pass http://127.0.0.1:8080/;
          proxy_set_header Host $host;
        }

The first 3 lines (with the rewrite rule) are required because of the lack of a prefix configuration. With the prefix configuration ability, those lines are not needed as nginx can route them just using the one location rule.

lovasoa commented 6 months ago

Hello! Such a configuration option would indeed be useful! Are you interested in trying to make a small pull request? The files to change are:

https://github.com/lovasoa/SQLpage/blob/main/configuration.md https://github.com/lovasoa/SQLpage/blob/main/src/app_config.rs https://github.com/lovasoa/SQLpage/blob/main/src/webserver/static_content.rs https://github.com/lovasoa/SQLpage/blob/main/src/template_helpers.rs#L135

dwhitemv25 commented 6 months ago

I'll see what I can do, I've never used Rust before so it'll take a bit to get a dev environment set up. In the meantime I looked into actix-web and it has explicit support for prefix configuration, so it should only be a few lines of code to set up the request router. The rest is making sure the templates are using the appropriate calls to generate the self-references.

dwhitemv25 commented 6 months ago

For the record, I looked at this and it isn't something I'm going to work on. I got the request router & the configuration verb setup (I think, couldn't test it) but the ServiceRequest needs to be pushed down all the way through to the template code and that entails either changing all the function signatures or some Rust magic. On top of that, I couldn't definitively find what generates the randomized filenames.

lovasoa commented 6 months ago

It's great to have looked at it anyway! Can you open a draft pr with what you have so that someone can pick it up where you left off later?

lovasoa commented 4 months ago

@vlasky @dwhitemv25 : The patch by @djyotta introducing sub-path hosting is now available on lovasoa/sqlpage:main and there is now a guide to reverse proxying on the website.