“Decouple” for FastAPI project
In a FastAPI project, you can use the decouple
library to separate your staging and production configurations. decouple
is a simple, flexible and widely used library that allow to store your configurations in a .env
file and easily read them in your Python code.
Here’s an example of how you can use decouple
to separate your staging and production configurations:
- Install the
decouple
library by runningpip install decouple
in your terminal. - Create a
.env
file in the root of your project and specify your staging and production configurations. For example:
# .env
DEBUG=True
DATABASE_URL=sqlite:///./test.db
SECRET_KEY=mysecretkey
- Import the
config
object from thedecouple
library in yourmain.py
file and use it to access the configurations in your code.
from decouple import config
DEBUG = config("DEBUG", default=False, cast=bool)
DATABASE_URL = config("DATABASE_URL", default="sqlite:///./test.db")
SECRET_KEY = config("SECRET_KEY", default="")
- To use different configurations for staging and production, you can use different
.env
files for each environment and load the appropriate file depending on the current environment. For example:
# .env.staging
DEBUG=True
DATABASE_URL=postgres://staging_user:staging_password@staging_host:5432/staging_db
SECRET_KEY=mysecretkey
# .env.production
DEBUG=False
DATABASE_URL=postgres://production_user:production_password@production_host:5432/production_db
SECRET_KEY=mysecretkey
- You can set the environment using an environment variable, for example in your shell or in your
.bashrc
or.bash_profile
:
export ENVIRONMENT=production
- Then in the code you can use that environment variable to choose which
.env
file you want to load
from decouple import Config, RepositoryEnv
environment = os.getenv("ENVIRONMENT", "development")
config = Config(RepositoryEnv(f".env.{environment}"))
By using this method, you can easily switch between your staging and production configurations by just changing the environment variable, and keep the sensitive data such as database credentials and secret key away from version control system.
- Another way to use different configurations for different environment is to use different
.env
files for each environment and load the appropriate file depending on the current environment. For example, you can have a.env.development
file for development environment, a.env.staging
file for staging environment and a.env.production
file for production environment. In each file, you can have the appropriate configurations for each environment. - Then in your main script, you can use the
os.environ
oros.getenv
method to check the current environment and load the corresponding.env
file. For example:
import os
from decouple import config
environment = os.getenv("ENVIRONMENT", "development")
if environment == "development":
config.config_file = ".env.development"
elif environment == "staging":
config.config_file = ".env.staging"
else:
config.config_file = ".env.production"
Here, we check the ENVIRONMENT
environment variable, if it's not set, it will fall back to "development", otherwise it will use the value of that variable to load the correct .env
file.
- You can also use a environment variable to set the .env file and call it in main script.
export APP_SETTINGS=config.DevelopmentConfig
import os
from decouple import config
config_name = os.getenv("APP_SETTINGS")
app.config.from_object(config_name)
You can use any of the above method to separate your staging and production configurations, depending on your specific use case and requirements. But, either way, decouple
library makes it easy to manage your configurations and keep sensitive information, such as secrets and credentials, separate from your codebase, making it more secure and maintainable.