๐ฅ DevOps Demo Application - Backend¶
The backend of our DevOps demo application is built with FastAPI, providing a high-performance, easy-to-use API with automatic interactive documentation.
๐ป Table of Contents¶
- Features
- Requirements
- Development Setup
- Docker-based Development
- Local Development
- API Documentation
- Project Structure
- Database
- Testing
- Deployment
โจ Features¶
- FastAPI Framework: High-performance, easy to learn, fast to code, ready for production
- SQLModel ORM: SQL databases in Python, designed for simplicity, compatibility, and robustness
- JWT Authentication: Secure authentication with JWT tokens
- Automatic API Documentation: Interactive API documentation with Swagger UI and ReDoc
- Dependency Injection: Clean and testable code with FastAPI's dependency injection system
- Async Support: Fully asynchronous API endpoints for high performance
- Comprehensive Testing: Pytest-based test suite with high coverage
๐ Requirements¶
- Docker and Docker Compose
- Python 3.11+
- uv for Python package and environment management
- PostgreSQL (when running locally without Docker)
๐ง Development Setup¶
Docker-based Development¶
The easiest way to get started is using Docker Compose, which sets up all required services including the database.
# From the project root directory
docker compose up -d
This will start:
- Backend API at http://api.localhost
- PostgreSQL database
- Adminer (database management) at http://adminer.localhost
Local Development¶
For a more responsive development experience, you can run the backend locally.
- Set up environment variables
# From the project root directory
cp .env.example .env
# Edit .env with your preferred settings
- Install dependencies
# Navigate to backend directory
cd backend
# Create a virtual environment
uv venv
# Activate the virtual environment
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install dependencies
uv pip install -e .
- Start the development server
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
๐ API Documentation¶
FastAPI automatically generates interactive API documentation:
-
Swagger UI: http://localhost:8000/docs
-
Interactive documentation with request/response examples
-
Try out API endpoints directly from the browser
-
ReDoc: http://localhost:8000/redoc
- Alternative documentation interface
- More readable for complex APIs
๐ Project Structure¶
backend/
โโโ app/
โ โโโ api/
โ โ โโโ deps.py # Dependency injection
โ โ โโโ routes/ # API endpoints
โ โโโ core/ # Core functionality
โ โ โโโ config.py # Configuration
โ โ โโโ security.py # Security utilities
โ โโโ crud/ # CRUD operations
โ โโโ db/ # Database setup
โ โโโ models/ # SQLModel models
โ โโโ schemas/ # Pydantic schemas
โ โโโ tests/ # Tests
โ โโโ main.py # Application entry point
โโโ pyproject.toml # Project dependencies
๐พ Database¶
Migrations¶
The application uses Alembic for database migrations:
# Generate a new migration
alembic revision --autogenerate -m "description"
# Apply migrations
alembic upgrade head
Database Models¶
Models are defined using SQLModel, which combines SQLAlchemy and Pydantic:
from sqlmodel import Field, SQLModel
class User(SQLModel, table=True):
id: int = Field(default=None, primary_key=True)
email: str = Field(unique=True, index=True)
# ...
๐งช Testing¶
The backend includes a comprehensive test suite using pytest:
# Run all tests
pytest
# Run tests with coverage report
pytest --cov=app
# Run specific test file
pytest app/tests/api/routes/test_users.py
๐ Deployment¶
The backend is designed to be deployed as a Docker container to AWS ECS. The deployment is handled automatically by GitHub Actions when changes are pushed to the appropriate branches.
For more information on the deployment process, see the CI/CD Pipeline section in the main README.