URL rewrite + internal development server: how to? #446
-
|
I'm running a small Quart project behind an nginx proxy. The project makes use of internal server via The problem is, since the app itself has no idea about the rewrite, I've tried setting SCRIPT_NAME, both via nginx config and directly as app environment, to no avail. An example to clarify what I'm looking for: import quart
app = quart.Quart(__name__)
app.config['SERVER_NAME'] = '127.0.0.1:8080'
app.config['APPLICATION_ROOT'] = '/foo'
@app.get('/test')
async def test():
return 'This is a test, lol!'
@app.get('/')
async def root():
# I want this to return "http://127.0.0.1:8080/foo/test"
# Instead it returns "http://127.0.0.1:8080/test"
return quart.url_for('test', _external=True)
async def main():
from pprint import pprint
pprint(dict(app.config))
await app.run_task('127.0.0.1', 8080)
if __name__ == '__main__':
import asyncio
asyncio.run(main()) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
|
I also tried this approach: import quart
from hypercorn.middleware import ProxyFixMiddleware
app = quart.Quart(__name__)
app.asgi_app = ProxyFixMiddleware(app.asgi_app, mode='legacy', trusted_hops=1)
... # same things after thatNginx config: Still no luck. Test app shows that headers are being passed, but middleware doesn't seem to do anything... |
Beta Was this translation helpful? Give feedback.
-
|
So after picking the framework apart over a couple of days, I've come to the conclusion that the dev server does not support this. As such, I've come up with this hack. I replace url map class to enforce it always returns a prefixed map adapter. This way only URL building is affected, but not URL routing. You might want to import quart
from quart.routing import QuartMap
from werkzeug.routing import MapAdapter
class CustomQuartMap(QuartMap):
PREFIX = ''
def bind_to_request(self, *args, **kwargs) -> MapAdapter:
adapter = super().bind_to_request(*args, **kwargs)
adapter.script_name = self.PREFIX + adapter.script_name
return adapter
def bind_to_environ(self, *args, **kwargs) -> MapAdapter:
adapter = super().bind_to_environ(*args, **kwargs)
adapter.script_name = self.PREFIX + adapter.script_name
return adapter
quart.Quart.url_map_class = CustomQuartMap
app = quart.Quart(__name__)You can configure |
Beta Was this translation helpful? Give feedback.
So after picking the framework apart over a couple of days, I've come to the conclusion that the dev server does not support this.
Which is a shame, since switching over to hypercorn would be pointless, as I would be stuck with maxworkers=1 anyway because of the way my app is structured (webserver is not its main component, but other components register blueprints of their own).
As such, I've come up with this hack. I replace url map class to enforce it always returns a prefixed map adapter. This way only URL building is affected, but not URL routing. You might want to