Skip to content
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

[Feature Request] Support setting APPLICATION_ROOT Flask config #1952

Open
bonnerl opened this issue Nov 28, 2023 · 5 comments
Open

[Feature Request] Support setting APPLICATION_ROOT Flask config #1952

bonnerl opened this issue Nov 28, 2023 · 5 comments

Comments

@bonnerl
Copy link

bonnerl commented Nov 28, 2023

Problem

I want to use a single NGINX instance and domain to proxy to several backend services with SimpleLogin being one of the backends under a dedicated folder, for example, I'd like to have example.com/SimpleLogin/Dashboard serve Simple Login and example.com/HelloWorld/ to serve my hello world app. Currently SimpleLogin doesn't play nice with this because it will not use the /SimpleLogin/ prefix when generating URLs.

Proposed Solution

APPLICATION_ROOT
Inform the application what path it is mounted under by the application / web server. This is used for generating URLs outside the context of a request

https://flask.palletsprojects.com/en/3.0.x/config/#APPLICATION_ROOT

Update Simple Login to support an optional APPLICATION_ROOT setting in simplelogin.env, and copy the value (when given) to Flask's APPLICATION_ROOT config. Flask will then use APPLICATION_ROOT to build URLs.

@MuhammadNaeem42

This comment was marked as abuse.

@muxo771
Copy link

muxo771 commented Dec 2, 2023

Is this the same for the 4.6.x beta?
Seems to have no effect.

@bonnerl
Copy link
Author

bonnerl commented Dec 4, 2023

@muxo771 SimpleLogin doesn't currently support APPLICATION_ROOT for 3.x or 4.6.x.

@leor-gh
Copy link

leor-gh commented May 6, 2024

I had the same use case requirement to host SImpleLogin not under web server ROOT but another path. I spent a few hours yesterday afternoon to fix the code and achieved it. And today I stumbled upon this issue.

Disclaimer I have not done any serious Python programming before, other than being aware of its syntax. I know even less about Flask (didn't hear about it before), and what I did may not be the "right" way.

APPLICATION_ROOT is a Flask thing, but it seems to be related to middle dispatcher, which SL does not use. Adding it may be quite a task.

What I did was simply adjusting the url_prefix (adding the prefix I want) when registering the blueprints in server.py (there are 10 blueprints) and prepending the prefix when adding the routes for the app (e.g. /favicon.ico). Then all the generated URLs will have the correct /path prefixes. I also changed the default "static" path to /path/static to take care of serving the static files.

The remaining issues are the HTML templates have mixed references to /static and /api Some places are hard-coded with /static while some use url_for('static'). The latter generates the correct path. Same goes for the /api path.

Then in nginx.conf, I pass the /path prefix from the URL to proxy_pass so SL receives the expected full path.

Once that is all done, SL works as expected. Only a few "routes" are not changed, but they are related to functionality I don't care/need, like OpenID, Paddle, Coinbase, etc.

Hope this helps.

@leor-gh
Copy link

leor-gh commented May 7, 2024

There is a thread on stackoverflow discussing the same issue: (https://stackoverflow.com/questions/67737069/how-to-host-a-flask-app-on-a-subfolder-url-prefix-with-nginx/75123044)

One solution seems to be simply as adding a couple new imports and then APPLICATION_ROOT should work as intended.

Another one is adding a new argument to ProxyFix in server.js, which SL is already using. In nginx configuration, be sure to add the trailing / to proxy_pass so that it will remove the prefix when passing the URL to the server. SL can then process all the URLs correctly.

On generating URL, Flask will add the prefix as passed in via the X-Forwarded-Prefix to all generated URLs. This takes care of most URLs from the application.

However, as mentioned, quite a few files in the templates are hard coded with /static and /api and they need to be changed with using url_for as appropriate so that URLs are generated correctly.

I tested X-Forwarded-Prefix set up and the app is working fine for the functionality I use. My Android client also works with API access once I update the server URL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants