Health-Seeker Backend is a FastAPI service that orchestrates healthcare workflows across superadmins, doctors, and patients. It exposes a secured REST API with JWT bearer authentication, role-aware authorization, appointment automation, and audit-friendly background processing.
- Role-driven access controls guarding every endpoint (superadmin, doctor, patient).
- JWT bearer login flow with configurable secrets and token lifetimes.
- Doctor profiles, availability schedules, and capacity-aware appointment booking.
- Patient profiles and discovery endpoints for finding available doctors by speciality and time.
- Superadmin tooling to onboard and manage users with enforced uniqueness and activity flags.
- Background task orchestration and event publication to confirm appointments asynchronously.
- FastAPI hosts versioned REST endpoints under
/api/v1. - SQLAlchemy 2.x models PostgreSQL tables for users, doctor/patient profiles, schedules, and appointments.
- Celery + Redis handle asynchronous appointment confirmation tasks.
- In-memory event bus broadcasts domain events that subscribers persist for audit trails.
- Pydantic v2 schemas provide request/response validation with
from_attributes=TrueORM support.
The project is intentionally modular: routers expose HTTP contracts, services encapsulate business logic, and models/schemas express shared healthcare concepts.
- Python 3.11+
- PostgreSQL 15 (local install or container)
- Redis 7 (for Celery workers)
- Optional: Docker & docker-compose for single-command bootstrapping
-
Clone & install dependencies
python -m venv .venv source .venv/bin/activate pip install --upgrade pip pip install -r requirements.txt -
Configure environment variables
cp .env.example .env
Update at minimum:
DATABASE_URL– points at your PostgreSQL instance.CELERY_BROKER_URL/CELERY_RESULT_BACKEND– Redis endpoints.JWT_SECRET_KEY,JWT_ALGORITHM,ACCESS_TOKEN_EXPIRE_MINUTES– auth secrets.- (Optional)
SUPERADMIN_EMAIL,SUPERADMIN_PASSWORD,SUPERADMIN_FULL_NAMEto auto-seed a superadmin during init.
-
Run PostgreSQL (if needed)
docker run --name health-seeker-postgres \ -p 5432:5432 \ -e POSTGRES_USER=postgres \ -e POSTGRES_PASSWORD=postgres \ -e POSTGRES_DB=health_seeker \ -d postgres:15-alpine
-
Provision the database schema
make init-db # or python -c "from app.db.init_db import init_db; init_db()"
This creates all tables and, when superadmin credentials are provided, seeds a default superadmin user.
-
Run the FastAPI application
make run # Uvicorn serves on http://127.0.0.1:8000Interactive documentation is available at
http://127.0.0.1:8000/docs. -
Start the Celery worker (optional for async tasks)
make worker # or docker compose up workerThe worker processes
schedule_appointmentjobs and updates background task records.
A compose file is provided for local parity with production.
docker compose up --buildServices:
api– FastAPI + Uvicorn (port 8000)worker– Celery worker consuming appointment taskspostgres– PostgreSQL 15 with initial schemaredis– Redis broker/result backend
Stop the stack with docker compose down. Remove persisted volumes if necessary: docker volume rm health-seeker-backend_postgres-data.
- Login:
POST /api/v1/auth/loginwith email/password returns a bearer token and user payload. - Bearer usage: Include
Authorization: Bearer <token>on subsequent requests. - Roles:
superadmin– full administrative rights; can create/update any user, inspect appointments, and act on behalf of patients.doctor– manage their profile, schedules, and appointments they are assigned to.patient– manage their profile, discover doctors, and book appointments for themselves.
- Users are marked
is_active; inactive accounts are denied authentication.
The FastAPI dependencies in app/api/dependencies.py enforce role gates (require_superadmin, require_doctor, require_patient) to keep handlers concise.
- Authenticate with the seeded superadmin credentials.
POST /api/v1/users/to create doctors or patients (password hashed automatically).- Share credentials with end-users; they login via
/auth/login.
- Doctor logs in and sets up their profile via
PUT /api/v1/doctors/me/profile. - Publish availability windows using
POST /api/v1/doctors/me/schedules. - Review upcoming appointments with
GET /api/v1/doctors/me/appointments. - Update appointment status, notes, diagnosis, or prescriptions through
PATCH /api/v1/appointments/{id}.
- Patient updates their profile through
PUT /api/v1/patients/me/profile. - Discover suitable doctors with
GET /api/v1/patients/doctors?specialization=cardiology. - Inspect schedule slots via
GET /api/v1/patients/doctors/{doctor_user_id}/schedules. - Book an appointment using
POST /api/v1/appointments/(requires schedule id and reason). - Track personal appointments with
GET /api/v1/patients/me/appointments.
- Booking triggers an appointment record with
pendingstatus. - A background task enqueues confirmation logic (via Celery) and publishes
appointment.createdevents. - Doctors or superadmins can update statuses (
confirmed,completed,cancelled), add notes, diagnoses, and prescriptions.
- Creating an appointment stores a
BackgroundTaskRecordand optionally dispatches a Celery job (schedule_appointment_task). - Events emitted by
EventBusincludeappointment.createdandappointment.updatedwith contextual payloads. - Audit subscribers persist events to an
audit_logstable for compliance and observability.
app/
├── api/ # FastAPI routers and dependency wiring
├── core/ # Settings, security helpers, event bus
├── db/ # SQLAlchemy session management and bootstrap
├── models/ # ORM models (users, profiles, schedules, appointments, tasks)
├── schemas/ # Pydantic models for requests/responses
├── services/ # Business logic, auth, scheduling, event orchestration
├── subscribers/ # Event subscribers (audit logging, etc.)
└── tasks/ # Celery configuration and background jobs
- Static type hints are provided across the codebase. Add
mypy/ruffas needed for stricter linting. - Use
python -m compileall app(already part of CI scripts) or integratepytestfor behavioural coverage.
- Introduce fine-grained permissions (e.g., per-clinic scoping) atop the current role model.
- Replace the in-memory event bus with Kafka, RabbitMQ, or another distributed broker for multi-service deployments.
- Add notification subscribers (email/SMS) responding to appointment lifecycle events.
- Layer on analytics dashboards consuming the audit/event stream.
Health-Seeker Backend demonstrates how a pragmatic, event-driven FastAPI stack can support clinical-grade scheduling, traceability, and secure access control in a single service.