"12-Factor" is a methodology for building SaaS services, a theory that applies to applications developed in any language and back-end services (databases, message queues, caching, etc.).
One of the most important principles is about configuration, and 12-Factor requires a strict separation of code and configuration.
Why would you do that?
If you put your code on an external network such as Github, it's very scary if your code is accidentally leaked one day and your passwords, keys, and other configurations are all exposed on the public network.
A simple way to tell if an application has correctly separated configuration from code is if your code can be open sourced immediately without worrying about any sensitive information being exposed.
It is common practice to store the configuration of an application in an environment variable, for example by adding it to the command line:
export PASSWORD=123456
windows
set PASSWORD=123456
In business code, it is loaded via environment variables.
import os env = ("PASSWORD") print(env)
By doing so, you don't expose sensitive information to business code, and you maximize the exposure of developers to sensitive information in the formal environment.
However, the problem comes that setting sensitive information as an environment variable is too much of a hassle to set one by one if there is a lot of such information.
You'll want to keep this sensitive information in a separate file, always managed separately from the code!
For example, in a flask project, we have sensitive information that we put in a file called .flaskenv.
.flaskenv file
FLASK_DEBUG=1 FLASK_ENV=local AAA=1234
But how are these configurations loaded into environment variables?
python-dotenv
python-dotenv is designed to do just that, automatically adding configuration information from configuration files to environment variables.
Install python-dotenv
pip install python-dotenv
Load Configuration File
from dotenv import load_dotenv # Load files load_dotenv(".flaskenv") import os flask_env = ("FLASK_ENV") print(flask_env) # local
Once the file is loaded, you can read the contents by reading from the environment variable.
flask configuration best practices
In flask, python-dotenv fits seamlessly into your project, prompting you to install python-dotenv whenever a .env or .flaskenv file exists in your project.
$ flask run * Tip: There are .env files present. Do "pip install python-dotenv" to use them.
After installing python-dotenv, it will automatically load the configuration files inside into the environment variables.
# class LocalConfig(BaseConfig): ENV = "development" FLASK_DEBUG = 1 # Load database configurations via variable environments SQLALCHEMY_DATABASE_URI = ("SQLALCHEMY_DATABASE_URI") # def create_app(): app = Flask(__name__) .from_object(LocalConfig) # def hello(): # Load environment variables ("AAA")
to this article on the python elegant realization of code and sensitive information to separate the method of the article is introduced to this, more related python code and sensitive information to separate the contents of the search for my previous articles or continue to browse the following related articles I hope that you will support me in the future more!