The backend service for CPF Board TextUs, built with FastAPI and SQLModel.
- Framework: FastAPI
- ORM: SQLModel (SQLAlchemy-based)
- Database:
- Development: SQLite
- Production: PostgreSQL (Amazon RDS)
- Package Management: uv
- Migration Tool: Alembic
- Containerization: Docker
Ensure that you are running commands in this backend/ folder, i.e. cd backend.
- Install uv package manager:
curl -LsSf https://astral.sh/uv/install.sh | sh- Set up environment variables:
cp .env.example .env
# Edit .env with your configuration- Create and activate virtual environment:
uv venv
source .venv/bin/activate # On Unix/macOS
# or
.venv\Scripts\activate # On Windows- Install dependencies:
uv syncuv run fastapi devuv run fastapi run app/main.pyTo run with reduced logging verbosity (recommended for development):
uv run uvicorn app.main:app --log-config uvicorn_config.pyThis uses a custom logging configuration that greatly simplifies error output and reduces noise in the console logs.
Database configuration is controlled by environment variables:
-
Development:
ENVIRONMENT=development(SQLite)DATABASE_URL=sqlite+aiosqlite:///./data/textus.db -
Production:
ENVIRONMENT=production(PostgreSQL)DATABASE_URL=postgresql://user:password@host:5432/dbname
- Create and apply database migrations:
uv run alembic upgrade head- Seed the database with initial test data (development only):
uv run python scripts/init_all.pyThis will create test users with the following credentials:
- Admin: email=
admin@example.com, password=admin - Trainer: email=
trainer@example.com, password=123 - Trainee: email=
test@example.com, password=test
We use Alembic for database migrations:
- Create a new migration:
uv run alembic revision --autogenerate -m "Description of changes"- Apply migrations:
uv run alembic upgrade head- Rollback migrations:
uv run alembic downgrade -1 # Rollback one step
uv run alembic downgrade base # Rollback all migrationsWe use pytest for our test suite. Tests are completed in tests/
- Run the test suite:
pytest testsWhen the server is running, access the API documentation at:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
backend/
├── alembic/ # Database migrations
├── app/
│ ├── core/ # Core functionality
│ ├── models/ # SQLModel definitions
│ ├── routers/ # API endpoints
│ ├── services/ # Business logic
│ └── main.py # Application entry point
│ ├── rubrics/ # Rubrics management
├── data/ # SQLite database (dev)
├── scripts/ # Utility scripts
├── tests/ # Test suite
├── .env.example # Environment variables template
├── Dockerfile # Container configuration
└── pyproject.toml # Project metadata
-
Database Connection Errors
- Verify DATABASE_URL in .env
- Check database service is running
- Ensure correct permissions
-
Migration Errors
- Run
alembic currentto check state - Compare local with
alembic history - Clear migration history if needed
- Run
-
Create detailed pull requests