Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

python - Flask: How to manage different environment databases?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...