Backend Setup
This guide walks you through setting up the FastAPI backend for acodeaday.
Prerequisites
Before starting, ensure you have:
- Python 3.12+ installed
uvpackage manager installed- Supabase project created (or local Supabase running)
- Judge0 running in Docker
See Prerequisites for installation instructions.
Clone the Repository
git clone https://github.com/engineeringwithtemi/acodeaday.git
cd acodeaday/backendInstall Dependencies
The project uses uv for dependency management, which reads from pyproject.toml.
uv syncThis will:
- Create a virtual environment (
.venv) - Install all dependencies from
pyproject.toml - Lock dependencies in
uv.lock
To activate the virtual environment:
source .venv/bin/activate # macOS/Linux
.venv\Scripts\activate # WindowsConfigure Environment Variables
Create a .env file in the backend directory:
cp .env.example .envEdit .env with your configuration:
# Database
DATABASE_URL=postgresql+asyncpg://postgres:[PASSWORD]@[HOST]:5432/postgres
# Supabase (if using Supabase cloud)
SUPABASE_URL=https://[PROJECT_ID].supabase.co
SUPABASE_KEY=[YOUR_ANON_KEY]
# Judge0
JUDGE0_URL=http://localhost:2358
# Authentication (Basic Auth)
AUTH_USER_EMAIL=admin
AUTH_PASSWORD=your-secure-password
# Environment
ENVIRONMENT=developmentImportant: Replace the placeholder values with your actual credentials.
Getting Supabase Credentials
- Go to your Supabase project dashboard
- Click "Settings" > "API"
- Copy:
- Project URL →
SUPABASE_URL - anon/public key →
SUPABASE_KEY
- Project URL →
- Click "Database" > "Connection string"
- Copy the URI and convert to async format:
- Change
postgresql://topostgresql+asyncpg:// - Replace
[YOUR-PASSWORD]with your database password
- Change
Run Database Migrations
Apply all database migrations to create the required tables:
uv run alembic upgrade headYou should see output like:
INFO [alembic.runtime.migration] Running upgrade -> 001_initial, Initial schema
INFO [alembic.runtime.migration] Running upgrade 001_initial -> 002_user_progress, Add user progress
...Verify Migrations
Check current migration status:
uv run alembic currentView migration history:
uv run alembic historySeed Initial Problems
Load the problems into the database:
uv run python scripts/seed_problems.py seedThis will:
- Read YAML files from
backend/data/problems/ - Insert problems, test cases, and language-specific code
- Skip problems that already exist (safe to run multiple times)
Seed Specific Problems
# Seed a specific file
uv run python scripts/seed_problems.py seed 001-two-sum.yaml
# Force update existing problems
uv run python scripts/seed_problems.py seed --forceCreate New Problem Template
uv run python scripts/seed_problems.py new my-problem-slug --lang pythonSee Adding Problems for more details.
Start the Development Server
Start the FastAPI server with auto-reload:
uv run uvicorn app.main:app --reloadThe server will start on http://localhost:8000
Verify Backend is Running
Open http://localhost:8000/docs in your browser to see the interactive API documentation (Swagger UI).
Test a simple endpoint:
curl http://localhost:8000/api/problemsYou should see a JSON response with the list of problems.
Run Tests
The backend includes pytest tests for API endpoints and core logic:
# Run all tests
uv run pytest
# Run with coverage
uv run pytest --cov=app
# Run specific test file
uv run pytest tests/test_problems.py
# Run with verbose output
uv run pytest -vCommon Commands
# Install new dependency
uv add package-name
# Install dev dependency
uv add --dev package-name
# Update dependencies
uv sync
# Run alembic commands
uv run alembic upgrade head # Apply migrations
uv run alembic downgrade -1 # Rollback one migration
uv run alembic revision --autogenerate -m "message" # Create new migration
# Run Python scripts
uv run python scripts/seed_problems.py seed
# Start server
uv run uvicorn app.main:app --reload
# Run tests
uv run pytestProject Structure
backend/
├── app/
│ ├── main.py # FastAPI app entry point
│ ├── models.py # SQLAlchemy ORM models
│ ├── schemas.py # Pydantic schemas
│ ├── routes/ # API route handlers
│ ├── services/ # Business logic
│ └── db.py # Database session management
├── alembic/
│ ├── versions/ # Migration files
│ └── env.py # Alembic configuration
├── data/
│ └── problems/ # YAML problem definitions
├── scripts/
│ └── seed_problems.py # Problem seeder CLI
├── tests/ # Pytest tests
├── pyproject.toml # Python dependencies
├── uv.lock # Locked dependencies
└── .env # Environment variablesTroubleshooting
"No module named 'app'"
Make sure you're running commands with uv run:
uv run uvicorn app.main:app --reloadDatabase connection errors
- Verify
DATABASE_URLin.envusespostgresql+asyncpg://(notpostgresql://) - Check your Supabase project is active
- Ensure your IP is allowed in Supabase (Settings > Database > Connection pooling)
Judge0 connection errors
- Verify Judge0 is running:
docker-compose psinbackend/judge0/ - Check
JUDGE0_URLin.envis correct (http://localhost:2358) - Test Judge0 directly:
curl http://localhost:2358/about
Migration errors
# Reset migrations (WARNING: deletes all data)
uv run alembic downgrade base
uv run alembic upgrade headNext Steps
- Frontend Setup - Set up the React frontend
- Judge0 Setup - Configure code execution
- Adding Problems - Add more problems
- API Reference - Explore API endpoints