Skip to main content

System Architecture & Design

Architecture Overview

The STO Education Platform follows a modern full-stack architecture with clear separation of concerns, microservices integration, and scalable design patterns.

High-Level Architecture

┌─────────────────┐    ┌─────────────────┐    ┌─────────────────┐
│   Frontend      │    │   Backend       │    │   External      │
│   (React/Vite)  │◄──►│   (Supabase)    │◄──►│   Services      │
│                 │    │                 │    │                 │
│ • React 18      │    │ • PostgreSQL    │    │ • 100ms Video   │
│ • TypeScript    │    │ • Edge Functions│    │ • SendBird Chat │
│ • Tailwind CSS  │    │ • Auth          │    │ • PayMob Payment│
│ • React Router  │    │ • Storage       │    │ • PostHog Analytics│
└─────────────────┘    └─────────────────┘    └─────────────────┘

Frontend Architecture

Component Hierarchy

App.tsx
├── Router (React Router v6)
├── Context Providers
│   ├── AdminProvider
│   ├── CartProvider
│   ├── LayoutProvider
│   └── PageContentProvider
├── MainNavbar
├── Routes
│   ├── Public Routes
│   ├── Protected Student Routes
│   ├── Protected Teacher Routes
│   ├── Protected Admin Routes
│   └── Protected Trainee Routes
├── AuthModal
└── UpdateNotification

State Management Architecture

┌─────────────────────────────────────────────────────────────┐
│                    React Context API                        │
├─────────────────────────────────────────────────────────────┤
│  AdminContext          │  Role-based access control         │
│  CartContext           │  Shopping cart state management    │
│  LayoutContext         │  UI layout state (sidebar, etc.)   │
│  PageContentContext    │  Dynamic content management        │
└─────────────────────────────────────────────────────────────┘

Routing Architecture

/ (Public)
├── /dashboard/* (Student/Parent Protected)
├── /teacher/* (Teacher Protected)
├── /admin/* (Admin Protected)
├── /training/* (Trainee Protected)
├── /courses/* (Public/Protected)
├── /teachers/* (Public)
├── /exam-papers/* (Public)
└── /blog/* (Public)

Backend Architecture

Supabase Backend Services

┌─────────────────────────────────────────────────────────────┐
│                    Supabase Platform                        │
├─────────────────────────────────────────────────────────────┤
│  PostgreSQL Database    │  Edge Functions (Deno)            │
│  • User Management      │  • Payment Processing             │
│  • Course Data          │  • Video Token Generation         │
│  • Chat Messages        │  • Email Notifications            │
│  • File Storage         │  • Webhook Handlers               │
│  • Real-time Subscriptions│ • Data Processing              │
├─────────────────────────────────────────────────────────────┤
│  Authentication         │  Storage                          │
│  • JWT Tokens           │  • File Upload                    │
│  • Role Management      │  • Image Processing               │
│  • Session Handling     │  • Document Storage               │
└─────────────────────────────────────────────────────────────┘

Database Schema Overview

┌─────────────────────────────────────────────────────────────┐
│                    Core Tables                              │
├─────────────────────────────────────────────────────────────┤
│  profiles           │  User profiles and role management    │
│  courses            │  Course information and metadata      │
│  enrollments        │  Student course enrollments           │
│  live_sessions      │  Video session management             │
│  chat_messages      │  Real-time messaging                  │
│  assignments        │  Assignment management                │
│  quizzes            │  Quiz system and questions            │
│  payments           │  Payment processing and records       │
├─────────────────────────────────────────────────────────────┤
│                    Supporting Tables                        │
├─────────────────────────────────────────────────────────────┤
│  subjects           │  Academic subjects                    │
│  teacher_mark_schemes│  Teacher-created grading schemes     │
│  notifications      │  User notifications                   │
│  page_content       │  Dynamic content management           │
│  blog_posts         │  Content management system            │
└─────────────────────────────────────────────────────────────┘

Integration Architecture

Third-Party Service Integration

┌─────────────────────────────────────────────────────────────┐
│                External Service Layer                       │
├─────────────────────────────────────────────────────────────┤
│  100ms Video        │  Real-time video conferencing         │
│  • Room Management  │  • Token Generation                   │
│  • Session Recording│  • Webhook Events                     │
├─────────────────────────────────────────────────────────────┤
│  SendBird Chat      │  Real-time messaging platform         │
│  • User Channels    │  • File Sharing                       │
│  • Push Notifications│ • Message History                    │
├─────────────────────────────────────────────────────────────┤
│  PayMob Payment     │  Payment processing gateway           │
│  • Payment Intents  │  • Webhook Validation                 │
│  • Multiple Methods │  • Fraud Detection                    │
├─────────────────────────────────────────────────────────────┤
│  PostHog Analytics  │  User behavior tracking               │
│  • Event Tracking   │  • Feature Flags                      │
│  • User Identification│ • Funnel Analysis                   │
└─────────────────────────────────────────────────────────────┘

Data Flow Architecture

┌─────────────────┐    ┌─────────────────┐    ┌─────────────────┐
│   User Action   │    │   Frontend      │    │   Backend       │
│                 │    │   Processing    │    │   Processing    │
├─────────────────┤    ├─────────────────┤    ├─────────────────┤
│ 1. User Input   │───►│ 2. Validation   │───►│ 3. Business     │
│                 │    │                 │    │    Logic        │
│                 │    │ 4. API Call     │    │ 5. Database     │
│                 │    │                 │    │    Operation    │
│                 │    │ 6. State Update │◄───│ 7. Response     │
│                 │    │                 │    │                 │
│ 8. UI Update    │◄───│ 9. Re-render    │    │                 │
└─────────────────┘    └─────────────────┘    └─────────────────┘

Security Architecture

Authentication Flow

┌─────────────────────────────────────────────────────────────┐
│                    Authentication Flow                      │
├─────────────────────────────────────────────────────────────┤
│  1. User Login Request                                      │
│  2. Supabase Auth Validation                               │
│  3. JWT Token Generation                                    │
│  4. Role-based Access Control                               │
│  5. Session Management                                      │
│  6. Token Refresh Mechanism                                 │
└─────────────────────────────────────────────────────────────┘

Data Security Layers

┌─────────────────────────────────────────────────────────────┐
│                    Security Layers                          │
├─────────────────────────────────────────────────────────────┤
│  Transport Layer    │  HTTPS/TLS encryption                │
│  Application Layer  │  Input validation & sanitization     │
│  Database Layer     │  Row Level Security (RLS) policies   │
│  API Layer          │  Authentication & authorization       │
│  File Storage       │  Secure file upload & access control  │
└─────────────────────────────────────────────────────────────┘

Performance Architecture

Frontend Performance

┌─────────────────────────────────────────────────────────────┐
│                Frontend Performance Strategy                │
├─────────────────────────────────────────────────────────────┤
│  Code Splitting    │  Lazy loading of route components      │
│  Bundle Optimization│  Tree shaking and minification        │
│  Image Optimization│  WebP format and lazy loading          │
│  Caching Strategy  │  Service worker and browser caching    │
│  State Management  │  Optimized re-rendering patterns       │
└─────────────────────────────────────────────────────────────┘

Backend Performance

┌─────────────────────────────────────────────────────────────┐
│                Backend Performance Strategy                 │
├─────────────────────────────────────────────────────────────┤
│  Database Indexing │  Optimized query performance           │
│  Connection Pooling│  Efficient database connections        │
│  Edge Functions    │  Serverless compute optimization       │
│  CDN Distribution  │  Global content delivery               │
│  Real-time Optimization│  Efficient WebSocket connections   │
└─────────────────────────────────────────────────────────────┘

Scalability Architecture

Horizontal Scaling Strategy

┌─────────────────────────────────────────────────────────────┐
│                    Scaling Architecture                     │
├─────────────────────────────────────────────────────────────┤
│  Frontend Scaling   │  Vercel edge network distribution     │
│  Backend Scaling    │  Supabase auto-scaling infrastructure │
│  Database Scaling   │  PostgreSQL connection pooling        │
│  Storage Scaling    │  Supabase storage auto-scaling        │
│  CDN Scaling        │  Global content delivery network      │
└─────────────────────────────────────────────────────────────┘

Microservices Integration

┌─────────────────────────────────────────────────────────────┐
│                Microservices Architecture                    │
├─────────────────────────────────────────────────────────────┤
│  Authentication Service │  Supabase Auth                    │
│  Video Service          │  100ms Video Platform             │
│  Communication Service  │  SendBird Messaging               │
│  Payment Service        │  PayMob Payment Gateway           │
│  Analytics Service      │  PostHog Analytics                │
│  File Storage Service   │  Supabase Storage                 │
│  Email Service          │  Supabase Edge Functions          │
└─────────────────────────────────────────────────────────────┘

Real-time Architecture

WebSocket Connections

┌─────────────────────────────────────────────────────────────┐
│                Real-time Communication                      │
├─────────────────────────────────────────────────────────────┤
│  Chat Messages      │  SendBird WebSocket connections       │
│  Video Sessions     │  100ms WebRTC connections             │
│  Database Changes   │  Supabase real-time subscriptions     │
│  Notifications      │  Push notification system             │
│  Live Updates       │  Optimistic UI updates                │
└─────────────────────────────────────────────────────────────┘

Error Handling Architecture

Error Boundary Strategy

┌─────────────────────────────────────────────────────────────┐
│                    Error Handling Strategy                  │
├─────────────────────────────────────────────────────────────┤
│  Frontend Errors    │  React Error Boundaries               │
│  API Errors         │  Centralized error handling           │
│  Database Errors    │  Supabase error handling              │
│  Third-party Errors │  Service-specific error handling      │
│  User Feedback      │  Toast notifications and modals       │
│  Error Tracking     │  LogRocket session replay             │
└─────────────────────────────────────────────────────────────┘

Deployment Architecture

Production Environment

┌─────────────────────────────────────────────────────────────┐
│                    Production Deployment                    │
├─────────────────────────────────────────────────────────────┤
│  Frontend Hosting   │  Vercel edge network                  │
│  Backend Hosting    │  Supabase cloud infrastructure        │
│  Database Hosting   │  Supabase PostgreSQL cluster          │
│  CDN Distribution   │  Global content delivery network      │
│  Monitoring         │  LogRocket and PostHog analytics      │
│  Backup Strategy    │  Automated database backups           │
└─────────────────────────────────────────────────────────────┘

Development Environment

┌─────────────────────────────────────────────────────────────┐
│                  Development Environment                    │
├─────────────────────────────────────────────────────────────┤
│  Local Development  │  Vite dev server                      │
│  Database           │  Supabase local development           │
│  Testing            │  Jest and React Testing Library       │
│  Code Quality       │  ESLint and TypeScript checking       │
│  Version Control    │  Git with feature branch workflow     │
│  CI/CD Pipeline     │  GitHub Actions for automated testing │
└─────────────────────────────────────────────────────────────┘

Monitoring & Observability

Monitoring Stack

┌─────────────────────────────────────────────────────────────┐
│                    Monitoring Architecture                  │
├─────────────────────────────────────────────────────────────┤
│  Application Monitoring│  LogRocket session replay          │
│  User Analytics       │  PostHog behavior tracking          │
│  Performance Monitoring│  Vercel Analytics                  │
│  Error Tracking       │  LogRocket error capture            │
│  Database Monitoring  │  Supabase monitoring dashboard      │
│  Uptime Monitoring    │  External uptime monitoring         │
└─────────────────────────────────────────────────────────────┘

Future Architecture Considerations

Planned Improvements

┌─────────────────────────────────────────────────────────────┐
│                Future Architecture Plans                    │
├─────────────────────────────────────────────────────────────┤
│  Microservices Migration│  Service decomposition            │
│  Advanced Caching       │  Redis caching layer              │
│  Message Queue System   │  Background job processing        │
│  Advanced Analytics     │  Machine learning integration     │
│  Mobile App Integration │  React Native mobile app          │
│  API Gateway            │  Centralized API management       │
└─────────────────────────────────────────────────────────────┘