Skip to content

Commit

Permalink
Parse flags in wsgi_app
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwillchen committed May 9, 2024
1 parent 8ccc8d2 commit 0ed8a45
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions mesop/server/wsgi_app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import sys
from typing import Any, Callable

from absl import flags
from flask import Flask

from mesop.runtime import enable_debug_mode
Expand Down Expand Up @@ -43,6 +45,17 @@ def create_app(
return App(flask_app=flask_app)


flask_app = None


def wsgi_app(environ: dict[Any, Any], start_response: Callable[..., Any]):
app = create_app(prod_mode=True)
return app._flask_app.wsgi_app(environ, start_response) # type: ignore
# Lazily create and reuse a flask app singleton to avoid
# the overhead for each WSGI request.
global flask_app
if not flask_app:
# Parse the flags before creating the app otherwise you will
# get UnparsedFlagAccessError.
flags.FLAGS(sys.argv)
flask_app = create_app(prod_mode=True)

return flask_app._flask_app.wsgi_app(environ, start_response) # type: ignore

0 comments on commit 0ed8a45

Please sign in to comment.