How does FastAPI distinguish multiple environments: development/testing/pre-release/production environment

1 Dependence

Starting to develop projects with FastAPI,
distinguishing environments is the first step in deployment. Therefore, it is necessary to distinguish dev/test/pre/prod environments,
and FastAPI just provides parameters for reading environment configuration files, which can be specified when starting the service. The path of the file,
the parameter is env_file, and the module to read env_file is python-dotenv, which
needs to be installed manually:

pip install python-dotenv

There are two ways to use it:
(1) Configure in the run method: env_file=file_path
(2) Use in Docker: "--env-file", "file_path"
share as follows to help readers who need to use FastAPI development to easily deal with environment division.

2 applications

2.1 Add environment files

deploy/dev.env
insert image description here

  • File content
    format: key = value
    The read data are all str type, therefore, remember to convert the int type data,
    if the value is an empty string, the read data is None, therefore, only need to check None.
REDIS_HOST = "192.168.179.128"
REDIS_PORT = 6379

2.2 Configure environment parameters

Use startup parameters: env_file, configuration file path

if __name__ == "__main__":
    env_file_path = "deploy/prod.env"
    uvicorn.run(host="0.0.0.0", port=8000, app="main:app", reload=True, env_file=env_file_path)

2.3 How to get the configuration content

import os

REDIS_HOST = os.getenv("REDIS_HOST")
REDIS_PORT = int(os.getenv("REDIS_PORT"))

3 principles

3.1 start method run

There are many parameters for the uvicorn startup method run. The source code is shown in the figure below.
Find the env_file we need to configure the environment file path.
Location: uvicorn.main.run
insert image description here
When env_file is not empty, the method of reading env_file is: load_dotenv
insert image description here

The source code of the load_dotenv method is as follows. From the source code, we can see that
the specific reading logic is in the method: set_as_environment_variables.
Location: dotenv.main.load_dotenv

insert image description here

The source code of the set_as_environment_variables method is shown in the figure below.
From the source code, the data obtained from env_file is written into the environment configuration through os.environ.
Therefore, the corresponding value can be obtained through os.getenv(key_name).

位置:dotenv.main.DotEnv.set_as_environment_variables
insert image description here

4 Summary

(1) FastAPI provides many startup parameters, including environment configuration, which is passed in through the parameter env_file. If it is a Dockerfile, use –env_file to pass the parameter; (2) The
module to read the env_file file is python-dotenv;
(3) The acquired The data are all string type, if there is other type of data, pay attention to convert the data type.

Guess you like

Origin blog.csdn.net/Xin_101/article/details/130474481