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

Automatically use the ASGI scope root_path field #372

Closed
waweber opened this issue Jun 7, 2023 · 3 comments
Closed

Automatically use the ASGI scope root_path field #372

waweber opened this issue Jun 7, 2023 · 3 comments

Comments

@waweber
Copy link

waweber commented Jun 7, 2023

🚀 Feature Request

ASGI servers sitting behind a reverse proxy often have their routes served at a URL prefix.

For example, this route:

@app.route("/example")
def example():
    return "Example"

Might be served at https://example.com/app/example, with a server like nginx rewriting requests for /app/example to /example before forwarding them to the ASGI application.

The application doesn't need to know about this, everything works just fine on the routing side. But if you use something like get_absolute_url_to_path() it would return https://example.com/example, which is incorrect.

The Request object has a base_path property that is taken into account, and the correct URL is returned when it is set to "/app", but nothing currently seems to set this value automatically.

The ASGI scope defines a field called "root_path" which contains the correct value to use for Request.base_path. It is configured/sent by the ASGI server (like uvicorn --root-path /app ...) so this value could very easily be used to automatically set the base_path without any specific configuration of the application.

In the meantime, I am using a very simple middleware to handle this, but it would be nice if this was included automatically:

async def set_base_path(request, handler):
    """Middleware to set base_path from the ASGI scope's root_path field."""
    request.base_path = request.scope.get("root_path", "")
    return await handler(request)
@RobertoPrevato
Copy link
Member

Hi @waweber,
Thank You for your recommendation! You are absolutely right, that is a right thing to do. I probably missed that the ASGI scope included that property, when I was working on that feature (I added the base_path and methods to build the full URL in the same period I worked on the built-in support for OpenID Connect).

RobertoPrevato added a commit that referenced this issue Jun 14, 2023
RobertoPrevato added a commit that referenced this issue Jun 14, 2023
@RobertoPrevato
Copy link
Member

Fixed on main - can be fixed also on v1.

@RobertoPrevato RobertoPrevato self-assigned this Jun 17, 2023
@RobertoPrevato
Copy link
Member

Fixed at 1.2.17.

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

No branches or pull requests

2 participants