【demand】
Using configuration management class management flask test environment,
By a parameter to control Flask is running develpment environment or production environment (database configuration, e-mail configuration also varies depending on the environment)
[Thinking]
1. All configuration information is stored in config.ini
2. settings.py by DdevelopConfig, ProductionConfig based access environment variable values different test environments
3. By way of example of DdevelopConfig or ProductionConfig class in app.py get the value of the classes of the environment variable
After creating an object Flask app, by the method app.config.from_object (obj) configuration information is written to app.config
So that the final effect is:
When creating app, by create_app (config_name), you can be flexible to switch test environment
[Code example]
Directory Structure:
config.ini
# The Flask object configuration
[App]
FLASK_CONFIG = Production
JSON_AS_ASCII = False
JSONIFY_MIMETYPE = " file application / JSON; charset = UTF-. 8 "
of SECRET_KEY = ' Secret String '
# database configuration
[SQLAlchemy]
SQLALCHEMY_TRACK_MODIFICATIONS = False
# online environment
[Production]
DATABASE_URL = ' MySQL + mysqlconnector: // username: password @ ip:? Port / dbname charset = utf8 '
# development environment
[development]
DATABASE_URL = '+ mysqlconnector MySQL: // username: password @ ip:? Port / dbname charset = utf8 '
# email configuration information
[Email]
MAIL_SERVER = " smtp.126.com "
MAIL_USERNAME = ' [email protected] '
mail_password = " Authorization Code "
settings.py
import os
from configparser import ConfigParser
basedir = os.path.abspath(os.path.dirname(__file__))
cfgpath = os.path.join(basedir, 'config.ini')
cf = ConfigParser()
cf.read(cfgpath)
# 获取邮件配置
def get_email_config(option):
return cf.get('email', option)
# 获取邮件配置
def get_app_config(option):
return cf.get('app', option)
# 获取数据库配置
def get_sqlalchemy_config(option):
return cf.get('sqlalchemy', option)
# 获取生产环境配置
def get_production_config(option):
return cf.get('production', option)
# 获取开发环境配置
def get_development_config(option):
return cf.get('development', option)
class BaseConfig(object):
SECRET_KEY = get_app_config('SECRET_KEY')
SQLALCHMEY_TRACK_MODIFICATIONS = False
MAIL_SERVER = get_email_config('MAIL_SERVER')
MAIL_PORT = 465
MAIL_USE_SSL = True
MAIL_USERNAME = get_email_config('MAIL_USERNAME')
MAIL_PASSWORD = get_email_config('MAIL_USERNAME')
MAIL_DEFAULT_SENDER = ('Bluelog Admin', MAIL_USERNAME)
class DevelopmentConfig(BaseConfig):
FLASK_ENV = 'development'
AAA = 'in DevelopmentConfig'
SQLALCHEMY_DATABASE_URI = get_development_config('DATABASE_URL')
class ProductingConfig(BaseConfig):
FLASK_ENV = 'production'
AAA='in ProductingConfig'
SQLALCHEMY_DATABASE_URI = get_production_config('DATABASE_URL')
config = {
'production': ProductingConfig,
'development': DevelopmentConfig
}
app.py
from flask import Flask
from settings import config,get_app_config
def create_app(config_name=None):
# 不指定config_name,默认取配置文件FLASK_CONFIG的值
# 指定config_name,则以指定的环境为主
if config_name is None:
config_name=get_app_config('FLASK_CONFIG')
app = Flask(__name__)
cfobj=config[config_name]
app.config.from_object(cfobj)
return app
#测试不指定config_name
app = create_app()
print(app.config['FLASK_ENV'])
print(app.config['AAA'])
print('*'*50)
#测试指定config_name
app = create_app('development')
print(app.config['FLASK_ENV'])
print(app.config['AAA'])
运行结果:
production
in ProductingConfig
**************************************************
development
in DevelopmentConfig