- Python Microservices Development
- Tarek Ziadé
- 406字
- 2021-07-02 18:54:23
Configuration
When building applications, you will need to expose options to run them, like the information to connect to a database or any other variable that is specific to a deployment.
Flask uses a mechanism similar to Django in its configuration approach. The Flask object comes with an object called config, which contains some built-in variables, and which can be updated when you start your Flask app via your configuration objects.
For example, you can define a Config class in a prod_settings.py file as follows:
class Config:
DEBUG = False
SQLURI = 'postgres://tarek:xxx@localhost/db'
And then, load it from your app object using app.config.from_object :
>>> from flask import Flask
>>> app = Flask(__name__)
>>> app.config.from_object('prod_settings.Config')
>>> print(app.config)
<Config {'SESSION_COOKIE_HTTPONLY': True, 'LOGGER_NAME': '__main__',
'APPLICATION_ROOT': None, 'MAX_CONTENT_LENGTH': None,
'PRESERVE_CONTEXT_ON_EXCEPTION': None,
'LOGGER_HANDLER_POLICY': 'always',
'SESSION_COOKIE_DOMAIN': None, 'SECRET_KEY': None,
'EXPLAIN_TEMPLATE_LOADING': False,
'TRAP_BAD_REQUEST_ERRORS': False,
'SESSION_REFRESH_EACH_REQUEST': True,
'TEMPLATES_AUTO_RELOAD': None,
'JSONIFY_PRETTYPRINT_REGULAR': True,
'SESSION_COOKIE_PATH': None,
'SQLURI': 'postgres://tarek:xxx@localhost/db',
'JSON_SORT_KEYS': True, 'PROPAGATE_EXCEPTIONS': None,
'JSON_AS_ASCII': True, 'PREFERRED_URL_SCHEME': 'http',
'TESTING': False, 'TRAP_HTTP_EXCEPTIONS': False,
'SERVER_NAME': None, 'USE_X_SENDFILE': False,
'SESSION_COOKIE_NAME': 'session', 'DEBUG': False,
'JSONIFY_MIMETYPE': 'application/json',
'PERMANENT_SESSION_LIFETIME': datetime.timedelta(31),
'SESSION_COOKIE_SECURE': False,
'SEND_FILE_MAX_AGE_DEFAULT': datetime.timedelta(0, 43200)}>
However, there are two significant drawbacks when using Python modules as configuration files.
First, it can be tempting to add into those configuration modules some code that's more complex than simple flat classes; and by doing so, it means you will have to treat those modules like the rest of the application code. That's usually not what happens when applications are deployed: the configuration files are managed separately from the code.
Secondly, if another team is in charge of managing the configuration file of your application, they will need to edit the Python code to do so. While this is usually fine, it makes it easier to introduce some problems. For instance, it's harder to make Puppet templates out of Python modules rather than flat, static configuration files.
Since Flask exposes its configuration via app.config, it's pretty simple to load additional options from a YAML file, or any other text-based file.
The INI format is the most-used format in the Python community, because there's an INI parser included in the standard library, and because it's pretty universal.
Many Flask extensions exist to load the configuration from an INI file, but using the standard library ConfigParser is trivial. Although, there's one major caveat from using INI files: variables values are all strings, and your application needs to take care of converting them to the right type.
The Konfig project (https://github.com/mozilla-services/konfig) is a small layer on top of ConfigParser, which automates the conversion of simple types like integers and Booleans.
Using it with Flask is straightforward:
$ more settings.ini
[flask]
DEBUG = 0
SQLURI = postgres://tarek:xxx@localhost/db
$ python
>>> from konfig import Config
>>> from flask import Flask
>>> c = Config('settings.ini')
>>> app = Flask(__name__)
>>> app.config.update(c.get_map('flask'))
>>> app.config['SQLURI']
'postgres://tarek:xxx@localhost/db