I want to pass in environment variables through Apache + mod_wsgi to tell my app whether it's running in a development or production environment. (This needs to happen when the app is launched, before any requests have come in.) For example:
<VirtualHost *:80>
...
SetEnv ENVTYPE production
WSGIScriptAlias /myapp /apps/www/80/wsgi-scripts/myapp/run.py
</VirtualHost>
<VirtualHost *:8080>
...
SetEnv ENVTYPE development
WSGIScriptAlias /myapp /apps/www/80/wsgi-scripts/myapp/run.py
</VirtualHost>
Based on the answer given to "Apache SetEnv not working as expected with mod_wsgi", I have setup run.py
and the main __init__.py
like this:
Old run.py:
from myapp import app as application
if __name__ == '__main__':
application.run(debug=True, threaded=True)
New run.py :
import os
from myapp import app as _application
def application(environ, start_response):
os.environ['ENVTYPE'] = environ['ENVTYPE']
return _application(environ, start_response)
if __name__ == '__main__':
_application.run(debug=True, threaded=True)
__init__.py
app = Flask(__name__)
app.config.from_object(__name__)
if os.environ.get('ENVTYPE') == 'production'
# Setup DB and other stuff for prod environment
else:
# Setup DB and other stuff for dev environment
Two problems
This does not actually work. Within
__init__.py
, there is no 'ENVTYPE' key inos.envrion
. Why not?I also don't know how to fix the
if __name__ == '__main__'
section so that I can runrun.py
as a local Flask app on my PC. What I put in my newrun.py
works, but only by calling_application
instead of the wrapper functionapplication
. As a result_application
doesn't have access to the environment variables I defined. How can I fix that line?
Thank you!