Core¶
Core utilities, configuration, and shared functionality.
All core modules are located in src/core/ and provide foundational services used throughout the application.
Configuration¶
src.core.config.Settings ¶
Bases: BaseSettings
Application settings loaded from environment variables.
This class uses Pydantic to load, validate, and type-check all application settings from environment variables or a .env file. Settings are validated at startup, ensuring the application fails fast if misconfigured.
All settings have sensible defaults for development, but should be properly configured for production deployment.
Attributes:
| Name | Type | Description |
|---|---|---|
APP_NAME |
str
|
Application name used in API responses and logging. |
APP_VERSION |
str
|
Current version of the application. |
ENVIRONMENT |
str
|
Application environment (development/staging/production). |
DEBUG |
bool
|
Enable debug mode with verbose logging and auto-reload. |
API_V1_PREFIX |
str
|
URL prefix for API version 1 endpoints. |
HOST |
str
|
Host interface to bind the server to. |
PORT |
int
|
Port number for the main API server. |
RELOAD |
bool
|
Enable auto-reload on code changes (development only). |
SECRET_KEY |
str
|
Secret key for JWT token signing and encryption. |
ALGORITHM |
str
|
Algorithm used for JWT token encoding. |
ACCESS_TOKEN_EXPIRE_MINUTES |
int
|
Access token TTL in minutes. |
REFRESH_TOKEN_EXPIRE_DAYS |
int
|
Refresh token TTL in days. |
DATABASE_URL |
str
|
PostgreSQL connection URL with async driver. |
DB_ECHO |
bool
|
Enable SQLAlchemy query logging. |
CORS_ORIGINS |
str
|
List of allowed CORS origins. |
API_SSL_CERT_FILE |
str
|
Path to SSL certificate for HTTPS. |
API_SSL_KEY_FILE |
str
|
Path to SSL private key for HTTPS. |
CALLBACK_SERVER_HOST |
str
|
Host for OAuth callback server. |
CALLBACK_SERVER_PORT |
int
|
Port for OAuth callback server. |
API_CALLBACK_SSL_CERT_FILE |
str
|
SSL certificate for callback server. |
API_CALLBACK_SSL_KEY_FILE |
str
|
SSL private key for callback server. |
SCHWAB_API_KEY |
Optional[str]
|
Charles Schwab API client ID. |
SCHWAB_API_SECRET |
Optional[str]
|
Charles Schwab API client secret. |
SCHWAB_API_BASE_URL |
str
|
Base URL for Schwab API endpoints. |
SCHWAB_REDIRECT_URI |
str
|
OAuth redirect URI registered with Schwab. |
PLAID_CLIENT_ID |
Optional[str]
|
Plaid API client ID (future). |
PLAID_SECRET |
Optional[str]
|
Plaid API secret key (future). |
PLAID_ENVIRONMENT |
str
|
Plaid environment (sandbox/development/production). |
BCRYPT_ROUNDS |
int
|
Number of bcrypt hashing rounds (10-14 recommended). |
AWS_REGION |
str
|
AWS region for SES email service. |
AWS_ACCESS_KEY_ID |
Optional[str]
|
AWS access key for SES. |
AWS_SECRET_ACCESS_KEY |
Optional[str]
|
AWS secret key for SES. |
SES_FROM_EMAIL |
str
|
Email address used as sender. |
SES_FROM_NAME |
str
|
Display name for email sender. |
EMAIL_VERIFICATION_TOKEN_EXPIRE_HOURS |
int
|
Email verification token TTL. |
PASSWORD_RESET_TOKEN_EXPIRE_HOURS |
int
|
Password reset token TTL. |
GEOLITE2_DB_PATH |
str
|
Path to MaxMind GeoLite2 City database file. |
Source code in src/core/config.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 | |
Attributes¶
cors_origins_list
property
¶
Parse CORS origins from comma-separated string to list.
This property converts the CORS_ORIGINS string (comma-separated) into a list of origin URLs for use in FastAPI CORS middleware.
Returns:
| Type | Description |
|---|---|
List[str]
|
List of validated CORS origin URLs. |
Example
Input (CORS_ORIGINS): "https://localhost:3000,https://app.dashtam.com" Output: ["https://localhost:3000", "https://app.dashtam.com"]
server_url
property
¶
Get the full HTTPS URL for the main API server.
Constructs the complete URL including protocol, host, and port for the main FastAPI application server.
Returns:
| Type | Description |
|---|---|
str
|
Full HTTPS URL for the API server. |
Example
settings.server_url 'https://0.0.0.0:8000'
callback_server_url
property
¶
Get the full HTTPS URL for the OAuth callback server.
Constructs the complete URL including protocol, host, and port for the OAuth callback server that handles provider redirects.
Returns:
| Type | Description |
|---|---|
str
|
Full HTTPS URL for the callback server. |
Example
settings.callback_server_url 'https://0.0.0.0:8182'
Functions¶
set_debug_from_environment
classmethod
¶
Auto-set DEBUG mode based on ENVIRONMENT if not explicitly set.
In development and staging environments, DEBUG should be True by default to enable features like API documentation and detailed error messages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
bool
|
Current DEBUG value |
required |
values
|
Any
|
All field values including ENVIRONMENT |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Updated DEBUG value based on environment |
Source code in src/core/config.py
get_http_timeout ¶
Create httpx.Timeout object from configuration.
Returns configured timeout settings for all HTTP/HTTPS client operations. This prevents hanging requests and resource exhaustion.
Timeout Breakdown
- connect: Time allowed to establish a connection
- read: Time allowed to read response data
- pool: Time allowed to acquire a connection from the pool
- timeout: Total time allowed for the entire request
Returns:
| Type | Description |
|---|---|
Timeout
|
httpx.Timeout object with configured timeouts. |
Example
from src.core.config import settings import httpx async with httpx.AsyncClient(timeout=settings.get_http_timeout()) as client: ... response = await client.get(url)
Source code in src/core/config.py
validate_database_url
classmethod
¶
Ensure DATABASE_URL uses asyncpg driver for async operations.
This validator automatically converts standard PostgreSQL URLs to use the asyncpg driver, which is required for async database operations with SQLAlchemy and SQLModel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
str
|
Database URL string. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Database URL with asyncpg driver. |
Example
Input: "postgresql://user:pass@localhost/db" Output: "postgresql+asyncpg://user:pass@localhost/db"
Source code in src/core/config.py
validate_ssl_files
classmethod
¶
Validate that SSL certificate files exist.
This validator checks if the specified SSL certificate and key files exist on the filesystem. In development, it provides helpful guidance if certificates are missing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
str
|
Path to SSL certificate or key file. |
required |
info
|
Any
|
Pydantic validation context. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The validated file path. |
Note
In development, missing certificates will show a warning but won't fail validation. In production, you should ensure certificates exist.
Source code in src/core/config.py
src.core.config.get_settings
cached
¶
Get cached application settings singleton.
This function returns a cached instance of Settings to avoid re-reading and re-validating environment variables on every call. The settings are loaded once at application startup and reused throughout the application lifecycle.
The @lru_cache decorator ensures that only one Settings instance is created, making this function act as a singleton factory.
Returns:
| Type | Description |
|---|---|
Settings
|
Application configuration instance with all settings loaded from |
Settings
|
environment variables and validated according to their type annotations |
Settings
|
and validators. |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If required settings are missing or invalid. |
Example
settings = get_settings() print(settings.APP_NAME) 'Dashtam' print(settings.DATABASE_URL) 'postgresql+asyncpg://postgres:postgres@localhost:5432/dashtam'
Source code in src/core/config.py
Database¶
src.core.database.get_session
async
¶
Dependency injection function for database sessions.
This async generator function is designed to be used with FastAPI's dependency injection system. It provides a database session that is automatically closed after the request completes.
Yields:
| Type | Description |
|---|---|
AsyncGenerator[AsyncSession, None]
|
An AsyncSession instance for database operations. |
Example
from fastapi import Depends
@app.get("/users") async def get_users(session: AsyncSession = Depends(get_session)): result = await session.exec(select(User)) return result.all()
Source code in src/core/database.py
src.core.database.init_db
async
¶
Initialize the database by creating all tables.
This function creates all database tables defined by SQLModel models. It should be called once during application startup in development or testing environments.
Warning
This is for development only. Use Alembic migrations for production to preserve data and handle schema changes properly.
Raises:
| Type | Description |
|---|---|
Exception
|
If database initialization fails. |