烧瓶:如何管理不同的环境数据库? [英] Flask: How to manage different environment databases?

查看:102
本文介绍了烧瓶:如何管理不同的环境数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个类似于

的应用程序。 feed /
__init__.py
business.py
views.py
models /
persistence.py
user.py
chat /
__init__.py
models.py
business.py
views.py
config /
dev.py
test.py
prod.py

我想要三个环境 Dev Test 生产

我有以下要求:

。)当我启动服务器 python runserver.py ,我想提一下我想连接的环境 - Dev Test Production

b。) Dev & 生产应该有内置的模式,只需要连接到机器
c。)我也想为我的测试连接到 sqlite db ,然后创建模式,运行测试

我怎样才能以配置的方式实现这一点,以便我不必硬编码任何与数据库相关的东西。



在烧瓶中有没有好的模式?



目前我的 runerver.py 对我不喜欢的环境进行硬编码,

  app = Flask(__ name__)
app.config ['SECRET_KEY'] = dev.SECRET_KEY

I我正在寻找比我更好的创意

解决方案

解决方案我使用:

< pre $ #__ init__.py
app = Flask(__ name__)
app.config.from_object('settings')
app.config.from_envvar(' MYCOOLAPP_CONFIG',silent = True)

离子载入:

pre $ #settings.py
SERVER_NAME =dev.app.com
DEBUG = True
SECRET_KEY ='xxxxxxxxxx'


#settings_production.py
SERVER_NAME =app.com
DEBUG = False

所以。
如果环境变量MYCOOLAPP_CONFIG不存在 - >只会加载settings.py,它指的是默认设置(开发服务器) -
这就是silent = True的原因,第二个配置文件不是必需的,而settings.py默认的开发和普通配置键的默认值



如果除了第一个值之外还将加载其他的settings_file它覆盖原来的值。 (在我的例子中,DEBUG和SERVER_NAME将被覆盖,而SECRET_KEY对于所有的服务器保持不变)

你应该发现自己唯一的事情取决于你如何启动你的应用程序
启动ENVVAR之前应该设置MYCOOLAPP_CONFIG

例如我运行supervisor守护进程和生产服务器上我只是把它放在监控程序配置文件:

  environment = MYCOOLAPP_CONFIG =/ home / tigra / mycoolapp / settings_production.py

通过这种方式,您可以轻松管理所有配置文件,而且,通过这种方式,您可以从git或任何其他版本控制实用程序中排除此文件。



默认的Linux方式是在启动之前在控制台中执行:

export MYCOOLAPP_CONFIG =/ home / tigra / mycoolapp / settings_production.py


I am working on an app which looks similar to

facebook/
         __init__.py
         feed/
             __init__.py
             business.py
             views.py
             models/
                    persistence.py
                    user.py
         chat/
             __init__.py
             models.py
             business.py
             views.py
         config/
                dev.py
                test.py
                prod.py 

I want to have three environments Dev, Test and Production.
I have the following requirements:
a.) When I start the server python runserver.py, I would like to mention which environment I want to connect - Dev, Test or Production.
b.) Dev & Production should have the schema built and just need to connect to machine
c.) I would also like for my test to connect to sqlite db, and create the schema, run tests

How can I achieve this in a configuration manner so that I do not have to hardcode anything related to database.

Are there any good patterns available in flask?

Currently my runerver.py has hardcoding for environment that I don't like,

app = Flask(__name__)
app.config['SECRET_KEY'] = dev.SECRET_KEY

I am looking for better ideas than I have

解决方案

Solution I use:

#__init__.py
app = Flask(__name__)
app.config.from_object('settings')
app.config.from_envvar('MYCOOLAPP_CONFIG',silent=True)

On the same level from which application loads:

#settings.py
SERVER_NAME="dev.app.com"
DEBUG=True
SECRET_KEY='xxxxxxxxxx'


#settings_production.py
SERVER_NAME="app.com"
DEBUG=False

So. If Environment Variable MYCOOLAPP_CONFIG does not exist -> only settings.py will load, which refers to default settings (development server as for me)
This is the reason for "silent=True", second config file not required, while settings.py default for development and with default values for common config keys

If any other settings_file will be loaded in addition to first one values inside it overrides values in original one. (in my example DEBUG and SERVER_NAME will be overrided, while SECRET_KEY stays same for all servers)

The only thing you should discover for yourself depends on the way how you launch your application
Before launching ENVVAR MYCOOLAPP_CONFIG should be set
For example I run with supervisor daemon and on production server I just put this in supervisor config file:

environment=MYCOOLAPP_CONFIG="/home/tigra/mycoolapp/settings_production.py"

With this way you can easily manage all your configuration files, moreover, with this way you can exclude this files from git or any other version control utility

default Linux way is this one in console before launching:
export MYCOOLAPP_CONFIG="/home/tigra/mycoolapp/settings_production.py"

这篇关于烧瓶:如何管理不同的环境数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆