How kshitijomkar/ledger works
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: handler.go HTTP router · data_repository.dart Provider & repository · apiWithOffline.ts API data boundary · main.go Go entry point
Components 21
Go Backend
Authoritative authenticated ledger API.
- Go entry point
apps/backend/cmd/main.go - Config & database · Loads configuration and initializes database access.
apps/backend/internal/database/db.go - HTTP router · Maps API endpoints to domain handlers.
apps/backend/internal/handlers/handler.go - JWT auth middleware
apps/backend/internal/middleware/middleware.go - Ledger domain services · Auth, parties, transactions, reminders, reports, and sync operations.
apps/backend/internal/services/auth_service.go - Ledger models · Party, transaction, and reminder records.
apps/backend/internal/models/transaction.go
Web PWA
Next.js client with offline-first synchronization.
- App routes & screens · Auth and authenticated dashboard workflows.
apps/web/src/app/page.tsx - App & offline state
apps/web/src/app/providers.tsx - API data boundary · Uses remote and offline-aware request paths.
apps/web/src/lib/apiWithOffline.ts - Offline local store
apps/web/src/lib/db.ts - Sync & conflicts · Reconciles queued local mutations with the API.
apps/web/src/lib/syncService.ts - Service worker
apps/web/public/sw.js
Flutter Mobile
Flutter client with local and remote data access.
- Flutter app
apps/mobile/lib/main.dart - Mobile screens · Auth, dashboard, ledger, reminders, reports, and settings.
apps/mobile/lib/screens/dashboard_screen.dart - Provider & repository · Coordinates screen data from local and remote services.
apps/mobile/lib/services/data_repository.dart - Remote API service
apps/mobile/lib/services/api_service.dart - Mobile local database
apps/mobile/lib/database/database.dart - Notifications
apps/mobile/lib/services/notifications/notification_service.dart
Delivery
- Backend container
apps/backend/Dockerfile - Web deployment
apps/web/vercel.json - Android APK build
.github/workflows/android_build.yml
Connections 23
- 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