4. Set Configurations
I used a .env file to store configuration values. To load them into the application, I’m using BaseSettings from the pydantic_settings package.
-
Here’s an sample
.envfile used to configure thePostgreSQLdatabase connection:POSTGRES_HOST=localhost POSTGRES_PORT=5432 POSTGRES_USER=myuser POSTGRES_PASSWORD=secret POSTGRES_DB=cryptobot
1. Settings
-
pydantic_settings.BaseSettingsis an extended version ofPydantic’s model designed for managing environment variables. -
It’s commonly used in Python backend frameworks like FastAPI to load and validate environment variables.
-
First, I created a
Settingsclass to handlePostgreSQLconnection configuration:from pydantic_settings import BaseSettings class Settings(BaseSettings): ... POSTGRES_HOST: str POSTGRES_PORT: int POSTGRES_USER: str POSTGRES_PASSWORD: str POSTGRES_DB: str ... -
Next, I added a property method to easily build the
PostgreSQLconnection URL:class Settings(BaseSettings): ... @property def db_url(self): return f"postgresql://{self.POSTGRES_USER}:{self.POSTGRES_PASSWORD}@{self.POSTGRES_HOST}:{self.POSTGRES_PORT}/{self.POSTGRES_DB}"- With this method, I can easily access the full PostgreSQL URL wherever it’s needed in this application.
-
To create an instance of the
Settingsclass, I added the following code below the class definition:class Settings(BaseSettings): ... settings = Settings() -
Once initialized, the configuration values can be accessed anywhere in the application like this:
from app.core.config import settings settings.db_url