如何使用create_app将flask应用程序中的不同配置设置提供给uwsgi? [英] How to serve different config settings in flask app to uwsgi using create_app?
问题描述
在我的Flask应用程序的 现在我正在使用 以及其中一个 我想知道,是否正确地使用 或者使用环境变量来区分不同的配置设置?像 更好? Flask的 一个常见的方法是将配置存储在字典中。 然后,在实际创建应用程序的位置,您可以执行下列操作: 在环境之间轻松地通过更改您的shell中的变量。 In my Flask application's Now i'm using and in one of other I want to know, will it properly with Or is it better to use Environment Variables to distinguish between different config settings? Like Which way is better? Flask's A common way of doing this is storing the configurations in a dictionary. And then where you actually create your app, you'd do something like this:: That way you can easily switch between environments easily by changing a variable in your shell. 这篇关于如何使用create_app将flask应用程序中的不同配置设置提供给uwsgi?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! config.py
中,我有 LocalConfig
和 ProdConfig $
uWsgi
将应用程序提供给 Nginx
,这里创建了 myapp.wsgi
。
from myapp import create_app $ b $ from myapp.config import ProdConfig
$ b $ app = create_app(config = ProdConfig)
app.py
被定义为:
$ $ $ $ c $ def create_app(config = None,app_name = None, blueprints = None):
#一些代码
app = Flask(app_name,instance_path = INSTANCE_FOLDER_PATH,instance_relative_config = True)
configure_app(app,config)
#其他代码
返回应用程序
def configure_app(应用程序,配置=无):
不同的配置方式。
app.config.f rom_object(LocalConfig)
app.config.from_pyfile('production.cfg',silent = True)
如果配置:
app.config.from_object(config)
uWSGI
? uWSGI能够成功应用 ProdConfig
吗?
if os.environ.get('PROD',True):#do something ?
create_app()
一个还是env变量?任何其他合适的方法?
$ b
class Config:
ALL_CAPS_CONFIG ='SOME VALUE'
$ b $ class DevConfig(Config):
pass
$ b $ class TestConfig(Config):
pass
$ b $ class ProdConfig(Config):
pass
configs = {
'dev':DevConfig,
'test':TestConfig,
'prod':ProdConfig,
'default':ProdConfig
}
from config import configs
import os
evn = os.environ.get('MY_FLASK_APP_ENV','default')
create_app(config = configs [evn])
config.py
i have LocalConfig
and ProdConfig
and latter is to be used in production, once app is deployed.uWsgi
to serve app to Nginx
and here the myapp.wsgi
i have created.from myapp import create_app
from myapp.config import ProdConfig
app = create_app(config=ProdConfig)
app.py
create_app
is defined as:def create_app(config=None, app_name=None, blueprints=None):
# some code
app = Flask(app_name, instance_path=INSTANCE_FOLDER_PATH, instance_relative_config=True)
configure_app(app, config)
# some other code
return app
def configure_app(app, config=None):
"""Different ways of configurations."""
app.config.from_object(LocalConfig)
app.config.from_pyfile('production.cfg', silent=True)
if config:
app.config.from_object(config)
uWSGI
? Will uWSGI be able to applying the ProdConfig
successfully?if os.environ.get('PROD', True): #do something
?create_app()
one or the env variable one? Any other suitable approach?class Config:
ALL_CAPS_CONFIG = 'SOME VALUE'
class DevConfig(Config):
pass
class TestConfig(Config):
pass
class ProdConfig(Config):
pass
configs = {
'dev' : DevConfig,
'test' : TestConfig,
'prod' : ProdConfig,
'default' : ProdConfig
}
from config import configs
import os
evn = os.environ.get('MY_FLASK_APP_ENV', 'default')
create_app(config=configs[evn])