# sunbird-spark/sunbird-spark-portal architecture

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

- Interactive diagram: https://gitdiagram.com/sunbird-spark/sunbird-spark-portal
- Repository: https://github.com/sunbird-spark/sunbird-spark-portal

## Overview

1. **Overall shape**
   - This is a two-app monorepo: `frontend/` is a React 19 SPA, and `backend/` is an Express 5 API server.
   - In production, the backend also serves the built frontend bundle; in development, Vite runs the UI and proxies API/content routes to the backend.

2. **Primary runtime boundary**
   - The frontend is a client-side application with route-level pages, shared components, hooks, and client services.
   - The backend is the system boundary for authentication, session management, upstream proxying, and persistence access.

3. **Frontend presentation layer**
   - UI is organized by route domains such as home, collection, workspace, reports, profile, signup, onboarding, and user management.
   - Shared building blocks live under `components/`, while page orchestration lives under `pages/`.
   - The app uses React Router, TanStack Query for server state, Tailwind for styling, and i18next for localization.

4. **Frontend data access and business logic**
   - API communication is centralized in `src/api/`, `src/services/`, and `src/lib/http-client/`.
   - Hooks under `src/hooks/` encapsulate feature-specific state and data loading, while `src/rbac/` gates routes and components by role.
   - This separation keeps pages thin: pages compose hooks/services and render feature components.

5. **Content and player subsystem**
   - A major frontend subsystem is content rendering and authoring: PDF, video, ePub, and QUML players, plus collection/content editors.
   - These depend on Sunbird web components and require static assets copied into `public/assets/` during install.
   - The player/editor layers sit behind page components and feature services, not directly in the routing layer.

6. **Authentication and session model**
   - Browser users authenticate through the backend using OIDC/Keycloak and Google OAuth.
   - The frontend stores auth state in `AuthContext` and uses `ProtectedRoute`, `PermissionGate`, and `OnboardingGuard` to enforce access.
   - Backend sessions are used for browser flows; mobile flows are token-based and bypass server sessions.

7. **Backend request pipeline**
   - Express routes are split by domain under `src/routes/`, then mapped to controllers, which call services.
   - Cross-cutting middleware handles auth, conditional session handling, validation, and request shaping before handlers run.
   - This creates a clear chain: route → middleware → controller → service → database/proxy/external auth provider.

8. **Backend persistence and state**
   - Persistent storage is split by concern: YugabyteDB/pg for general relational data and session storage, plus Cassandra-compatible access for forms and review comments.
   - Session storage can be in-memory or Yugabyte-backed depending on configuration.
   - The backend also maintains typed environment configuration in `src/config/env.ts`, making deployment settings part of the architecture boundary.

9. **Backend integrations and upstream proxying**
   - Several backend routes act as reverse proxies to upstream Sunbird services via `http-proxy-middleware`.
   - Proxies exist for Kong, Knowlg middleware, user-related services, and other content/plugin paths.
   - This means the backend is not just an API host; it is also an aggregation and routing layer for external platform services.

10. **Auth special cases for mobile**
   - Mobile support is a distinct backend subsystem under `/mobile`: native Keycloak login, Google sign-in, and token refresh.
   - These endpoints return tokens directly and do not rely on browser session cookies.
   - The frontend also supports mobile WebView flows for signup and password reset, passing language and redirect context through query/session state.

11. **Telemetry, reporting, and observability**
   - The frontend includes telemetry providers/components and content/player telemetry context builders.
   - Reporting pages and services are a separate slice, with charts, tables, and export helpers on the UI side.
   - On the backend, telemetry, user, tenant, organization, and forms services provide the data and integrations those workflows depend on.

12. **Internationalization and locale flow**
   - i18n is a first-class cross-cutting concern in the frontend, with locale files for English, French, Portuguese, and Arabic.
   - Arabic adds RTL handling via a direction provider and CSS overrides.
   - Language choice is persisted in browser storage and also coordinated with Keycloak/mobile flows so login and recovery pages match the selected locale.

13. **Build, packaging, and deployment**
   - The repo uses separate frontend and backend toolchains, both TypeScript-based and both tested with Vitest.
   - A multi-stage Dockerfile builds the frontend, compiles the backend, and produces a production image that runs the server and serves static assets.
   - GitHub Actions handle PR validation and image publishing, so the architecture is intended for repeatable cloud deployment.

14. **What to show in a diagram**
   - Diagram the browser SPA as the main user entry point, with the backend as the central orchestration/API node.
   - Show backend fan-out to Keycloak/Google, YugabyteDB/Cassandra, and upstream Sunbird services via proxies.
   - Show the frontend split into routing, feature components, hooks/services, auth/RBAC, and content-player/editor subsystems, with shared static assets supporting the players/editors.

Main entry points: [`frontend/src/pages`](https://github.com/sunbird-spark/sunbird-spark-portal/tree/main/frontend/src/pages) (Pages), [`backend/src/app.ts`](https://github.com/sunbird-spark/sunbird-spark-portal/blob/main/backend/src/app.ts) (Express API), [`backend/src/middlewares`](https://github.com/sunbird-spark/sunbird-spark-portal/tree/main/backend/src/middlewares) (Middleware & Controllers), [`frontend/src/components/players`](https://github.com/sunbird-spark/sunbird-spark-portal/tree/main/frontend/src/components/players) (Content System)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend SPA"]
  node_browser(("Browser App<br/>entry point<br/>[main.tsx]"))
  node_routing["Routes<br/>router<br/>[AppRoutes.tsx]"]
  node_pages["Pages<br/>screens"]
  node_shell["Shared UI<br/>components"]
  node_authui["Auth & RBAC<br/>guard layer"]
  node_data["API Layer<br/>client services"]
  node_hooks["Feature Hooks<br/>state hooks"]
  node_contentsys["Content System<br/>players and editors"]
  node_telemetryui["Telemetry UI<br/>observability"]
  node_i18n["Locale Layer<br/>i18n<br/>[i18n.ts]"]
end

subgraph group_backend["Backend API"]
  node_server["Express API<br/>api server<br/>[app.ts]"]
  node_routes["Domain Routes<br/>routing"]
  node_pipeline["Middleware & Controllers<br/>request pipeline"]
  node_services["Backend Services<br/>business services"]
  node_persistence[("Persistence<br/>datastores")]
  node_proxy["Upstream Proxy<br/>reverse proxy"]
  node_env["Env Config<br/>config boundary<br/>[env.ts]"]
end

subgraph group_external["External Systems"]
  node_keycloak(("Keycloak<br/>identity provider"))
  node_google(("Google OAuth<br/>identity provider"))
  node_yugabyte[("YugabyteDB<br/>relational store")]
  node_cassandra[("Cassandra API<br/>wide-column store")]
  node_sunbird["Sunbird Services<br/>upstream platform"]
end

node_browser -->|"loads"| node_routing
node_routing -->|"renders"| node_pages
node_pages -->|"uses"| node_shell
node_pages -->|"orchestrates"| node_hooks
node_hooks -->|"calls"| node_data
node_pages -->|"guards"| node_authui
node_pages -->|"embeds"| node_contentsys
node_contentsys -->|"composes"| node_shell
node_contentsys -->|"reports"| node_telemetryui
node_pages -->|"localizes"| node_i18n
node_browser -->|"api calls"| node_server
node_server -->|"dispatches"| node_routes
node_routes -->|"passes through"| node_pipeline
node_pipeline -->|"invokes"| node_services
node_services -->|"reads/writes"| node_persistence
node_server -->|"forwards"| node_proxy
node_server -->|"configures"| node_env
node_pipeline -->|"authenticates"| node_keycloak
node_pipeline -->|"oauth"| node_google
node_persistence -->|"relational"| node_yugabyte
node_persistence -->|"wide-column"| node_cassandra
node_proxy -->|"routes to"| node_sunbird
node_data -->|"requests"| node_server

click node_browser "https://github.com/sunbird-spark/sunbird-spark-portal/blob/main/frontend/src/main.tsx"
click node_routing "https://github.com/sunbird-spark/sunbird-spark-portal/blob/main/frontend/src/AppRoutes.tsx"
click node_pages "https://github.com/sunbird-spark/sunbird-spark-portal/tree/main/frontend/src/pages"
click node_shell "https://github.com/sunbird-spark/sunbird-spark-portal/tree/main/frontend/src/components"
click node_authui "https://github.com/sunbird-spark/sunbird-spark-portal/tree/main/frontend/src/auth"
click node_data "https://github.com/sunbird-spark/sunbird-spark-portal/tree/main/frontend/src/api"
click node_hooks "https://github.com/sunbird-spark/sunbird-spark-portal/tree/main/frontend/src/hooks"
click node_contentsys "https://github.com/sunbird-spark/sunbird-spark-portal/tree/main/frontend/src/components/players"
click node_telemetryui "https://github.com/sunbird-spark/sunbird-spark-portal/tree/main/frontend/src/components/telemetry"
click node_i18n "https://github.com/sunbird-spark/sunbird-spark-portal/blob/main/frontend/src/configs/i18n.ts"
click node_server "https://github.com/sunbird-spark/sunbird-spark-portal/blob/main/backend/src/app.ts"
click node_routes "https://github.com/sunbird-spark/sunbird-spark-portal/tree/main/backend/src/routes"
click node_pipeline "https://github.com/sunbird-spark/sunbird-spark-portal/tree/main/backend/src/middlewares"
click node_services "https://github.com/sunbird-spark/sunbird-spark-portal/tree/main/backend/src/services"
click node_persistence "https://github.com/sunbird-spark/sunbird-spark-portal/tree/main/backend/src/databases"
click node_proxy "https://github.com/sunbird-spark/sunbird-spark-portal/tree/main/backend/src/proxies"
click node_env "https://github.com/sunbird-spark/sunbird-spark-portal/blob/main/backend/src/config/env.ts"

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_browser,node_routing,node_pages,node_shell,node_authui,node_data,node_hooks,node_contentsys,node_telemetryui,node_i18n toneBlue
class node_server,node_routes,node_pipeline,node_services,node_persistence,node_proxy,node_env toneAmber
class node_keycloak,node_google,node_yugabyte,node_cassandra,node_sunbird toneMint
```

## Components

### Frontend SPA

React client app with routes, feature UI, state, and player/editor subsystems.

- [Browser App](https://github.com/sunbird-spark/sunbird-spark-portal/blob/main/frontend/src/main.tsx): entry point · Primary user entry into the portal SPA. · `frontend/src/main.tsx`
- [Routes](https://github.com/sunbird-spark/sunbird-spark-portal/blob/main/frontend/src/AppRoutes.tsx): router · Route map and page-level composition for the SPA. · `frontend/src/AppRoutes.tsx`
- [Pages](https://github.com/sunbird-spark/sunbird-spark-portal/tree/main/frontend/src/pages): screens · Domain pages for home, collection, workspace, reports, profile, signup, onboarding, and admin flows. · `frontend/src/pages`
- [Shared UI](https://github.com/sunbird-spark/sunbird-spark-portal/tree/main/frontend/src/components): components · Reusable presentation components and layout primitives. · `frontend/src/components`
- [Auth & RBAC](https://github.com/sunbird-spark/sunbird-spark-portal/tree/main/frontend/src/auth): guard layer · Client auth state plus route/component guards for protection and onboarding. · `frontend/src/auth`
- [API Layer](https://github.com/sunbird-spark/sunbird-spark-portal/tree/main/frontend/src/api): client services · Central HTTP client, API modules, and feature services for server communication. · `frontend/src/api`
- [Feature Hooks](https://github.com/sunbird-spark/sunbird-spark-portal/tree/main/frontend/src/hooks): state hooks · Feature-specific data loading and state orchestration. · `frontend/src/hooks`
- [Content System](https://github.com/sunbird-spark/sunbird-spark-portal/tree/main/frontend/src/components/players): players and editors · PDF, video, ePub, QUML players and collection/content editors. · `frontend/src/components/players`
- [Telemetry UI](https://github.com/sunbird-spark/sunbird-spark-portal/tree/main/frontend/src/components/telemetry): observability · Telemetry providers and context builders used by content and analytics workflows. · `frontend/src/components/telemetry`
- [Locale Layer](https://github.com/sunbird-spark/sunbird-spark-portal/blob/main/frontend/src/configs/i18n.ts): i18n · Language loading, direction handling, and locale persistence. · `frontend/src/configs/i18n.ts`

### Backend API

Express server for auth, routing, persistence, and upstream proxying.

- [Express API](https://github.com/sunbird-spark/sunbird-spark-portal/blob/main/backend/src/app.ts): api server · Backend application boundary for browser session flows, token flows, and orchestration. · `backend/src/app.ts`
- [Domain Routes](https://github.com/sunbird-spark/sunbird-spark-portal/tree/main/backend/src/routes): routing · API route modules grouped by domain and flow. · `backend/src/routes`
- [Middleware & Controllers](https://github.com/sunbird-spark/sunbird-spark-portal/tree/main/backend/src/middlewares): request pipeline · Auth, session, validation, and controller handling before services run. · `backend/src/middlewares`
- [Backend Services](https://github.com/sunbird-spark/sunbird-spark-portal/tree/main/backend/src/services): business services · Core services for users, tenants, telemetry, forms, reviews, mobile auth, and Google/Kong auth. · `backend/src/services`
- [Persistence](https://github.com/sunbird-spark/sunbird-spark-portal/tree/main/backend/src/databases): datastores · Yugabyte/pg session and relational storage plus Cassandra-compatible form/review data. · `backend/src/databases`
- [Upstream Proxy](https://github.com/sunbird-spark/sunbird-spark-portal/tree/main/backend/src/proxies): reverse proxy · Proxy layer for Kong, Knowlg middleware, user services, and content/plugin paths. · `backend/src/proxies`
- [Env Config](https://github.com/sunbird-spark/sunbird-spark-portal/blob/main/backend/src/config/env.ts): config boundary · Typed deployment and runtime configuration for the backend. · `backend/src/config/env.ts`

### External Systems

Identity providers, databases, and Sunbird upstream services.

- **Keycloak**: identity provider · OIDC auth provider for browser and mobile login flows.
- **Google OAuth**: identity provider · Google sign-in provider used by browser and mobile authentication.
- **YugabyteDB**: relational store · Primary relational and session-backed storage.
- **Cassandra API**: wide-column store · Cassandra-compatible persistence used for forms and review comments.
- **Sunbird Services**: upstream platform · External platform services reached through backend proxies.

## Connections

- Browser App → Routes: loads · Bootstraps the SPA and resolves routes.
- Routes → Pages: renders · Routes compose page-level screens.
- Pages → Shared UI: uses · Pages compose shared UI and layout primitives.
- Pages → Feature Hooks: orchestrates · Pages rely on hooks for feature state and data loading.
- Feature Hooks → API Layer: calls · Hooks delegate network work to the central API layer.
- Pages → Auth & RBAC: guards · Protected and onboarding flows enforce access control.
- Pages → Content System: embeds · Content, workspace, and collection pages embed players and editors.
- Content System → Shared UI: composes · Players and editors depend on shared UI components.
- Content System → Telemetry UI: reports · Player/editor interactions feed telemetry context.
- Pages → Locale Layer: localizes · Pages and components render through locale and direction state.
- Browser App → Express API: api calls · Client requests target the backend for auth and data.
- Express API → Domain Routes: dispatches · Express requests enter domain route modules.
- Domain Routes → Middleware & Controllers: passes through · Routes are protected and shaped by middleware and controllers.
- Middleware & Controllers → Backend Services: invokes · Controllers call domain services after middleware processing.
- Backend Services → Persistence: reads/writes · Services persist and query application state.
- Express API → Upstream Proxy: forwards · Selected routes are reverse-proxied to upstream platform services.
- Express API → Env Config: configures · Typed environment settings define runtime behavior.
- Middleware & Controllers → Keycloak: authenticates · OIDC and session-based browser auth integrates with Keycloak.
- Middleware & Controllers → Google OAuth: oauth · Google sign-in and related flows use Google OAuth.
- Persistence → YugabyteDB: relational · General data and session storage use Yugabyte-backed Postgres.
- Persistence → Cassandra API: wide-column · Forms and review comments use Cassandra-compatible access.
- Upstream Proxy → Sunbird Services: routes to · Backend proxies fan out to Sunbird platform services.
- API Layer → Express API: requests · Frontend service modules communicate with backend endpoints.
