rio-labs / rio

WebApps in pure Python. No JavaScript, HTML and CSS needed
https://rio.dev
Apache License 2.0
1.89k stars 65 forks source link

[Feature Request]Support configuration of a "root-path" for entire project #129

Open andrelcm opened 3 months ago

andrelcm commented 3 months ago

Description

Currently, as far as I know, the default configuration of root path for web apps is / and that cannot be changed. So, for instance, we can start an app that answer requests on http://localhost:5000/ but not on http://localhost:5000/myapp/ If I am correct, the lack of this feature raises lots of problems with proxies, like nginx, when you run it in a subfolder. Although it is possible to configure nginx to strip the path before contacting the upstream, for some reason, that does not work with a rio app.

Suggested Solution

I'd suggest two possible solutions (both could apply simultaneously): provide a "root path" configuration option and/or use only relative paths in entire project.

Alternatives

I am not an expert in rio or related projects, but I tried setting somehow the root path option for uvicorn without any luck.

Additional Context

I have a nginx with a subfolder "/myapp/" that redirects to the app running in "http://upstream:5000/". Without any further configuration, an access to http(s)://mynginx/myapp/ won't work, because it will call /myapp/ on upstream. Striping the path in nginx could work, and it almost worked on calling / , but the page loaded requests resources on /rio (in other words, from the root path), that does not exist on nginx. sub_filter does not solve the problem because it cannot catch some strings inside javascript. Mapping /rio to the upstream, despite beeing a poor solution, still does not work. However, even in this scenario, all resources are correctly loaded by the browser, but the main page complains that the address does not exist.

Related Issues/Pull Requests

No response

mad-moo commented 3 months ago

Hey!

To the best of my knowledge all paths in Rio are relative, so this is supposed to work. I'll try to reproduce this and get back to you. Thank you for the report!

EDIT: Just to be clear, it should work behind nginx. I'm not a fan of configuring things twice, so I'd prefer all configuration to happen in nginx, and Rio has function correctly regardless of path.

mad-moo commented 3 months ago

I can reproduce this

mad-moo commented 2 months ago

4c667a2ba027f7acae4958ced2e499853a855c38 adds initial support for custom base URLs. This has required far more changes than anticipated and as such needs thorough testing before calling it finished.

@andrelcm if you run the development version please give it a shot and let me know how it goes. We'll also do more testing over the coming days.

The easiest way to set the base-url is using rio run:

python -m rio run --base-url "http://localhost/app"

The as_fastapi and run_as_web_server methods have also gotten a new parameter that works the same way.

andrelcm commented 2 months ago

Hi,

Thank you very much for the effort and so quick response. I couple of questions, though. I'm not an expert yet in python/rio, so... sorry if this is too obvious, but how can the development version? Should I get it directly from github?

Thanks in advance.

mad-moo commented 2 months ago

Yeah, since this isn't in any published release yet, you need to install directly from the source. Our Contributing Guidelines have a section on how to build Rio locally. You can skip the forking part of course.

If you want to keep it simple you can also just wait for the next pre-release and then install from pypi. We'll ship this code soon (albeit marked as experimental) so we can get real-world feedback.

mad-moo commented 2 months ago

Hey @andrelcm !

A new pre-release is out, and with that experimental support for the base URL. You can easily get it from PyPI:

pip install --upgrade --pre rio-ui

(Note the --pre. Without that flag pip ignores pre-releases like this one)