Production-style FastAPI backend skeleton with async SQLAlchemy, Pydantic, and dependency injection.
- Python 3.11+
- PostgreSQL (for async database support)
-
Create a virtual environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
Copy environment configuration:
cp .env.example .env
Edit
.envwith yourDATABASE_URLand other settings. -
Run the server:
uvicorn app.main:app --reload
Or:
python -m app.main
GET /health- Health check (returns{"status": "ok"})POST /workspaces- Create a workspaceGET /workspaces- List all workspacesPOST /sources/text- Create a text sourcePOST /sources/url- Fetch a webpage, extract readable text, and create a URL sourceGET /workspaces/{workspace_id}/sources- List sources in a workspaceGET /sources/{id}- Get source details withchunk_countGET /chunks/{source_id}- List chunks for a sourcePOST /retrieve- Retrieve semantically similar chunks within a workspaceDELETE /sources/{id}- Delete a sourceGET /docs- Swagger UIGET /redoc- ReDoc documentation
FAISS indexes are persisted locally under data/faiss_indexes/ and are ignored by git.
-
Start PostgreSQL (e.g. via Docker):
docker compose up -d
-
Ensure
.envhas:DATABASE_URL=postgresql+asyncpg://postgres:postgres@localhost:5432/clearview -
Start the API:
uvicorn app.main:app --reload
Tables are created automatically on startup via
init_db(). -
Run the test script:
python scripts/test_workspaces_and_sources.py
Or with curl + jq:
chmod +x scripts/test_workspaces_and_sources.sh ./scripts/test_workspaces_and_sources.sh
app/
├── main.py # FastAPI app entry point
├── api/
│ └── routes.py # API route definitions
├── core/
│ ├── config.py # Pydantic settings from .env
│ ├── logging_config.py
│ └── deps.py # Dependency injection
├── db/
│ ├── base.py # SQLAlchemy declarative base
│ └── session.py # Async engine & session factory
├── models/ # SQLAlchemy models
└── services/ # Business logic layer
MIT