Project Hub API (FastAPI Backend)
This is the Python backend for the Project Hub platform, built on FastAPI. It acts as the high-performance API layer handling project management, GitHub repository integration, learning paths, gamification, and user tracking.
๐๏ธ Architecture Stack
- Framework: FastAPI (Python 3.12+)
- Database: PostgreSQL (shared with the Next.js frontend)
- ORM: SQLAlchemy 2.0 (using asynchronous
asyncpgbindings) - Auth: Custom JWT integration compatible with NextAuth.js
- Package Manager: uv
๐ Getting Started
1. Prerequisites
- Python 3.12 or newer.
- uv installed (
curl -LsSf https://astral.sh/uv/install.sh | sh). - A running instance of PostgreSQL (which should ideally be configured via the Next.js
frontend.env.local).
2. Environment Configuration
Copy the .env.example file (or create an .env) in the backend-python root.
DATABASE_URL is used by the FastAPI runtime and is normalized to asyncpg automatically. ALEMBIC_DATABASE_URL is optional and can be set when migrations need an explicit sync URL.
Example .env:
# Ensure the Secret Key perfectly matches the Next.js NEXTAUTH_SECRET to decode JWTs
SECRET_KEY="your-nextauth-secret-key"
DATABASE_URL="postgresql://postgres:password@localhost:5432/projecthub1"
ALEMBIC_DATABASE_URL="postgresql+psycopg2://postgres:password@localhost:5432/projecthub1"
BACKEND_CORS_ORIGINS='["http://localhost:3000"]'Neon example:
DATABASE_URL="postgresql://USER:PASSWORD@HOST/neondb?sslmode=require&channel_binding=require"
ALEMBIC_DATABASE_URL="postgresql+psycopg2://USER:PASSWORD@HOST/neondb?sslmode=require"3. Installation
Install all dependencies in a lightning-fast virtual environment using uv:
uv sync # Syncs dependencies using uv.lock4. Running the Development Server
Since the backend uses ASGI, you initiate the application using uvicorn:
uv run uvicorn src.main:app --reloadYou should now be able to visit http://localhost:8000/docs to view the interactive Swagger/OpenAPI documentation.
๐๏ธ Database Management
Note on Migrations: The database schema is strictly owned and migrated by Prisma in the
frontendfolder. Do not runalembic upgrade headto apply schema changes unless explicitly separating the backends.
If you are developing a new feature:
- First, modify the
frontend/prisma/schema.prismafile. - Run
npx prisma db pushinside the frontend directory. - Update the equivalent Python SQLAlchemy definitions inside
backend-python/src/models/to match.
๐งช Testing
The test suite utilizes pytest with pytest-asyncio and httpx.
To execute the tests:
uv run pytest tests/๐ Project Collection Scraper
The Python backend now includes a GitHub project discovery scraper that is aligned to the platform's five collection domains:
- Web Development
- Artificial Intelligence
- Machine Learning
- Data Science
- Cybersecurity
Run it from the backend-python folder:
uv run python scripts/find_projects.py --domain all --target 20 --output output/candidates.jsonUseful flags:
--require-demokeeps only repositories with a detected live demo/homepage--use-aifills case-study style fields using the configured AI service--upsert-dbwrites the curated results into the shared PostgreSQL database
Example:
uv run python scripts/find_projects.py --domain web-development --target 50 --require-demo --upsert-db๐ Railway Deployment With Docker
This backend can be deployed to Railway using the Docker configuration in backend-python/Dockerfile and backend-python/railway.json.
Files
- Dockerfile - container build for the FastAPI app
- railway.json - Railway build and deploy settings
- .dockerignore - keeps the image small
Railway setup
- Create a new Railway service from this repository.
- Set the service root directory to backend-python
- Railway will detect railway.json and build using Docker.
- Add the required environment variables:
DATABASE_URL=postgresql://...
ALEMBIC_DATABASE_URL=postgresql+psycopg2://...
SECRET_KEY=your-nextauth-secret
BACKEND_CORS_ORIGINS=["https://your-frontend-domain.com"]
GITHUB_TOKEN=optional
CEREBRAS_API_KEY=optional
GEMINI_API_KEY=optionalLocal Docker test
docker build -t project-hub-backend-python .
docker run --rm -p 8000:8000 \
-e DATABASE_URL="postgresql://..." \
-e ALEMBIC_DATABASE_URL="postgresql+psycopg2://..." \
-e SECRET_KEY="your-secret" \
-e BACKEND_CORS_ORIGINS='["http://localhost:3000"]' \
project-hub-backend-pythonRailway injects PORT automatically, and the container command binds uvicorn to 0.0.0.0 on that port.