Skip to content

๐Ÿ”ฅ DevOps Demo Application - Backend

FastAPI Python Docker PostgreSQL

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

  • 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.

  1. Set up environment variables
# From the project root directory
cp .env.example .env
# Edit .env with your preferred settings
  1. 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 .
  1. 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.