Configuration & Environment Variables
Relevant source files
The following files were used as context for generating this wiki page:
The configuration system in the fikak_app project is designed to manage application settings, secrets, and environment-specific parameters through a centralized and type-safe approach. It primarily utilizes Pydantic's BaseSettings to load variables from environment files and system environment variables, ensuring that the application has the necessary context for database connections, security protocols, and external service integrations.
This centralized configuration ensures that the application remains portable and secure, separating sensitive credentials from the source code. The management logic is primarily housed within the app/core/config.py module, which serves as the single source of truth for all configurable parameters across the backend services.
Configuration Architecture
The application follows a hierarchical loading strategy where environment variables override defaults defined in the source code. The Settings class in app/core/config.py defines the expected schema for these variables.
The diagram above illustrates how configuration data flows from external sources into the Pydantic settings model, which then distributes validated settings to core application components.
Sources: app/core/config.py, app/db/session.py
Environment Variables Reference
The application requires several environment variables to function correctly, particularly for database connectivity and security. These are typically stored in a .env file during development.
| Variable | Description | Default Value |
|---|---|---|
PROJECT_NAME | The name of the FastAPI application | fikak_app |
DATABASE_URL | SQLAlchemy connection string for the database | N/A |
SECRET_KEY | Secret key for JWT token generation and encryption | N/A |
ACCESS_TOKEN_EXPIRE_MINUTES | Duration in minutes for JWT validity | 30 |
API_V1_STR | Prefix for the API endpoints | /api/v1 |
Sources: .env.example, app/core/config.py:10-30
Database Configuration
The database connection is established using the DATABASE_URL environment variable. The system uses SQLAlchemy to manage the engine and session factory. The app/db/session.py file consumes settings to initialize the database engine.
The flow shows the transformation of the raw configuration string into an active database session used by the API layer.
Sources: app/db/session.py:5-15, app/core/config.py
Application Initialization
During the startup of the FastAPI application in app/main.py, the settings object is imported to configure global middleware, CORS (Cross-Origin Resource Sharing), and the API router prefixes.
from app.core.config import settings
from fastapi import FastAPI
app = FastAPI(
title=settings.PROJECT_NAME,
openapi_url=f"{settings.API_V1_STR}/openapi.json"
)
Sources: app/main.py:1-10
Docker and Orchestration
The project utilizes docker-compose.yml to define the environment for both the application and its dependencies (like PostgreSQL). The environment variables defined in the .env file are passed into the container services to ensure consistency between the host and the containerized environment.
services:
db:
image: postgres:13-alphine
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
backend:
build: .
env_file:
- .env
depends_on:
- db
Sources: docker-compose.yml:5-25
Conclusion
The configuration management system in fikak_app provides a robust framework for handling application state and secrets. By leveraging Pydantic for validation and environment variables for flexibility, the project ensures that deployment across different environments (development, staging, production) can be handled via external configuration without modifying the codebase. This architecture supports secure practices by keeping sensitive data out of the version control system while maintaining a clear structure for developers.