# nhamonin/widget-dashboard architecture

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

- Interactive diagram: https://gitdiagram.com/nhamonin/widget-dashboard
- Repository: https://github.com/nhamonin/widget-dashboard

## Overview

1. **Monorepo shape**
   - This is a pnpm workspace with three main codebases:
     - `packages/shared`: the contract layer
     - `apps/server`: the Fastify API and persistence layer
     - `apps/web`: the React SPA

2. **Single source of truth for the data model**
   - `packages/shared` holds the Zod schemas and inferred types for widgets and HTTP payloads.
   - Both server and web import these shared definitions, so validation, runtime parsing, and TypeScript types stay aligned.
   - The same schemas also feed API docs generation on the backend.

3. **Backend request path**
   - The server is a Fastify 5 app in `apps/server/src/server.ts`.
   - Requests enter through route modules in `src/routes`:
     - `/health` for infrastructure checks
     - `/api/widgets` for the widget resource
   - Route handlers delegate to services, which delegate to repositories, which talk to SQLite.

4. **Backend layering and responsibilities**
   - `services/widget.service.ts` contains business rules such as widget creation, update rules, and text-edit restrictions.
   - `repositories/widget.repo.ts` isolates persistence queries and record mapping.
   - `lib/chart-data.ts` is the server-side generator for random chart points; the data is created once and then stored, so reloads remain stable.

5. **Persistence boundary**
   - The backend uses Node’s built-in `node:sqlite` with a persistent database file.
   - The README indicates WAL mode and a single-writer deployment model.
   - Widget ordering is insertion-based; deletions leave gaps rather than renumbering everything, which matters for the dashboard layout.

6. **Backend cross-cutting concerns**
   - Custom Fastify plugins handle JSON body parsing, centralized error formatting, OpenAPI exposure, and per-IP rate limiting.
   - Errors are normalized to `{ error: { message, code } }`.
   - The API is limited to 100 requests per minute per IP, which is an architectural boundary the frontend must tolerate.

7. **Frontend application shape**
   - The SPA lives in `apps/web` and is built with React 18 + Vite.
   - `main.tsx` bootstraps the app, and `providers/AppProviders.tsx` likely wires shared client concerns such as TanStack Query and notifications.
   - The app is a client-side dashboard, not a server-rendered page app.

8. **Frontend data flow**
   - `src/lib/api-client.ts` centralizes browser calls to the backend API.
   - `src/hooks/widgets.ts` uses TanStack Query to fetch widgets and run create/update/delete mutations.
   - This gives each widget or action its own loading/error state rather than one global screen state.

9. **Dashboard and widget rendering**
   - `components/dashboard` owns the board layout: empty state, grid, widget cards, skeletons, and the add-widget control.
   - `components/widgets` renders type-specific content:
     - charts via Recharts for `line` and `bar`
     - inline-editable text for `text`
   - The UI enforces the product rules described in the README, including save confirmation, delete confirmation, and the 1,000-character cap for text.

10. **Shared UI and presentation layer**
   - The app uses shadcn/ui-style components under `components/ui` for dialogs, buttons, cards, menus, skeletons, and toasts.
   - `src/lib/widget-meta.ts` and `src/lib/skeleton-count.ts` support view logic such as widget-specific labels and responsive loading placeholders.
   - Styling is handled through `index.css` plus component-level composition, with the dashboard designed for a three-column responsive grid.

11. **Routing and deployment boundary**
   - The browser always calls `/api/*` on the same origin.
   - In production, Vercel serves the SPA and rewrites `/api` to the Fly.io backend.
   - Locally, `docker-compose.yml` plus Caddy provide the same single-origin shape by serving the SPA and proxying `/api` to Fastify.
   - This avoids CORS entirely and keeps the frontend deployment independent from the API deployment.

12. **Operational and test boundaries**
   - The backend is containerized with its own Dockerfile and entrypoint, and Fly.io mounts a persistent volume for SQLite.
   - The frontend also has its own Dockerfile and Vercel config.
   - Tests are split by layer: backend `node:test`, web Vitest/Testing Library, plus CI for lint, typecheck, build, and a backend smoke test.

Main entry points: [`apps/server/src/server.ts`](https://github.com/nhamonin/widget-dashboard/blob/main/apps/server/src/server.ts) (Fastify app), [`apps/web/src/main.tsx`](https://github.com/nhamonin/widget-dashboard/blob/main/apps/web/src/main.tsx) (App bootstrap), [`apps/server/src/services/widget.service.ts`](https://github.com/nhamonin/widget-dashboard/blob/main/apps/server/src/services/widget.service.ts) (Widget service), [`pnpm-workspace.yaml`](https://github.com/nhamonin/widget-dashboard/blob/main/pnpm-workspace.yaml) (pnpm workspace)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_workspace["Workspace"]
  node_workspace_root["pnpm workspace<br/>monorepo"]
end

subgraph group_shared["Shared Contracts"]
  node_shared_widget["Widget schema<br/>zod contract<br/>[widget.ts]"]
  node_shared_http["HTTP schema<br/>zod contract<br/>[http.ts]"]
  node_shared_index["Shared exports<br/>package entry<br/>[index.ts]"]
end

subgraph group_server["Server"]
  node_server_app["Fastify app<br/>http server<br/>[server.ts]"]
  node_server_routes["Routes<br/>routing layer<br/>[widgets.ts]"]
  node_server_service["Widget service<br/>business logic<br/>[widget.service.ts]"]
  node_server_repo["Widget repo<br/>persistence adapter<br/>[widget.repo.ts]"]
  node_server_db[("SQLite DB<br/>database<br/>[index.ts]")]
  node_server_chart["Chart data<br/>data generator<br/>[chart-data.ts]"]
  node_server_plugins["Fastify plugins<br/>cross-cutting<br/>[openapi.ts]"]
end

subgraph group_web["Web"]
  node_web_main["App bootstrap<br/>spa entry<br/>[main.tsx]"]
  node_web_providers["App providers<br/>client wiring<br/>[AppProviders.tsx]"]
  node_web_api["API client<br/>browser client<br/>[api-client.ts]"]
  node_web_hooks["Widgets hook<br/>query state<br/>[widgets.ts]"]
  node_web_dashboard["Dashboard<br/>ui composition<br/>[DashboardGrid.tsx]"]
  node_web_widgets["Widget views<br/>ui components<br/>[ChartWidget.tsx]"]
  node_web_meta["Widget meta<br/>view logic<br/>[widget-meta.ts]"]
end

subgraph group_deploy["Deployment"]
  node_deploy_route["Same-origin API<br/>routing boundary"]
  node_deploy_local["Local proxy<br/>dev deployment<br/>[docker-compose.yml]"]
  node_deploy_prod["Prod deploy<br/>production deployment<br/>[fly.toml]"]
end

node_workspace_root -->|"workspace contains"| node_shared_index
node_workspace_root -->|"workspace contains"| node_server_app
node_workspace_root -->|"workspace contains"| node_web_main
node_shared_index -->|"re-exports"| node_shared_widget
node_shared_index -->|"re-exports"| node_shared_http
node_server_app -->|"uses"| node_server_plugins
node_server_app -->|"mounts"| node_server_routes
node_server_routes -->|"delegates"| node_server_service
node_server_service -->|"persists via"| node_server_repo
node_server_repo -->|"queries"| node_server_db
node_server_service -->|"creates"| node_server_chart
node_server_app -->|"validates with"| node_shared_http
node_server_service -->|"validates with"| node_shared_widget
node_web_main -->|"wraps with"| node_web_providers
node_web_main -->|"renders"| node_web_dashboard
node_web_hooks -->|"calls"| node_web_api
node_web_dashboard -->|"reads"| node_web_hooks
node_web_widgets -->|"uses"| node_web_meta
node_web_dashboard -->|"renders"| node_web_widgets
node_web_api -->|"shares contract"| node_shared_http
node_web_widgets -->|"shares contract"| node_shared_widget
node_deploy_route -->|"supports"| node_deploy_local
node_deploy_route -->|"supports"| node_deploy_prod
node_deploy_local -->|"proxies to"| node_server_app
node_deploy_local -->|"serves"| node_web_main
node_deploy_prod -->|"persists on"| node_server_db
node_deploy_prod -->|"hosts"| node_web_main

click node_workspace_root "https://github.com/nhamonin/widget-dashboard/blob/main/pnpm-workspace.yaml"
click node_shared_widget "https://github.com/nhamonin/widget-dashboard/blob/main/packages/shared/src/widget.ts"
click node_shared_http "https://github.com/nhamonin/widget-dashboard/blob/main/packages/shared/src/http.ts"
click node_shared_index "https://github.com/nhamonin/widget-dashboard/blob/main/packages/shared/src/index.ts"
click node_server_app "https://github.com/nhamonin/widget-dashboard/blob/main/apps/server/src/server.ts"
click node_server_routes "https://github.com/nhamonin/widget-dashboard/blob/main/apps/server/src/routes/widgets.ts"
click node_server_service "https://github.com/nhamonin/widget-dashboard/blob/main/apps/server/src/services/widget.service.ts"
click node_server_repo "https://github.com/nhamonin/widget-dashboard/blob/main/apps/server/src/repositories/widget.repo.ts"
click node_server_db "https://github.com/nhamonin/widget-dashboard/blob/main/apps/server/src/db/index.ts"
click node_server_chart "https://github.com/nhamonin/widget-dashboard/blob/main/apps/server/src/lib/chart-data.ts"
click node_server_plugins "https://github.com/nhamonin/widget-dashboard/blob/main/apps/server/src/plugins/openapi.ts"
click node_web_main "https://github.com/nhamonin/widget-dashboard/blob/main/apps/web/src/main.tsx"
click node_web_providers "https://github.com/nhamonin/widget-dashboard/blob/main/apps/web/src/providers/AppProviders.tsx"
click node_web_api "https://github.com/nhamonin/widget-dashboard/blob/main/apps/web/src/lib/api-client.ts"
click node_web_hooks "https://github.com/nhamonin/widget-dashboard/blob/main/apps/web/src/hooks/widgets.ts"
click node_web_dashboard "https://github.com/nhamonin/widget-dashboard/blob/main/apps/web/src/components/dashboard/DashboardGrid.tsx"
click node_web_widgets "https://github.com/nhamonin/widget-dashboard/blob/main/apps/web/src/components/widgets/ChartWidget.tsx"
click node_web_meta "https://github.com/nhamonin/widget-dashboard/blob/main/apps/web/src/lib/widget-meta.ts"
click node_deploy_route "https://github.com/nhamonin/widget-dashboard/tree/main/Caddyfile"
click node_deploy_local "https://github.com/nhamonin/widget-dashboard/blob/main/docker-compose.yml"
click node_deploy_prod "https://github.com/nhamonin/widget-dashboard/blob/main/fly.toml"

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_workspace_root toneBlue
class node_shared_widget,node_shared_http,node_shared_index toneAmber
class node_server_app,node_server_routes,node_server_service,node_server_repo,node_server_db,node_server_chart,node_server_plugins toneMint
class node_web_main,node_web_providers,node_web_api,node_web_hooks,node_web_dashboard,node_web_widgets,node_web_meta toneRose
class node_deploy_route,node_deploy_local,node_deploy_prod toneIndigo
```

## Components

### Workspace

pnpm monorepo and shared tooling

- [pnpm workspace](https://github.com/nhamonin/widget-dashboard/blob/main/pnpm-workspace.yaml): monorepo · Workspace root and shared package management · `pnpm-workspace.yaml`

### Shared Contracts

Schema and type layer used by both apps

- [Widget schema](https://github.com/nhamonin/widget-dashboard/blob/main/packages/shared/src/widget.ts): zod contract · Widget domain schemas and inferred types · `packages/shared/src/widget.ts`
- [HTTP schema](https://github.com/nhamonin/widget-dashboard/blob/main/packages/shared/src/http.ts): zod contract · Request and response payload schemas · `packages/shared/src/http.ts`
- [Shared exports](https://github.com/nhamonin/widget-dashboard/blob/main/packages/shared/src/index.ts): package entry · Public re-exports for the contract layer · `packages/shared/src/index.ts`

### Server

Fastify API and SQLite persistence

- [Fastify app](https://github.com/nhamonin/widget-dashboard/blob/main/apps/server/src/server.ts): http server · Server bootstrap and application composition · `apps/server/src/server.ts`
- [Routes](https://github.com/nhamonin/widget-dashboard/blob/main/apps/server/src/routes/widgets.ts): routing layer · Health and widget route modules · `apps/server/src/routes/widgets.ts`
- [Widget service](https://github.com/nhamonin/widget-dashboard/blob/main/apps/server/src/services/widget.service.ts): business logic · Widget rules, validation flow, and mutation policy · `apps/server/src/services/widget.service.ts`
- [Widget repo](https://github.com/nhamonin/widget-dashboard/blob/main/apps/server/src/repositories/widget.repo.ts): persistence adapter · SQLite queries and record mapping · `apps/server/src/repositories/widget.repo.ts`
- [SQLite DB](https://github.com/nhamonin/widget-dashboard/blob/main/apps/server/src/db/index.ts): database · Persistent SQLite boundary used by the API · `apps/server/src/db/index.ts`
- [Chart data](https://github.com/nhamonin/widget-dashboard/blob/main/apps/server/src/lib/chart-data.ts): data generator · Stable random chart-point generation stored once · `apps/server/src/lib/chart-data.ts`
- [Fastify plugins](https://github.com/nhamonin/widget-dashboard/blob/main/apps/server/src/plugins/openapi.ts): cross-cutting · JSON parsing, error formatting, OpenAPI, and rate limiting · `apps/server/src/plugins/openapi.ts`

### Web

React dashboard SPA

- [App bootstrap](https://github.com/nhamonin/widget-dashboard/blob/main/apps/web/src/main.tsx): spa entry · Client entrypoint and provider wiring · `apps/web/src/main.tsx`
- [App providers](https://github.com/nhamonin/widget-dashboard/blob/main/apps/web/src/providers/AppProviders.tsx): client wiring · Shared client concerns such as query and toast providers · `apps/web/src/providers/AppProviders.tsx`
- [API client](https://github.com/nhamonin/widget-dashboard/blob/main/apps/web/src/lib/api-client.ts): browser client · Central browser access to the backend API · `apps/web/src/lib/api-client.ts`
- [Widgets hook](https://github.com/nhamonin/widget-dashboard/blob/main/apps/web/src/hooks/widgets.ts): query state · TanStack Query fetches and mutations for widgets · `apps/web/src/hooks/widgets.ts`
- [Dashboard](https://github.com/nhamonin/widget-dashboard/blob/main/apps/web/src/components/dashboard/DashboardGrid.tsx): ui composition · Board layout, empty state, widget cards, skeletons, and add control · `apps/web/src/components/dashboard/DashboardGrid.tsx`
- [Widget views](https://github.com/nhamonin/widget-dashboard/blob/main/apps/web/src/components/widgets/ChartWidget.tsx): ui components · Type-specific chart and text widget rendering · `apps/web/src/components/widgets/ChartWidget.tsx`
- [Widget meta](https://github.com/nhamonin/widget-dashboard/blob/main/apps/web/src/lib/widget-meta.ts): view logic · Widget labels and presentation helpers · `apps/web/src/lib/widget-meta.ts`

### Deployment

Local and production routing, containers, and persistent storage

- [Same-origin API](https://github.com/nhamonin/widget-dashboard/tree/main/Caddyfile): routing boundary · Browser traffic goes to /api on the same origin · `Caddyfile`
- [Local proxy](https://github.com/nhamonin/widget-dashboard/blob/main/docker-compose.yml): dev deployment · Caddy and docker compose provide the SPA plus API proxy locally · `docker-compose.yml`
- [Prod deploy](https://github.com/nhamonin/widget-dashboard/blob/main/fly.toml): production deployment · Vercel serves the SPA and Fly.io runs the API with persistent storage · `fly.toml`

## Connections

- pnpm workspace → Shared exports: workspace contains · Monorepo includes the shared contracts package
- pnpm workspace → Fastify app: workspace contains · Monorepo includes the backend app
- pnpm workspace → App bootstrap: workspace contains · Monorepo includes the frontend app
- Shared exports → Widget schema: re-exports · Shared package exposes widget schema and types
- Shared exports → HTTP schema: re-exports · Shared package exposes HTTP payload schemas
- Fastify app → Fastify plugins: uses · App installs cross-cutting Fastify plugins
- Fastify app → Routes: mounts · App composes route modules
- Routes → Widget service: delegates · Route handlers forward widget operations to services
- Widget service → Widget repo: persists via · Business rules use repository methods for storage
- Widget repo → SQLite DB: queries · Repository talks to SQLite database
- Widget service → Chart data: creates · Service uses stable chart data generation
- Fastify app → HTTP schema: validates with · Server uses shared payload schemas
- Widget service → Widget schema: validates with · Service enforces widget contract rules
- App bootstrap → App providers: wraps with · SPA bootstraps shared client providers
- App bootstrap → Dashboard: renders · Root app composes the dashboard experience
- Widgets hook → API client: calls · Query layer centralizes browser requests
- Dashboard → Widgets hook: reads · Dashboard state comes from widget queries and mutations
- Widget views → Widget meta: uses · Widget views rely on presentation metadata
- Dashboard → Widget views: renders · Dashboard shows chart and text widgets
- API client → HTTP schema: shares contract · Browser client follows the same request/response schemas
- Widget views → Widget schema: shares contract · Widget rendering follows shared widget types
- Same-origin API → Local proxy: supports · Local proxy shape matches same-origin API access
- Same-origin API → Prod deploy: supports · Production routing preserves same-origin API access
- Local proxy → Fastify app: proxies to · Caddy forwards API calls to the backend
- Local proxy → App bootstrap: serves · Local environment serves the SPA
- Prod deploy → SQLite DB: persists on · Fly deployment keeps SQLite data on a mounted volume
- Prod deploy → App bootstrap: hosts · Vercel hosts the frontend application
