# kshitijomkar/ledger architecture

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

- Interactive diagram: https://gitdiagram.com/kshitijomkar/ledger
- Repository: https://github.com/kshitijomkar/ledger

## Overview

#### 1. Purpose and Monorepo Shape
Khatabook Pro is a small-business digital ledger: users manage parties/customers/suppliers, transactions, reminders, and reports. It has three independently deployable clients/services: Go REST backend, Next.js PWA/web client, and Flutter mobile client. Root purpose and topology are defined in `README.md`, with app boundaries under `apps/backend`, `apps/web`, and `apps/mobile`.

#### 2. Backend API and Composition Root
The backend is a Go service using Gin. Its executable entry point is `apps/backend/cmd/main.go`; configuration and database initialization are separated into `apps/backend/internal/config/config.go` and `apps/backend/internal/database/db.go`. HTTP request routing is centralized in `apps/backend/internal/handlers/handler.go`, which connects endpoint handlers to domain services.

The backend is the authoritative remote API boundary for authenticated ledger data and synchronization.

#### 3. Backend Domain and Request Flow
Core backend domains are users/authentication, parties, transactions, reminders, reporting, and synchronization:
- Auth: `apps/backend/internal/handlers/auth_handler.go`, `apps/backend/internal/services/auth_service.go`
- Ledger entities: `apps/backend/internal/models/party.go`, `apps/backend/internal/models/transaction.go`, `apps/backend/internal/models/reminder.go`
- Sync/report endpoints: `apps/backend/internal/handlers/sync_handler.go`, `apps/backend/internal/handlers/report_handler.go`

Control flow is HTTP request → middleware → handler → service → database/model persistence → JSON response. Cross-cutting authentication/error behavior belongs at `apps/backend/internal/middleware/middleware.go` and `apps/backend/pkg/errors/errors.go`. JWT auth is the security boundary between clients and protected ledger operations.

#### 4. Web PWA: UI, State, and Offline Sync
The web application is a Next.js 14 PWA, rooted at `apps/web/src/app/layout.tsx` and `apps/web/src/app/page.tsx`. Route groups divide authentication from authenticated dashboard workflows:
- Auth routes: `apps/web/src/app/(auth)/login/page.tsx`, `apps/web/src/app/(auth)/register/page.tsx`
- Ledger dashboard routes: `apps/web/src/app/(dashboard)/layout.tsx`, `apps/web/src/app/(dashboard)/transactions/page.tsx`, `apps/web/src/app/(dashboard)/customers/page.tsx`

Global application and offline state are provided through `apps/web/src/app/providers.tsx`, `apps/web/src/contexts/AppContext.tsx`, and `apps/web/src/contexts/OfflineContext.tsx`.

#### 5. Web Data Boundary and Conflict Handling
Web screens call the remote backend through `apps/web/src/lib/api.ts`; offline-aware operations use `apps/web/src/lib/apiWithOffline.ts`. Local persistence is abstracted behind `apps/web/src/lib/db.ts` (and Android-specific `apps/web/src/lib/dbAndroid.ts`). Sync orchestration and reconciliation are explicit in `apps/web/src/lib/syncService.ts`, `apps/web/src/hooks/useOfflineSync.ts`, and `apps/web/src/lib/conflictResolver.ts`.

Thus, user mutations may first commit locally, then synchronize in the background with the backend and resolve conflicts. Service worker/offline assets are in `apps/web/public/sw.js` and `apps/web/public/offline.html`. The web client can also be packaged through Capacitor, configured by `apps/web/capacitor.config.json`.

#### 6. Flutter Mobile Client
The Flutter application starts at `apps/mobile/lib/main.dart`. Its presentation layer is organized around auth, dashboard, parties, transactions, reminders, reports, and settings screens, including `apps/mobile/lib/screens/auth_screen.dart`, `apps/mobile/lib/screens/dashboard_screen.dart`, and `apps/mobile/lib/screens/transactions_screen.dart`.

Mobile state/data flow is screen → provider/repository → local or remote service. Relevant boundaries are `apps/mobile/lib/providers/app_provider.dart`, `apps/mobile/lib/services/data_repository.dart`, `apps/mobile/lib/services/api_service.dart`, and `apps/mobile/lib/services/offline_data_service.dart`. SQLite-style local persistence is represented by `apps/mobile/lib/database/database.dart`; mobile notifications are isolated in `apps/mobile/lib/services/notifications/notification_service.dart`.

#### 7. Runtime and Delivery Infrastructure
Backend runs as a containerized Go service via `apps/backend/Dockerfile` and is intended for Render. Web is deployed to Vercel, with deployment configuration in `apps/web/vercel.json`. The PWA runtime is Next.js/Node.js; mobile runs on Flutter/Dart and targets Android through `apps/mobile/android`.

Android APK delivery is automated by `.github/workflows/android_build.yml` and the mobile-specific `apps/mobile/.github/workflows/build_apk.yml`. Environment-dependent backend URLs and secrets are intentionally externalized through `apps/backend/.env.example`, `apps/web/.env.example`, and `apps/mobile/.env.example`.

Main entry points: [`apps/backend/internal/handlers/handler.go`](https://github.com/kshitijomkar/ledger/blob/main/apps/backend/internal/handlers/handler.go) (HTTP router), [`apps/mobile/lib/services/data_repository.dart`](https://github.com/kshitijomkar/ledger/blob/main/apps/mobile/lib/services/data_repository.dart) (Provider & repository), [`apps/web/src/lib/apiWithOffline.ts`](https://github.com/kshitijomkar/ledger/blob/main/apps/web/src/lib/apiWithOffline.ts) (API data boundary), [`apps/backend/cmd/main.go`](https://github.com/kshitijomkar/ledger/blob/main/apps/backend/cmd/main.go) (Go entry point)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_backend["Go Backend"]
  node_backend_main["Go entry point<br/>Gin bootstrap<br/>[main.go]"]
  node_backend_config_db[("Config &amp; database<br/>Go infrastructure<br/>[db.go]")]
  node_backend_router["HTTP router<br/>Gin handlers<br/>[handler.go]"]
  node_backend_auth{{"JWT auth middleware<br/>security boundary<br/>[middleware.go]"}}
  node_backend_domains["Ledger domain services<br/>Go services<br/>[auth_service.go]"]
  node_backend_models["Ledger models<br/>persistence models<br/>[transaction.go]"]
end

subgraph group_web["Web PWA"]
  node_web_routes["App routes &amp; screens<br/>Next.js App Router<br/>[page.tsx]"]
  node_web_providers["App &amp; offline state<br/>React contexts<br/>[providers.tsx]"]
  node_web_api["API data boundary<br/>web API client<br/>[apiWithOffline.ts]"]
  node_web_local[("Offline local store<br/>browser persistence<br/>[db.ts]")]
  node_web_sync["Sync &amp; conflicts<br/>sync orchestration<br/>[syncService.ts]"]
  node_web_pwa["Service worker<br/>PWA runtime<br/>[sw.js]"]
end

subgraph group_mobile["Flutter Mobile"]
  node_mobile_main["Flutter app<br/>mobile entry point<br/>[main.dart]"]
  node_mobile_screens["Mobile screens<br/>presentation layer"]
  node_mobile_data["Provider &amp; repository<br/>mobile state/data"]
  node_mobile_api["Remote API service<br/>mobile API client<br/>[api_service.dart]"]
  node_mobile_local[("Mobile local database<br/>SQLite persistence<br/>[database.dart]")]
  node_mobile_notifications["Notifications<br/>mobile notification service"]
end

subgraph group_delivery["Delivery"]
  node_backend_container{{"Backend container<br/>Render deployment"}}
  node_web_deployment{{"Web deployment<br/>Vercel deployment<br/>[vercel.json]"}}
  node_apk_build{{"Android APK build<br/>GitHub Actions<br/>[android_build.yml]"}}
end

node_backend_main -->|"initializes"| node_backend_config_db
node_backend_main -->|"starts"| node_backend_router
node_backend_router -->|"protects requests"| node_backend_auth
node_backend_auth -->|"authorizes"| node_backend_domains
node_backend_router -->|"dispatches"| node_backend_domains
node_backend_domains -->|"persists"| node_backend_models
node_backend_models -->|"uses"| node_backend_config_db
node_web_routes -->|"uses state"| node_web_providers
node_web_providers -->|"issues mutations"| node_web_api
node_web_api -->|"commits offline"| node_web_local
node_web_local -->|"queues changes"| node_web_sync
node_web_sync -->|"replays and reconciles"| node_web_api
node_web_api -->|"HTTPS JSON + JWT"| node_backend_router
node_web_pwa -.->|"caches offline runtime"| node_web_routes
node_mobile_main -->|"renders"| node_mobile_screens
node_mobile_screens -->|"reads and mutates"| node_mobile_data
node_mobile_data -->|"stores offline data"| node_mobile_local
node_mobile_data -->|"synchronizes"| node_mobile_api
node_mobile_api -->|"HTTPS JSON + JWT"| node_backend_router
node_mobile_data -->|"schedules reminders"| node_mobile_notifications
node_backend_container -.->|"deploys"| node_backend_main
node_web_deployment -.->|"deploys"| node_web_routes
node_apk_build -.->|"packages Android APK"| node_mobile_main

click node_backend_main "https://github.com/kshitijomkar/ledger/blob/main/apps/backend/cmd/main.go"
click node_backend_config_db "https://github.com/kshitijomkar/ledger/blob/main/apps/backend/internal/database/db.go"
click node_backend_router "https://github.com/kshitijomkar/ledger/blob/main/apps/backend/internal/handlers/handler.go"
click node_backend_auth "https://github.com/kshitijomkar/ledger/blob/main/apps/backend/internal/middleware/middleware.go"
click node_backend_domains "https://github.com/kshitijomkar/ledger/blob/main/apps/backend/internal/services/auth_service.go"
click node_backend_models "https://github.com/kshitijomkar/ledger/blob/main/apps/backend/internal/models/transaction.go"
click node_web_routes "https://github.com/kshitijomkar/ledger/blob/main/apps/web/src/app/page.tsx"
click node_web_providers "https://github.com/kshitijomkar/ledger/blob/main/apps/web/src/app/providers.tsx"
click node_web_api "https://github.com/kshitijomkar/ledger/blob/main/apps/web/src/lib/apiWithOffline.ts"
click node_web_local "https://github.com/kshitijomkar/ledger/blob/main/apps/web/src/lib/db.ts"
click node_web_sync "https://github.com/kshitijomkar/ledger/blob/main/apps/web/src/lib/syncService.ts"
click node_web_pwa "https://github.com/kshitijomkar/ledger/blob/main/apps/web/public/sw.js"
click node_mobile_main "https://github.com/kshitijomkar/ledger/blob/main/apps/mobile/lib/main.dart"
click node_mobile_screens "https://github.com/kshitijomkar/ledger/blob/main/apps/mobile/lib/screens/dashboard_screen.dart"
click node_mobile_data "https://github.com/kshitijomkar/ledger/blob/main/apps/mobile/lib/services/data_repository.dart"
click node_mobile_api "https://github.com/kshitijomkar/ledger/blob/main/apps/mobile/lib/services/api_service.dart"
click node_mobile_local "https://github.com/kshitijomkar/ledger/blob/main/apps/mobile/lib/database/database.dart"
click node_mobile_notifications "https://github.com/kshitijomkar/ledger/blob/main/apps/mobile/lib/services/notifications/notification_service.dart"
click node_backend_container "https://github.com/kshitijomkar/ledger/tree/main/apps/backend/Dockerfile"
click node_web_deployment "https://github.com/kshitijomkar/ledger/blob/main/apps/web/vercel.json"
click node_apk_build "https://github.com/kshitijomkar/ledger/blob/main/.github/workflows/android_build.yml"

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_backend_main,node_backend_config_db,node_backend_router,node_backend_auth,node_backend_domains,node_backend_models toneBlue
class node_web_routes,node_web_providers,node_web_api,node_web_local,node_web_sync,node_web_pwa toneAmber
class node_mobile_main,node_mobile_screens,node_mobile_data,node_mobile_api,node_mobile_local,node_mobile_notifications toneMint
class node_backend_container,node_web_deployment,node_apk_build toneRose
```

## Components

### Go Backend

Authoritative authenticated ledger API.

- [Go entry point](https://github.com/kshitijomkar/ledger/blob/main/apps/backend/cmd/main.go): Gin bootstrap · `apps/backend/cmd/main.go`
- [Config & database](https://github.com/kshitijomkar/ledger/blob/main/apps/backend/internal/database/db.go): Go infrastructure · Loads configuration and initializes database access. · `apps/backend/internal/database/db.go`
- [HTTP router](https://github.com/kshitijomkar/ledger/blob/main/apps/backend/internal/handlers/handler.go): Gin handlers · Maps API endpoints to domain handlers. · `apps/backend/internal/handlers/handler.go`
- [JWT auth middleware](https://github.com/kshitijomkar/ledger/blob/main/apps/backend/internal/middleware/middleware.go): security boundary · `apps/backend/internal/middleware/middleware.go`
- [Ledger domain services](https://github.com/kshitijomkar/ledger/blob/main/apps/backend/internal/services/auth_service.go): Go services · Auth, parties, transactions, reminders, reports, and sync operations. · `apps/backend/internal/services/auth_service.go`
- [Ledger models](https://github.com/kshitijomkar/ledger/blob/main/apps/backend/internal/models/transaction.go): persistence models · Party, transaction, and reminder records. · `apps/backend/internal/models/transaction.go`

### Web PWA

Next.js client with offline-first synchronization.

- [App routes & screens](https://github.com/kshitijomkar/ledger/blob/main/apps/web/src/app/page.tsx): Next.js App Router · Auth and authenticated dashboard workflows. · `apps/web/src/app/page.tsx`
- [App & offline state](https://github.com/kshitijomkar/ledger/blob/main/apps/web/src/app/providers.tsx): React contexts · `apps/web/src/app/providers.tsx`
- [API data boundary](https://github.com/kshitijomkar/ledger/blob/main/apps/web/src/lib/apiWithOffline.ts): web API client · Uses remote and offline-aware request paths. · `apps/web/src/lib/apiWithOffline.ts`
- [Offline local store](https://github.com/kshitijomkar/ledger/blob/main/apps/web/src/lib/db.ts): browser persistence · `apps/web/src/lib/db.ts`
- [Sync & conflicts](https://github.com/kshitijomkar/ledger/blob/main/apps/web/src/lib/syncService.ts): sync orchestration · Reconciles queued local mutations with the API. · `apps/web/src/lib/syncService.ts`
- [Service worker](https://github.com/kshitijomkar/ledger/blob/main/apps/web/public/sw.js): PWA runtime · `apps/web/public/sw.js`

### Flutter Mobile

Flutter client with local and remote data access.

- [Flutter app](https://github.com/kshitijomkar/ledger/blob/main/apps/mobile/lib/main.dart): mobile entry point · `apps/mobile/lib/main.dart`
- [Mobile screens](https://github.com/kshitijomkar/ledger/blob/main/apps/mobile/lib/screens/dashboard_screen.dart): presentation layer · Auth, dashboard, ledger, reminders, reports, and settings. · `apps/mobile/lib/screens/dashboard_screen.dart`
- [Provider & repository](https://github.com/kshitijomkar/ledger/blob/main/apps/mobile/lib/services/data_repository.dart): mobile state/data · Coordinates screen data from local and remote services. · `apps/mobile/lib/services/data_repository.dart`
- [Remote API service](https://github.com/kshitijomkar/ledger/blob/main/apps/mobile/lib/services/api_service.dart): mobile API client · `apps/mobile/lib/services/api_service.dart`
- [Mobile local database](https://github.com/kshitijomkar/ledger/blob/main/apps/mobile/lib/database/database.dart): SQLite persistence · `apps/mobile/lib/database/database.dart`
- [Notifications](https://github.com/kshitijomkar/ledger/blob/main/apps/mobile/lib/services/notifications/notification_service.dart): mobile notification service · `apps/mobile/lib/services/notifications/notification_service.dart`

### Delivery

- [Backend container](https://github.com/kshitijomkar/ledger/tree/main/apps/backend/Dockerfile): Render deployment · `apps/backend/Dockerfile`
- [Web deployment](https://github.com/kshitijomkar/ledger/blob/main/apps/web/vercel.json): Vercel deployment · `apps/web/vercel.json`
- [Android APK build](https://github.com/kshitijomkar/ledger/blob/main/.github/workflows/android_build.yml): GitHub Actions · `.github/workflows/android_build.yml`

## Connections

- Go entry point → Config & database: initializes
- Go entry point → HTTP router: starts
- HTTP router → JWT auth middleware: protects requests
- JWT auth middleware → Ledger domain services: authorizes
- HTTP router → Ledger domain services: dispatches
- Ledger domain services → Ledger models: persists
- Ledger models → Config & database: uses
- App routes & screens → App & offline state: uses state
- App & offline state → API data boundary: issues mutations
- API data boundary → Offline local store: commits offline
- Offline local store → Sync & conflicts: queues changes
- Sync & conflicts → API data boundary: replays and reconciles
- API data boundary → HTTP router: HTTPS JSON + JWT
- Service worker → App routes & screens: caches offline runtime
- Flutter app → Mobile screens: renders
- Mobile screens → Provider & repository: reads and mutates
- Provider & repository → Mobile local database: stores offline data
- Provider & repository → Remote API service: synchronizes
- Remote API service → HTTP router: HTTPS JSON + JWT
- Provider & repository → Notifications: schedules reminders
- Backend container → Go entry point: deploys
- Web deployment → App routes & screens: deploys
- Android APK build → Flutter app: packages Android APK
