# shahzan01/excali-sketch architecture

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

- Interactive diagram: https://gitdiagram.com/shahzan01/excali-sketch
- Repository: https://github.com/shahzan01/excali-sketch

## Overview

##### Main Components Identification
- Backend Application – An Express server featuring REST APIs (via controllers, routes, and services directories) and WebSocket handling (e.g., wsHandler.ts) for real-time collaboration. It includes middleware for authentication and input validation.
- Database – Managed by Prisma via schema definitions and migrations. This component will store user info, room data, and session information.
- Shared Packages – There are several packages (e.g., packages/ui, packages/typescript-config, packages/tailwind-config, and eslint-config) that provide configuration, reusable UI components, and shared utilities that support both frontend and backend components.
- Deployment & Infrastructure – The README notes that the frontend is deployed on Vercel and the backend on Render. This is an important aspect for the overall architecture.

##### Relationships and Interactions
- The Frontend communicates with the Backend using HTTP APIs for room management, authentication, and configuration (the ‘authRoutes’, ‘roomRoutes’, and ‘userRoutes’ under apps/backend).
- Real-time drawing and collaboration are achieved via WebSocket connections between the frontend (using hooks like useSocket.ts) and the backend’s WebSocket handler.
- The Backend interacts with the Database through Prisma, executing migrations and data operations as defined in the Prisma schema and through service layers.
- Shared utilities and UI components from the “packages/ui” ensure consistency across the application, so include a group for “Shared/Reusable Components & Configurations.”

##### Architectural Patterns & Design Principles
- Modular Architecture – Clear separation between frontend and backend code with explicit directories for controllers, routes, models, and services indicating adherence to separation of concerns.
- Real-time communication by leveraging WebSockets.
- Use of middleware for authorization and input validation enhances security and data integrity.
- Monorepo management with Turborepo facilitates reuse of configuration and UI components across projects.

a. Components:
- Frontend (Next.js/React):
- UI Components (Canvas, Toolbar, Auth pages, etc.)
- Pages and Routes (auth, canvas/[roomId], dashboard)
- State management and hooks (e.g., useSocket)
- Backend (Express):
- Controllers (roomController, userController)
- Routes (authRoutes, roomRoutes, userRoutes)
- Middlewares (authentication, input validation)
- Services (roomService, userService)
- WebSocket Handler (wsHandler.ts)
- Database:
- Prisma-managed PostgreSQL database (as configured in the backend)
- Shared Packages and Configuration:
- UI components, eslint-config, tailwind-config, typescript-config

b. Relationships/Interactions:
- From the Frontend to the Backend for REST API calls (for authentication, room creation, etc.)
- From the Backend to the Database (indicating data CRUD operations via Prisma)
- Optionally, show interactions from the configuration/shared packages feeding into both the Frontend and Backend setups.

c. Design Details:

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Subgraph
    subgraph "User Interface (Frontend)"
        FE["Frontend Application (Next.js/React)"]:::frontend
        FE_Auth["Auth Pages"]:::frontend
        FE_Canvas["Canvas (Dynamic Routing)"]:::frontend
        FE_Dashboard["Dashboard"]:::frontend
        FE_UI["UI Components"]:::frontend
        FE_Hooks["State Management & Hooks (useSocket)"]:::frontend
    end

    %% Backend Subgraph
    subgraph "Business Logic (Backend)"
        BE["Backend Application (Express)"]:::backend
        subgraph "Controllers"
            BE_RCtrl["Room Controller"]:::backend
            BE_UCtrl["User Controller"]:::backend
        end
        subgraph "Routes"
            BE_AuthRoute["Auth Routes"]:::backend
            BE_RoomRoute["Room Routes"]:::backend
            BE_UserRoute["User Routes"]:::backend
        end
        subgraph "Middlewares"
            BE_AuthMW["Authentication Middleware"]:::backend
            BE_ValMW["Input Validation Middleware"]:::backend
        end
        subgraph "Services"
            BE_RService["Room Service"]:::backend
            BE_UService["User Service"]:::backend
        end
        BE_WS["WebSocket Handler"]:::backend
    end

    %% Database Subgraph
    subgraph "Data Persistence (Database)"
        DB["Prisma-managed PostgreSQL Database"]:::database
        DB_Pragma["Prisma Schema & Migrations"]:::database
    end

    %% Shared Packages Subgraph
    subgraph "Shared & Configuration"
        SH_UI["Shared UI Components & Utilities"]:::shared
        SH_TS["TypeScript Configurations"]:::shared
        SH_TW["Tailwind Configurations"]:::shared
        SH_ES["ESLint Configurations"]:::shared
    end

    %% Deployment Subgraph
    subgraph "Deployment"
        DEP_FE["Vercel (Frontend Deployment)"]:::deployment
        DEP_BE["Render (Backend Deployment)"]:::deployment
    end

    %% Connections
    FE -->|"HTTP"| BE
    FE_Hooks <==>|"WS"| BE_WS
    BE -->|"CRUD via Prisma"| DB
    DB -->|"Uses"| DB_Pragma

    %% Shared dependencies
    SH_UI --- FE
    SH_TS --- FE
    SH_TW --- FE
    SH_ES --- FE
    SH_TS --- BE
    SH_TW --- BE
    SH_ES --- BE

    %% Deployment relations
    DEP_FE --- FE
    DEP_BE --- BE

    %% Click Events
    click FE "https://github.com/shahzan01/excali-sketch/tree/main/apps/ExcaliSketch"
    click FE_Auth "https://github.com/shahzan01/excali-sketch/tree/main/apps/ExcaliSketch/src/app/auth"
    click FE_Canvas "https://github.com/shahzan01/excali-sketch/tree/main/apps/ExcaliSketch/src/app/canvas/[roomId]"
    click FE_Dashboard "https://github.com/shahzan01/excali-sketch/tree/main/apps/ExcaliSketch/src/app/dashboard"
    click FE_UI "https://github.com/shahzan01/excali-sketch/tree/main/apps/ExcaliSketch/src/components"
    click FE_Hooks "https://github.com/shahzan01/excali-sketch/blob/main/apps/ExcaliSketch/src/hooks/useSocket.ts"
    click BE "https://github.com/shahzan01/excali-sketch/tree/main/apps/backend"
    click BE_RCtrl "https://github.com/shahzan01/excali-sketch/blob/main/apps/backend/src/controllers/roomController.ts"
    click BE_UCtrl "https://github.com/shahzan01/excali-sketch/blob/main/apps/backend/src/controllers/userController.ts"
    click BE_AuthRoute "https://github.com/shahzan01/excali-sketch/blob/main/apps/backend/src/routes/authRoutes.ts"
    click BE_RoomRoute "https://github.com/shahzan01/excali-sketch/blob/main/apps/backend/src/routes/roomRoutes.ts"
    click BE_UserRoute "https://github.com/shahzan01/excali-sketch/blob/main/apps/backend/src/routes/userRoutes.ts"
    click BE_AuthMW "https://github.com/shahzan01/excali-sketch/blob/main/apps/backend/src/middlewares/authMiddleware.ts"
    click BE_ValMW "https://github.com/shahzan01/excali-sketch/blob/main/apps/backend/src/middlewares/validateInput.ts"
    click BE_RService "https://github.com/shahzan01/excali-sketch/blob/main/apps/backend/src/services/roomService.ts"
    click BE_UService "https://github.com/shahzan01/excali-sketch/blob/main/apps/backend/src/services/userService.ts"
    click BE_WS "https://github.com/shahzan01/excali-sketch/blob/main/apps/backend/src/wsHandler.ts"
    click DB_Pragma "https://github.com/shahzan01/excali-sketch/tree/main/apps/backend/prisma"
    click SH_UI "https://github.com/shahzan01/excali-sketch/tree/main/packages/ui"
    click SH_TS "https://github.com/shahzan01/excali-sketch/tree/main/packages/typescript-config"
    click SH_TW "https://github.com/shahzan01/excali-sketch/tree/main/packages/tailwind-config"
    click SH_ES "https://github.com/shahzan01/excali-sketch/tree/main/packages/eslint-config"

    %% Styles
    classDef frontend fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef backend fill:#90EE90,stroke:#000,stroke-width:2px;
    classDef database fill:#FFFFE0,stroke:#000,stroke-width:2px;
    classDef shared fill:#FFC0CB,stroke:#000,stroke-width:2px;
    classDef deployment fill:#D3D3D3,stroke:#000,stroke-width:2px;
```
