# vuhnger/backend architecture

> Architecture diagram of the GitHub repository vuhnger/backend, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/vuhnger/backend
- Repository: https://github.com/vuhnger/backend

## Overview

This is a multi-service FastAPI backend, principally for a Strava activity tracker, with companion WakaTime, project-management, site-presence, and n8n-proxy APIs. The main user journey authorizes Strava, syncs activity history and cached aggregates, then serves statistics, activity lists, and heatmaps. WakaTime follows a similar OAuth-and-cached-stats flow; the other services provide portfolio CRUD, visitor notifications/live cursors, and an upstream health proxy. Shared infrastructure supplies app hardening, configuration, persistence, authentication, encryption, and OAuth helpers. Some route and cursor details are unsampled; edges rely on excerpts, index references, or README statements.

Main entry points: [`apps/strava/main.py`](https://github.com/vuhnger/backend/blob/main/apps/strava/main.py) (Activity API), [`apps/wakatime/main.py`](https://github.com/vuhnger/backend/blob/main/apps/wakatime/main.py) (Stats API), [`apps/shared/database.py`](https://github.com/vuhnger/backend/blob/main/apps/shared/database.py) (Database sessions), [`apps/strava/tasks.py`](https://github.com/vuhnger/backend/blob/main/apps/strava/tasks.py) (Activity sync)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_strava["Strava tracking"]
  node_strava_routes["Activity API<br/>[main.py]"]
  node_strava_sync["Activity sync<br/>[tasks.py]"]
  node_strava_client["Provider client<br/>[client.py]"]
  node_strava_factory["Client factory<br/>[client_factory.py]"]
  node_strava_models["Activity models<br/>[models.py]"]
  node_strava_geo["Heatmap geometry<br/>[geometry.py]"]
end

subgraph group_waka["WakaTime stats"]
  node_waka_routes["Stats API<br/>[main.py]"]
  node_waka_sync["Stats sync<br/>[tasks.py]"]
  node_waka_client["Provider client<br/>[client.py]"]
  node_waka_models["Stats models<br/>[models.py]"]
end

subgraph group_shared["Shared infrastructure"]
  node_app_factory["App factory<br/>[app_factory.py]"]
  node_settings["Typed settings<br/>[config.py]"]
  node_database["Database sessions<br/>[database.py]"]
  node_upsert["Atomic upserts<br/>[upsert.py]"]
  node_oauth_tokens["Token refresh<br/>[oauth_tokens.py]"]
  node_oauth_state["OAuth state<br/>[oauth_state.py]"]
  node_oauth_owner["Account ownership<br/>[oauth_owner.py]"]
  node_encryption["Token encryption<br/>[encryption.py]"]
  node_auth["API key auth<br/>[auth.py]"]
end

subgraph group_portfolio["Portfolio services"]
  node_projects_api["Project CRUD<br/>[main.py]"]
  node_projects_models["Project records<br/>[models.py]"]
  node_project_schema["Project schemas<br/>[schemas.py]"]
  node_n8n_proxy["n8n health proxy<br/>[main.py]"]
end

subgraph group_site["Site presence"]
  node_site_api["Visitor beacon<br/>[main.py]"]
  node_cursors["Live cursors<br/>[cursors.py]"]
  node_notifications["Notifier<br/>[notifications.py]"]
end

node_visitor(("API consumer"))
node_strava_api{{"Strava API"}}
node_wakatime_api{{"WakaTime API"}}
node_postgres[("PostgreSQL")]
node_geo_service{{"Geo lookup"}}

node_visitor -->|"requests"| node_strava_routes
node_strava_routes -->|"authorizes and fetches"| node_strava_api
node_strava_routes -->|"validates state"| node_oauth_state
node_strava_routes -->|"checks owner"| node_oauth_owner
node_strava_routes -->|"creates client"| node_strava_factory
node_strava_routes -->|"triggers refresh"| node_strava_sync
node_strava_routes -->|"queries"| node_strava_models
node_strava_routes -->|"uses geometry"| node_strava_geo
node_strava_sync -->|"fetches data"| node_strava_client
node_strava_client -->|"uses"| node_strava_factory
node_strava_sync -->|"reads and writes"| node_strava_models
node_strava_sync -->|"upserts records"| node_upsert
node_strava_routes -->|"builds app"| node_app_factory
node_strava_sync -->|"opens session"| node_database
node_visitor -->|"requests"| node_waka_routes
node_waka_routes -->|"exchanges tokens and fetches"| node_wakatime_api
node_waka_routes -->|"validates state"| node_oauth_state
node_waka_routes -->|"checks owner"| node_oauth_owner
node_waka_routes -->|"refreshes tokens"| node_oauth_tokens
node_waka_routes -->|"triggers refresh"| node_waka_sync
node_waka_routes -->|"queries"| node_waka_models
node_waka_routes -->|"builds app"| node_app_factory
node_waka_sync -->|"fetches data"| node_waka_client
node_waka_sync -->|"reads and writes"| node_waka_models
node_waka_sync -->|"upserts records"| node_upsert
node_waka_sync -->|"opens session"| node_database
node_strava_models -->|"encrypts tokens"| node_encryption
node_waka_models -->|"encrypts tokens"| node_encryption
node_oauth_tokens -->|"opens isolated session"| node_database
node_oauth_tokens -.->|"refreshes grant"| node_wakatime_api
node_oauth_tokens -.->|"refreshes grant"| node_strava_api
node_upsert -->|"executes statements"| node_database
node_visitor -->|"manages projects"| node_projects_api
node_projects_api -->|"queries and updates"| node_projects_models
node_projects_api -->|"validates responses"| node_project_schema
node_projects_api -->|"uses session"| node_database
node_projects_api -->|"protects admin routes"| node_auth
node_projects_models -->|"uses ORM base"| node_database
node_n8n_proxy -->|"builds app"| node_app_factory
node_n8n_proxy -->|"reads upstream config"| node_settings
node_visitor -->|"sends visit beacon"| node_site_api
node_visitor -->|"shares cursor presence"| node_cursors
node_site_api -->|"sends visit alert"| node_notifications
node_site_api -->|"looks up location"| node_geo_service
node_site_api -->|"hosts cursor routes"| node_cursors
node_site_api -->|"builds app"| node_app_factory
node_database -->|"reads connection config"| node_settings
node_auth -->|"reads key config"| node_settings

click node_strava_routes "https://github.com/vuhnger/backend/blob/main/apps/strava/main.py"
click node_strava_sync "https://github.com/vuhnger/backend/blob/main/apps/strava/tasks.py"
click node_strava_client "https://github.com/vuhnger/backend/blob/main/apps/strava/client.py"
click node_strava_factory "https://github.com/vuhnger/backend/blob/main/apps/strava/client_factory.py"
click node_strava_models "https://github.com/vuhnger/backend/blob/main/apps/strava/models.py"
click node_strava_geo "https://github.com/vuhnger/backend/blob/main/apps/strava/geometry.py"
click node_waka_routes "https://github.com/vuhnger/backend/blob/main/apps/wakatime/main.py"
click node_waka_sync "https://github.com/vuhnger/backend/blob/main/apps/wakatime/tasks.py"
click node_waka_client "https://github.com/vuhnger/backend/blob/main/apps/wakatime/client.py"
click node_waka_models "https://github.com/vuhnger/backend/blob/main/apps/wakatime/models.py"
click node_app_factory "https://github.com/vuhnger/backend/blob/main/apps/shared/app_factory.py"
click node_settings "https://github.com/vuhnger/backend/blob/main/apps/shared/config.py"
click node_database "https://github.com/vuhnger/backend/blob/main/apps/shared/database.py"
click node_upsert "https://github.com/vuhnger/backend/blob/main/apps/shared/upsert.py"
click node_oauth_tokens "https://github.com/vuhnger/backend/blob/main/apps/shared/oauth_tokens.py"
click node_oauth_state "https://github.com/vuhnger/backend/blob/main/apps/shared/oauth_state.py"
click node_oauth_owner "https://github.com/vuhnger/backend/blob/main/apps/shared/oauth_owner.py"
click node_encryption "https://github.com/vuhnger/backend/blob/main/apps/shared/encryption.py"
click node_auth "https://github.com/vuhnger/backend/blob/main/apps/shared/auth.py"
click node_projects_api "https://github.com/vuhnger/backend/blob/main/apps/projects/main.py"
click node_projects_models "https://github.com/vuhnger/backend/blob/main/apps/projects/models.py"
click node_project_schema "https://github.com/vuhnger/backend/blob/main/apps/projects/schemas.py"
click node_n8n_proxy "https://github.com/vuhnger/backend/blob/main/apps/n8n/main.py"
click node_site_api "https://github.com/vuhnger/backend/blob/main/apps/site/main.py"
click node_cursors "https://github.com/vuhnger/backend/blob/main/apps/site/cursors.py"
click node_notifications "https://github.com/vuhnger/backend/blob/main/apps/shared/notifications.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_strava_routes,node_strava_sync,node_strava_client,node_strava_factory,node_strava_models,node_strava_geo toneBlue
class node_waka_routes,node_waka_sync,node_waka_client,node_waka_models,node_postgres toneAmber
class node_app_factory,node_settings,node_database,node_upsert,node_oauth_tokens,node_oauth_state,node_oauth_owner,node_encryption,node_auth,node_visitor,node_strava_api,node_wakatime_api toneMint
class node_projects_api,node_projects_models,node_project_schema,node_n8n_proxy toneRose
class node_site_api,node_cursors,node_notifications,node_geo_service toneIndigo
```

## Components

### Strava tracking

- [Activity API](https://github.com/vuhnger/backend/blob/main/apps/strava/main.py): `apps/strava/main.py`
- [Activity sync](https://github.com/vuhnger/backend/blob/main/apps/strava/tasks.py): `apps/strava/tasks.py`
- [Provider client](https://github.com/vuhnger/backend/blob/main/apps/strava/client.py): `apps/strava/client.py`
- [Client factory](https://github.com/vuhnger/backend/blob/main/apps/strava/client_factory.py): `apps/strava/client_factory.py`
- [Activity models](https://github.com/vuhnger/backend/blob/main/apps/strava/models.py): `apps/strava/models.py`
- [Heatmap geometry](https://github.com/vuhnger/backend/blob/main/apps/strava/geometry.py): `apps/strava/geometry.py`

### WakaTime stats

- [Stats API](https://github.com/vuhnger/backend/blob/main/apps/wakatime/main.py): `apps/wakatime/main.py`
- [Stats sync](https://github.com/vuhnger/backend/blob/main/apps/wakatime/tasks.py): `apps/wakatime/tasks.py`
- [Provider client](https://github.com/vuhnger/backend/blob/main/apps/wakatime/client.py): `apps/wakatime/client.py`
- [Stats models](https://github.com/vuhnger/backend/blob/main/apps/wakatime/models.py): `apps/wakatime/models.py`

### Shared infrastructure

- [App factory](https://github.com/vuhnger/backend/blob/main/apps/shared/app_factory.py): `apps/shared/app_factory.py`
- [Typed settings](https://github.com/vuhnger/backend/blob/main/apps/shared/config.py): `apps/shared/config.py`
- [Database sessions](https://github.com/vuhnger/backend/blob/main/apps/shared/database.py): `apps/shared/database.py`
- [Atomic upserts](https://github.com/vuhnger/backend/blob/main/apps/shared/upsert.py): `apps/shared/upsert.py`
- [Token refresh](https://github.com/vuhnger/backend/blob/main/apps/shared/oauth_tokens.py): `apps/shared/oauth_tokens.py`
- [OAuth state](https://github.com/vuhnger/backend/blob/main/apps/shared/oauth_state.py): `apps/shared/oauth_state.py`
- [Account ownership](https://github.com/vuhnger/backend/blob/main/apps/shared/oauth_owner.py): `apps/shared/oauth_owner.py`
- [Token encryption](https://github.com/vuhnger/backend/blob/main/apps/shared/encryption.py): `apps/shared/encryption.py`
- [API key auth](https://github.com/vuhnger/backend/blob/main/apps/shared/auth.py): `apps/shared/auth.py`

### Portfolio services

- [Project CRUD](https://github.com/vuhnger/backend/blob/main/apps/projects/main.py): `apps/projects/main.py`
- [Project records](https://github.com/vuhnger/backend/blob/main/apps/projects/models.py): `apps/projects/models.py`
- [Project schemas](https://github.com/vuhnger/backend/blob/main/apps/projects/schemas.py): `apps/projects/schemas.py`
- [n8n health proxy](https://github.com/vuhnger/backend/blob/main/apps/n8n/main.py): `apps/n8n/main.py`

### Site presence

- [Visitor beacon](https://github.com/vuhnger/backend/blob/main/apps/site/main.py): `apps/site/main.py`
- [Live cursors](https://github.com/vuhnger/backend/blob/main/apps/site/cursors.py): `apps/site/cursors.py`
- [Notifier](https://github.com/vuhnger/backend/blob/main/apps/shared/notifications.py): `apps/shared/notifications.py`

### Other components

- **API consumer**
- **Strava API**
- **WakaTime API**
- **PostgreSQL**
- **Geo lookup**

## Connections

- API consumer → Activity API: requests (evidence: `README.md`)
- Activity API → Strava API: authorizes and fetches (evidence: `README.md`)
- Activity API → OAuth state: validates state (evidence: `apps/strava/main.py`)
- Activity API → Account ownership: checks owner (evidence: `apps/strava/main.py`)
- Activity API → Client factory: creates client (evidence: `apps/strava/main.py`)
- Activity API → Activity sync: triggers refresh (evidence: `apps/strava/main.py`)
- Activity API → Activity models: queries (evidence: `apps/strava/main.py`)
- Activity API → Heatmap geometry: uses geometry (evidence: `apps/strava/main.py`)
- Activity sync → Provider client: fetches data (evidence: `apps/strava/tasks.py`)
- Provider client → Client factory: uses (evidence: `apps/strava/client.py`)
- Activity sync → Activity models: reads and writes (evidence: `apps/strava/tasks.py`)
- Activity sync → Atomic upserts: upserts records (evidence: `apps/strava/tasks.py`)
- Activity API → App factory: builds app (evidence: `apps/strava/main.py`)
- Activity sync → Database sessions: opens session (evidence: `apps/strava/tasks.py`)
- API consumer → Stats API: requests (evidence: `README.md`)
- Stats API → WakaTime API: exchanges tokens and fetches (evidence: `apps/wakatime/main.py`)
- Stats API → OAuth state: validates state (evidence: `apps/wakatime/main.py`)
- Stats API → Account ownership: checks owner (evidence: `apps/wakatime/main.py`)
- Stats API → Token refresh: refreshes tokens (evidence: `apps/wakatime/main.py`)
- Stats API → Stats sync: triggers refresh (evidence: `apps/wakatime/main.py`)
- Stats API → Stats models: queries (evidence: `apps/wakatime/main.py`)
- Stats API → App factory: builds app (evidence: `apps/wakatime/main.py`)
- Stats sync → Provider client: fetches data (evidence: `apps/wakatime/tasks.py`)
- Stats sync → Stats models: reads and writes (evidence: `apps/wakatime/tasks.py`)
- Stats sync → Atomic upserts: upserts records (evidence: `apps/wakatime/tasks.py`)
- Stats sync → Database sessions: opens session (evidence: `apps/wakatime/tasks.py`)
- Activity models → Token encryption: encrypts tokens (evidence: `apps/strava/models.py`)
- Stats models → Token encryption: encrypts tokens (evidence: `apps/wakatime/models.py`)
- Token refresh → Database sessions: opens isolated session (evidence: `apps/shared/oauth_tokens.py`)
- Token refresh → WakaTime API: refreshes grant
- Token refresh → Strava API: refreshes grant
- Atomic upserts → Database sessions: executes statements (evidence: `apps/shared/upsert.py`)
- API consumer → Project CRUD: manages projects (evidence: `README.md`)
- Project CRUD → Project records: queries and updates (evidence: `apps/projects/main.py`)
- Project CRUD → Project schemas: validates responses (evidence: `apps/projects/main.py`)
- Project CRUD → Database sessions: uses session (evidence: `apps/projects/main.py`)
- Project CRUD → API key auth: protects admin routes (evidence: `apps/projects/main.py`)
- Project records → Database sessions: uses ORM base (evidence: `apps/projects/models.py`)
- n8n health proxy → App factory: builds app (evidence: `apps/n8n/main.py`)
- n8n health proxy → Typed settings: reads upstream config (evidence: `apps/n8n/main.py`)
- API consumer → Visitor beacon: sends visit beacon (evidence: `apps/site/main.py`)
- API consumer → Live cursors: shares cursor presence
- Visitor beacon → Notifier: sends visit alert (evidence: `apps/site/main.py`)
- Visitor beacon → Geo lookup: looks up location (evidence: `apps/site/main.py`)
- Visitor beacon → Live cursors: hosts cursor routes (evidence: `apps/site/main.py`)
- Visitor beacon → App factory: builds app (evidence: `apps/site/main.py`)
- Database sessions → Typed settings: reads connection config (evidence: `apps/shared/database.py`)
- API key auth → Typed settings: reads key config (evidence: `apps/shared/auth.py`)
