Backend Configuration
Relevant source files
The following files were used as context for generating this wiki page:Introduction
The backend configuration for the QA Dashboard is managed primarily through environment variables, providing a clear separation between code and configuration. This system allows for flexible deployment across different environments (development, production) without code changes. The configuration handles database connections, security credentials for JWT, application behavior (like debug mode), and integrations with external services such as AI-powered suggestions and webhook notifications.
All configuration settings are centralized and loaded using Pydantic's BaseSettings in backend/settings.py. This ensures that settings are validated and type-checked upon application startup. The primary source for these settings is a .env file in the backend directory, with backend/.env.example serving as a template.
Sources: backend/settings.py, backend/.env.example, README.md:105-123
Environment Variables
The application relies on a .env file in the backend directory to configure its core components. The backend/.env.example file provides a template for all required variables.
Sources: backend/.env.example, README.md:105-123
The following diagram illustrates how environment variables from the .env file are loaded by the Settings class and distributed to various modules within the backend application.
Configuration Table
This table details all the environment variables used by the backend.
| Variable | Description | Default Value | Source File Usage |
|---|---|---|---|
DB_HOST | The hostname of the PostgreSQL database (e.g., AWS RDS endpoint). | localhost | settings.py:17 |
DB_PORT | The port number for the PostgreSQL database. | 5432 | settings.py:18 |
DB_NAME | The name of the database to connect to. | qa_dashboard | settings.py:19 |
DB_USER | The username for database authentication. | postgres | settings.py:20 |
DB_PASSWORD | The password for database authentication. | password | settings.py:21 |
API_HOST | The host address the FastAPI application binds to. | 0.0.0.0 | settings.py:26 |
API_PORT | The port the FastAPI application runs on. | 8000 | settings.py:27 |
DEBUG | Toggles debug mode for the application, enabling features like SQLAlchemy query echoing. | True | settings.py:28, database.py:16 |
SECRET_KEY | A secret key used for signing JWT tokens. Must be changed in production. | your-secret-key... | settings.py:31 |
JWT_ALGORITHM | The algorithm used for JWT encoding and decoding. | HS256 | settings.py:34 |
JWT_EXPIRATION_HOURS | The duration (in hours) for which a JWT token is valid. | 24 | settings.py:35 |
WEBHOOK_URL | The endpoint URL for sending webhook notifications (e.g., for new questions). | None | settings.py:38, utils/webhooks.py:6 |
OPENAI_API_KEY | API key for OpenAI (optional, for Langchain). | None | README.md:126 |
LANGCHAIN_API_KEY | API key for Langchain services (optional). | None | README.md:127 |
GOOGLE_API_KEY | API key for Google's Generative AI (Gemini). Used by the AI service. | None | services/ai_service.py:19 |
Sources: backend/.env.example, backend/settings.py, backend/services/ai_service.py:19, backend/utils/webhooks.py:6
Core Application Configuration
Settings Management (settings.py)
The backend/settings.py file uses Pydantic's BaseSettings to define and load all configuration variables from the environment. This provides a single, type-safe source of truth for all settings.
The Settings class automatically constructs the DATABASE_URL from its constituent parts (DB_HOST, DB_USER, etc.).
# backend/settings.py:9-23
class Settings(BaseSettings):
# ...
DB_HOST: str = "localhost"
DB_PORT: int = 5432
DB_NAME: str = "qa_dashboard"
DB_USER: str = "postgres"
DB_PASSWORD: str = "password"
DATABASE_URL: str = Field(
default_factory=lambda: f"postgresql+psycopg2://{settings.DB_USER}:{settings.DB_PASSWORD}@{settings.DB_HOST}:{settings.DB_PORT}/{settings.DB_NAME}"
)
Sources: backend/settings.py:9-23
Database Connection (database.py)
The database connection is established using SQLAlchemy's create_engine. It retrieves the fully constructed DATABASE_URL from the settings object. The DEBUG setting is used to toggle SQL query logging (echo=settings.DEBUG).
# backend/database.py:14-18
engine = create_engine(
settings.DATABASE_URL,
echo=settings.DEBUG,
pool_pre_ping=True,
)
Sources: backend/database.py:14-18
The following diagram shows the sequence of events during application startup to establish a database connection.
External Service Configuration
AI Service (ai_service.py)
The AI service for generating suggested answers is configured via the GOOGLE_API_KEY environment variable. If this key is not provided, the service operates in a "mock mode," returning a placeholder response instead of making a real API call. This allows the application to function without a valid AI service subscription.
The _initialize method within the AIService class handles this logic.
# backend/services/ai_service.py:18-29
def _initialize(self):
self.api_key = os.getenv("GOOGLE_API_KEY")
self.mock_mode = False
self.llm = None
if not self.api_key:
logger.warning("GOOGLE_API_KEY not found. AIService will run in MOCK mode.")
self.mock_mode = True
else:
try:
self.llm = ChatGoogleGenerativeAI(...)
logger.info("AIService initialized with Gemini.")
except Exception as e:
# ...
self.mock_mode = True
Sources: backend/services/ai_service.py:18-29
Webhook Notifications (webhooks.py)
The system can send notifications to an external service via webhooks. This is configured using the WEBHOOK_URL environment variable. If the WEBHOOK_URL is not set, the send_webhook_notification function will return False immediately, effectively disabling the feature.
# backend/utils/webhooks.py:5-8
async def send_webhook_notification(event_type: str, data: dict) -> bool:
"""Send webhook notification to external service"""
if not settings.WEBHOOK_URL:
return False
Sources: backend/utils/webhooks.py:5-8, backend/settings.py:38