From 525a7f981ea6e7086900ec918b3f863993add5fa Mon Sep 17 00:00:00 2001 From: Kraig Brockschmidt Date: Wed, 17 Oct 2018 17:04:52 -0700 Subject: [PATCH] Adding startup shim for App Service on Linux --- README.md | 4 +++- startup.py | 10 ++++++++++ startup.txt | 1 + 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 startup.py create mode 100644 startup.txt diff --git a/README.md b/README.md index e76951f5b..301c7ea4e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ This sample contains the completed program from the tutorial, [Using Flask in Visual Studio Code](https://code.visualstudio.com/docs/python/tutorial-flask). Intermediate steps are not included. -It also contains the Dockerfile and uwsgi.ini files necessary to build a container with a production server. The resulting image works both locally and when deployed to Azure App Service. +It also contains the Dockerfile and uwsgi.ini files necessary to build a container with a production server. The resulting image works both locally and when deployed to Azure App Service. See [Deploy Python using Docker containers](https://code.visualstudio.com/docs/python/tutorial-deploy-containers). + +The `startup.py` file, for its part, is specifically for deploying to Azure App Service on Linux without containers. Because the app code is in its own *module* in the `hello_app` folder (which has an `__init__.py`), trying to start the Gunicorn server within App Service on Linux produces an "Attempted relative import in non-package" error. The `startup.py` file, therefore, is just a shim to import the app object from the `hello_app` module, which then allows you to use startup:app in the Gunicorn command line (see `startup.txt`). Contributions to the sample are welcome. When submitting changes, also consider submitting matching changes to the tutorial, the source file for which is [tutorial-flask.md](https://github.com/Microsoft/vscode-docs/blob/master/docs/python/tutorial-flask.md). diff --git a/startup.py b/startup.py new file mode 100644 index 000000000..3c7d57d05 --- /dev/null +++ b/startup.py @@ -0,0 +1,10 @@ +# In this sample, the Flask app object is contained within the hello_app *module*; +# that is, hello_app contains an __init__.py along with relative imports. Because +# of this structure, a file like webapp.py cannot be run directly as the startup +# file through Gunicorn; the result is "Attempted relative import in non-package". +# +# The solution is to provide a simple alternate startup file, like this present +# startup.py, that just imports the app object. You can then just specify +# startup:app in the Gunicorn command. + +from hello_app.webapp import app diff --git a/startup.txt b/startup.txt new file mode 100644 index 000000000..068e6a817 --- /dev/null +++ b/startup.txt @@ -0,0 +1 @@ +gunicorn --bind=0.0.0.0 --workers=4 startup:app