Learning project: a small API that tracks lab samples and the results measured on them by devices.
Built step by step (see docs/). Stack: Python, FastAPI, SQLite (plain SQL), pytest.
pip install -r requirements.txt -r requirements-dev.txt
python -m app.keys create --name me --role admin # prints your API key once
uvicorn app.main:app --reloadOpen http://127.0.0.1:8000/docs, click Authorize and paste the key. Every endpoint except /health needs X-API-Key.
For HTTPS on the lab network with Docker see docs/04-security.md.
pytest -qapp/schema.sql: the data model, in plain SQLapp/security.py,app/keys.py: API keys, roles, key CLIapp/plates.py: plates, wells, bulk readings, workflow events (seedocs/06-plates.md)screening/,adapters/plate_files.py: simulated weekly drug screen (seedocs/07-screening.md)dashboard/: Streamlit dashboard, read-only (seedocs/08-dashboard.md);py -m screening.demoruns everything with one commanddeploy/,Dockerfile,docker-compose.yml: certificates and LAN deploymentapp/db.py: connection per request (a FastAPI dependency)app/models.py: request/response shapes + validation (Pydantic)app/main.py: endpointsadapters/: device adapters + shared client (retry, spool);simulators/: fake devices (seedocs/03-devices.md)tests/: API, adapter and SQL-injection tests
- Client sends
POST /sampleswith JSON. - FastAPI matches the route and validates the body against
SampleIn(bad input -> 422, never reaches SQL). - The handler runs a parameterised SQL statement (
?placeholders), which is injection-safe. - The row is read back, converted to the
Sampleresponse model, returned as JSON with status 201.
- New resource: add a table to
schema.sql, models tomodels.py, endpoints tomain.py, tests. - Changing the schema later: move to migrations (Alembic) once data matters.
- Roadmap: plates resource, rate limiting, key expiry, PostgreSQL (see
docs/04-security.md).