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

Support mounting application elsewhere than at root path #1731

Closed
Tronic opened this issue Dec 15, 2019 · 7 comments
Closed

Support mounting application elsewhere than at root path #1731

Tronic opened this issue Dec 15, 2019 · 7 comments

Comments

@Tronic
Copy link
Member

Tronic commented Dec 15, 2019

Sanic app might be exposed publicly in a path other than root at a HTTP server that also serves other things on the same domain. E.g. https://company.com/blog/ is proxied to Sanic app at http://localhost:8000/.

Currently Sanic offers no support for that, with url_for resolving to absolute paths pointing at site root.

It would be good to add support e.g. via config.SERVER_NAME that also included the external root path, so that proper URLs would be formed whenever external URLs are requested. The path information is already available but not used if presented on proxy headers req.forwarded, whereas I am not exactly sure of SERVER_NAME handling.

@sjsadowski
Copy link
Contributor

I like this idea.

@ahopkins
Copy link
Member

This seems like a fairly easy one to add.

@ahopkins
Copy link
Member

@ahopkins ahopkins added this to the v21.3 milestone Jan 11, 2021
@ahopkins
Copy link
Member

I finally got back to testing this. I ran the following on:

  • 19.12LTS
  • 20.12LTS
  • master
  • sanic-routing

On all of these branches, this seemed to work fine.

from sanic import Sanic
from sanic.response import text

app = Sanic("app")
app.config.SERVER_NAME = "example.com/api"


@app.route("/foo")
def handler(request):
    url = app.url_for("handler", _external=True)
    return text(f"URL: {url}")

The result:

URL: http://example.com/api/foo

Am I missing something? Can we close this?

@ashleysommer
Copy link
Member

Yep, that's right. I deploy sanic using that same pattern at my workplace in several applications.
I agree there could be a slightly more elegant way of doing it, but using config.SERVER_NAME is easy and just works (as long as you remember to always pass _external=True to url_for())
Maybe it could be documented better?

@ahopkins
Copy link
Member

Marking as a documentation issue.

@ahopkins ahopkins removed this from the v21.3 milestone Feb 21, 2021
@ahopkins
Copy link
Member

ahopkins commented Mar 1, 2021

Documented in this PR

@ahopkins ahopkins closed this as completed Mar 1, 2021
PromyLOPh added a commit to leibniz-psychology/bawwab that referenced this issue May 18, 2022
Looking at sanic-org/sanic#1731 this is “a
documentation issue”.
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

4 participants