Flask mounted with WSGIMiddleware gets request context mixed #6749
-
DescriptionHi everyone, i'm starting to migrate from Flask to FastAPI. I thought of doing this by mounting the old flask app under a new FastAPI instance like this: app = create_fastapi_app()
flask_app = create_flask_app()
app.mount("/", WSGIMiddleware(flask_app))My flask app uses the before_request hook to set the current user. def set_authenticated_user():
current_app.authenticated_user = None
if "authorization" in request.headers:
current_app.authenticated_user = (
AuthenticatedUserRequest.get_authenticated_user()
)
def create_flask_app():
app = Flask(__name__)
...
app.before_request(set_authenticated_user)My flask app also has a healthcheck endpoint as a kubernetes liveness probe. This request does not contain the authorization header. Thanks Environment
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
I would actually try to deploy the Flask app independently and handle routing based on path prefix with a Proxy like Traefik, probably also doing the HTTPS part. Flask's internals are a bit finicky, even more, if you are are heavily using Flask's own psedo-global objects. There might be a way to solve it, but it probably depends a lot on your code, and would probably take a lot of effort debugging it, so, if you can avoid it, that might be easier. But anyway, if you still have problems, please add a self-contained, minimal, reproducible, example that I can copy-paste to replicate it.
|
Beta Was this translation helpful? Give feedback.
-
|
Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs. |
Beta Was this translation helpful? Give feedback.
I would actually try to deploy the Flask app independently and handle routing based on path prefix with a Proxy like Traefik, probably also doing the HTTPS part.
Flask's internals are a bit finicky, even more, if you are are heavily using Flask's own psedo-global objects. There might be a way to solve it, but it probably depends a lot on your code, and would probably take a lot of effort debugging it, so, if you can avoid it, that might be easier.
But anyway, if you still have problems, please add a self-contained, minimal, reproducible, example that I can copy-paste to replicate it.