With the web framework and ORM Framework, we can start assembling apps.
Typically, a web app needs to read a configuration file at run time, such as a database username, password, etc., while running in a different environment, the web app can read different configuration files to get the right configuration.
Because of its simple syntax, Python can be configured directly with the Python source code without having to parse a separate. Properties or. yaml configuration file.
The default configuration file should be fully compliant with the local development environment so that you can start the server immediately without any setup.
We name the default configuration file config_default.py:
# config_default.pyconfigs = { ' db ': { ' host ': ' 127.0.0.1 ', ' Port ': 3306, ' user ': ' www-data ', ' Password ': ' Www-data ', ' database ': ' Awesome ' }, ' session ': { ' secret ': ' Awesome ' }}
The above configuration file is simple and straightforward. However, if you want to deploy to the server, usually need to modify the database host and other information, directly modify config_default.py is not a good way, a better way is to write a config_override.py, to override some of the default settings:
# config_override.pyconfigs = { ' db ': { ' host ': ' 192.168.0.100 ' }}
Using config_default.py as the standard configuration for the development environment and config_override.py as a standard configuration for production environments, we can easily develop locally and deploy applications to servers at any time.
The application read configuration file needs to be read first from config_override.py. To simplify reading the configuration file, all configurations can be read into the unified config.py:
# config.pyconfigs = config_default.configstry: import config_override configs = Merge (Configs, Config_ Override.configs) except Importerror: Pass
In this way, we have completed the configuration of the app.