Backend Deployment
Relevant source files
The following files were used as context for generating this wiki page:
The backend for the QA-Dashboard is a Python-based application built using the FastAPI framework. It is responsible for serving RESTful API endpoints, managing WebSocket connections for real-time updates, handling user authentication, and interacting with a PostgreSQL database. The backend also integrates with AI services for features like answer suggestions.
Deployment involves setting up the required infrastructure (database, Python environment), configuring the application using environment variables, and running it with an ASGI server like Uvicorn. The project documentation suggests deployment targets such as AWS Elastic Beanstalk or a combination of AWS Lambda and API Gateway.
Sources: README.md, backend/main.py
Architecture Overview
The backend is a monolithic service that communicates with a Next.js frontend, a PostgreSQL database, and external AI services.
This diagram illustrates the high-level components and their interactions during deployment.
Sources: README.md, backend/main.py, backend/api/questions.py
Prerequisites
Before deploying the backend, the target environment must meet the following prerequisites.
System Requirements
- Python 3.8+: The runtime environment for the FastAPI application.
- PostgreSQL: A relational database is required. The project is configured for AWS RDS PostgreSQL.
- Git: For cloning the repository.
Sources: README.md
Python Dependencies
The application's Python dependencies are listed in requirements.txt. They must be installed in a virtual environment using pip.
| Package | Description |
|---|---|
fastapi | The core web framework for building the API. |
sqlalchemy | ORM for database interaction with PostgreSQL. |
psycopg2-binary | PostgreSQL adapter for Python. |
python-jose[cryptography] | Implements JWTs for authentication. |
passlib[bcrypt] | Used for hashing and verifying passwords. |
python-dotenv | Manages environment variables from a .env file. |
uvicorn[standard] | ASGI server to run the FastAPI application. |
langchain / langchain-google-genai | Framework for integrating with Large Language Models (LLMs). |
faiss-cpu | Library for efficient similarity search, used by the AI service. |
pydantic[email] | Used for data validation and settings management. |
Sources: backend/requirements.txt, README.md
Configuration
The application is configured using environment variables, which are typically stored in a .env file in the backend/ directory for local development. For production deployment, these variables must be set in the hosting environment (e.g., Elastic Beanstalk configuration, Lambda environment variables).
| Variable | Description | Example Value |
|---|---|---|
DB_HOST | The endpoint/hostname of the PostgreSQL database. | your-rds-endpoint.rds.amazonaws.com |
DB_PORT | The port number for the database connection. | 5432 |
DB_NAME | The name of the database to use. | qa_dashboard |
DB_USER | The username for the database connection. | admin |
DB_PASSWORD | The password for the database user. | your-secure-password |
SECRET_KEY | A secret key for signing JWTs and other security functions. | your-secret-key-change-in-production |
DEBUG | Toggles debug mode. Should be False in production. | True |
JWT_ALGORITHM | The algorithm used for JWT signing. | HS256 |
JWT_EXPIRATION_HOURS | The number of hours a JWT token is valid. | 24 |
WEBHOOK_URL | Endpoint for webhook notifications. | https://your-webhook-endpoint.com/webhook |
OPENAI_API_KEY | Optional API key for OpenAI integration. | your-openai-key |
GOOGLE_API_KEY | API key for Google Generative AI (Gemini). | your-google-api-key |
Sources: README.md, backend/services/ai_service.py:15, backend/database.py:12
Database Setup
The backend requires a PostgreSQL database. The schema is defined using SQLAlchemy ORM models.
Database Schema
The database consists of three main tables: users, questions, and answers.
This ER diagram shows the relationships between the database tables.
Sources: backend/database.py:27-80
Initialization
On application startup, the init_db() function is called. This function uses SQLAlchemy's Base.metadata.create_all(bind=engine) to create all defined tables if they do not already exist in the database.
Sources: backend/main.py:14, backend/database.py:83-84
Application Startup
The application is launched via an ASGI server like Uvicorn, pointing to the app instance in main.py.
uvicorn main:app --reload
Sources: README.md
Startup Lifecycle (lifespan manager)
The main.py file defines a lifespan asynchronous context manager that executes tasks during application startup and shutdown.
The following flowchart illustrates the sequence of events during application startup.
Sources: backend/main.py:12-40
Middleware Configuration
The FastAPI application is configured with several middleware components:
- CORSMiddleware: Allows cross-origin requests from specified origins (e.g.,
http://localhost:3000), which is necessary for the frontend to communicate with the backend. - TrustedHostMiddleware: Restricts which hosts the application will respond to.
Sources: backend/main.py:48-58
API Routers
The application's endpoints are organized into routers which are included in the main app instance.
auth.router: Handles user registration and login.questions.router: Manages creating, retrieving, and updating questions, and includes the WebSocket endpoint.answers.router: Manages creating and retrieving answers.
Sources: backend/main.py:60-62
Deployment Strategies
The README.md file suggests several strategies for deploying the backend to a production environment.
- AWS Elastic Beanstalk: A PaaS offering that automates the deployment, scaling, and management of the application.
- AWS Lambda + API Gateway: A serverless approach where the FastAPI application is wrapped in a handler function and exposed via API Gateway.
In both scenarios, the PostgreSQL database would be hosted on AWS RDS, and the application configuration would be managed through the respective service's environment variable settings.
Sources: README.md