# shivanshusonwani/codecanvas architecture

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

- Interactive diagram: https://gitdiagram.com/shivanshusonwani/codecanvas
- Repository: https://github.com/shivanshusonwani/codecanvas

## Overview

1. **Overall shape**
   - CodeCanvas is a two-tier application: a React/Vite frontend in `client/` and an Express-based API in `server/`.
   - The main product flow is an authenticated code-editor workspace with live preview and persistence for saved canvas content.

2. **Frontend shell**
   - The React app is assembled in `client/src/main.jsx` and `App.jsx`, with page-level routing/sectioning across `Home`, `Login`, `Signup`, and `Canvas`.
   - `Navbar` and the CSS files provide the shared layout and visual structure for the app.

3. **Editor workspace**
   - The core interactive area is the `Canvas` page, which combines the code editor and preview experience.
   - `MonacoEditor.jsx` is the editing surface for HTML/CSS/JS, while `CanvasPreview.jsx` is responsible for rendering the output in a sandboxed preview surface, consistent with the README’s iframe-based live preview model.

4. **Client-side state and auth**
   - `AuthContext.jsx` centralizes authentication state for the frontend.
   - This context is the boundary between login/signup flows and protected app areas, so the UI can react to signed-in state without threading auth props through every component.

5. **Client-to-server API boundary**
   - `client/src/api.js` is the frontend’s API abstraction layer.
   - It isolates HTTP calls from the UI components/pages, making auth and canvas persistence requests go through a single client-side integration point.

6. **Backend entry and app composition**
   - `server/server.js` is the process entrypoint, and `server/src/app.js` composes the Express application.
   - This separation suggests a clean bootstrapping boundary: startup/environment setup in one place, middleware/routes/configuration in another.

7. **Backend domains**
   - The server is split by concern into `auth` and `canvas` domains, each with its own controller and route files.
   - `auth.controller.js` and `auth.routes.js` handle user registration/login-related flows, while `canvas.controller.js` and `canvas.routes.js` handle editor document persistence and retrieval.

8. **Data and persistence**
   - `user.model.js` and `canvas.model.js` represent the two persistent entities in the system: users and saved canvas content.
   - `config/db.js` establishes the database connection, so persistence is a core part of the architecture rather than an afterthought.

9. **Protected backend access**
   - `auth.middleware.js` is the security boundary for server endpoints that require an authenticated user.
   - Canvas-related actions are likely gated here so only authorized requests can read/write persisted canvases.

10. **Runtime and tooling**
   - The frontend uses React with Vite, and the backend uses a Node.js/Express stack.
   - The architecture is designed for fast local development and interactive editing, with the editor runtime, preview runtime, and persistence layer kept deliberately separated.

11. **Key data flow to diagram**
   - User signs in or signs up through the frontend, which calls the auth API and stores auth state in `AuthContext`.
   - In the Canvas workspace, editor changes update the live preview immediately on the client, and saved work can be sent to the backend canvas API for persistence.
   - The backend validates/authenticates requests, reads/writes via the models, and returns data back to the UI.

12. **Important boundaries**
   - Keep the preview sandbox separate from the editor state: the preview is a render target, not the source of truth.
   - Keep auth state on the client distinct from backend authorization checks: `AuthContext` manages UX state, while middleware enforces access control on the server.

Main entry points: [`client/src/App.jsx`](https://github.com/shivanshusonwani/codecanvas/blob/main/client/src/App.jsx) (Routes), [`client/src/pages/Canvas.jsx`](https://github.com/shivanshusonwani/codecanvas/blob/main/client/src/pages/Canvas.jsx) (Canvas page), [`client/src/api.js`](https://github.com/shivanshusonwani/codecanvas/blob/main/client/src/api.js) (API client), [`server/src/routes/canvas.routes.js`](https://github.com/shivanshusonwani/codecanvas/blob/main/server/src/routes/canvas.routes.js) (Canvas routes)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Client"]
  node_client_main["App shell<br/>react entry<br/>[main.jsx]"]
  node_client_app["Routes<br/>react layout<br/>[App.jsx]"]
  node_client_navbar["Navbar<br/>shared ui<br/>[Navbar.jsx]"]
  node_client_home["Home page<br/>landing page<br/>[Home.jsx]"]
  node_client_login["Login page<br/>auth page<br/>[Login.jsx]"]
  node_client_signup["Signup page<br/>auth page<br/>[Signup.jsx]"]
  node_client_canvas["Canvas page<br/>workspace page<br/>[Canvas.jsx]"]
  node_client_editor["Editor<br/>[MonacoEditor.jsx]"]
  node_client_preview["Preview<br/>render surface<br/>[CanvasPreview.jsx]"]
  node_client_authctx["Auth state<br/>client auth<br/>[AuthContext.jsx]"]
  node_client_api["API client<br/>http adapter<br/>[api.js]"]
end

subgraph group_server["Server"]
  node_server_entry["Server boot<br/>node entry<br/>[server.js]"]
  node_server_app["Express app<br/>api composition<br/>[app.js]"]
  node_server_auth_routes["Auth routes<br/>http routes<br/>[auth.routes.js]"]
  node_server_canvas_routes["Canvas routes<br/>http routes<br/>[canvas.routes.js]"]
  node_server_auth_ctrl["Auth logic<br/>controller<br/>[auth.controller.js]"]
  node_server_canvas_ctrl["Canvas logic<br/>controller"]
  node_server_auth_mw["Auth guard<br/>middleware<br/>[auth.middleware.js]"]
  node_server_user_model[("Users<br/>data model<br/>[user.model.js]")]
  node_server_canvas_model[("Canvases<br/>data model<br/>[canvas.model.js]")]
  node_server_db[("Database<br/>connection<br/>[db.js]")]
end

node_client_main -->|"mounts"| node_client_app
node_client_app -->|"shared chrome"| node_client_navbar
node_client_app -->|"routes"| node_client_home
node_client_app -->|"routes"| node_client_login
node_client_app -->|"routes"| node_client_signup
node_client_app -->|"routes"| node_client_canvas
node_client_canvas -->|"edits"| node_client_editor
node_client_canvas -->|"renders"| node_client_preview
node_client_canvas -->|"checks auth"| node_client_authctx
node_client_login -->|"updates"| node_client_authctx
node_client_signup -->|"updates"| node_client_authctx
node_client_login -->|"calls"| node_client_api
node_client_signup -->|"calls"| node_client_api
node_client_canvas -->|"saves"| node_client_api
node_client_api -->|"auth api"| node_server_auth_routes
node_client_api -->|"canvas api"| node_server_canvas_routes
node_server_entry -->|"starts"| node_server_app
node_server_app -->|"mounts"| node_server_auth_routes
node_server_app -->|"mounts"| node_server_canvas_routes
node_server_auth_routes -->|"delegates"| node_server_auth_ctrl
node_server_canvas_routes -->|"delegates"| node_server_canvas_ctrl
node_server_canvas_routes -->|"protects"| node_server_auth_mw
node_server_auth_ctrl -->|"reads/writes"| node_server_user_model
node_server_canvas_ctrl -->|"reads/writes"| node_server_canvas_model
node_server_user_model -->|"uses"| node_server_db
node_server_canvas_model -->|"uses"| node_server_db

click node_client_main "https://github.com/shivanshusonwani/codecanvas/blob/main/client/src/main.jsx"
click node_client_app "https://github.com/shivanshusonwani/codecanvas/blob/main/client/src/App.jsx"
click node_client_navbar "https://github.com/shivanshusonwani/codecanvas/blob/main/client/src/components/Navbar.jsx"
click node_client_home "https://github.com/shivanshusonwani/codecanvas/blob/main/client/src/pages/Home.jsx"
click node_client_login "https://github.com/shivanshusonwani/codecanvas/blob/main/client/src/pages/Login.jsx"
click node_client_signup "https://github.com/shivanshusonwani/codecanvas/blob/main/client/src/pages/Signup.jsx"
click node_client_canvas "https://github.com/shivanshusonwani/codecanvas/blob/main/client/src/pages/Canvas.jsx"
click node_client_editor "https://github.com/shivanshusonwani/codecanvas/blob/main/client/src/components/MonacoEditor.jsx"
click node_client_preview "https://github.com/shivanshusonwani/codecanvas/blob/main/client/src/components/CanvasPreview.jsx"
click node_client_authctx "https://github.com/shivanshusonwani/codecanvas/blob/main/client/src/context/AuthContext.jsx"
click node_client_api "https://github.com/shivanshusonwani/codecanvas/blob/main/client/src/api.js"
click node_server_entry "https://github.com/shivanshusonwani/codecanvas/blob/main/server/server.js"
click node_server_app "https://github.com/shivanshusonwani/codecanvas/blob/main/server/src/app.js"
click node_server_auth_routes "https://github.com/shivanshusonwani/codecanvas/blob/main/server/src/routes/auth.routes.js"
click node_server_canvas_routes "https://github.com/shivanshusonwani/codecanvas/blob/main/server/src/routes/canvas.routes.js"
click node_server_auth_ctrl "https://github.com/shivanshusonwani/codecanvas/blob/main/server/src/controllers/auth.controller.js"
click node_server_canvas_ctrl "https://github.com/shivanshusonwani/codecanvas/blob/main/server/src/controllers/canvas.controller.js"
click node_server_auth_mw "https://github.com/shivanshusonwani/codecanvas/blob/main/server/src/middleware/auth.middleware.js"
click node_server_user_model "https://github.com/shivanshusonwani/codecanvas/blob/main/server/src/models/user.model.js"
click node_server_canvas_model "https://github.com/shivanshusonwani/codecanvas/blob/main/server/src/models/canvas.model.js"
click node_server_db "https://github.com/shivanshusonwani/codecanvas/blob/main/server/src/config/db.js"

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_client_main,node_client_app,node_client_navbar,node_client_home,node_client_login,node_client_signup,node_client_canvas,node_client_editor,node_client_preview,node_client_authctx,node_client_api toneBlue
class node_server_entry,node_server_app,node_server_auth_routes,node_server_canvas_routes,node_server_auth_ctrl,node_server_canvas_ctrl,node_server_auth_mw,node_server_user_model,node_server_canvas_model,node_server_db toneAmber
```

## Components

### Client

React/Vite frontend workspace and editor UI.

- [App shell](https://github.com/shivanshusonwani/codecanvas/blob/main/client/src/main.jsx): react entry · Bootstraps the frontend React tree. · `client/src/main.jsx`
- [Routes](https://github.com/shivanshusonwani/codecanvas/blob/main/client/src/App.jsx): react layout · Assembles the top-level app structure and page sections. · `client/src/App.jsx`
- [Navbar](https://github.com/shivanshusonwani/codecanvas/blob/main/client/src/components/Navbar.jsx): shared ui · Shared navigation and header chrome. · `client/src/components/Navbar.jsx`
- [Home page](https://github.com/shivanshusonwani/codecanvas/blob/main/client/src/pages/Home.jsx): landing page · Public entry page for the product. · `client/src/pages/Home.jsx`
- [Login page](https://github.com/shivanshusonwani/codecanvas/blob/main/client/src/pages/Login.jsx): auth page · User sign-in surface. · `client/src/pages/Login.jsx`
- [Signup page](https://github.com/shivanshusonwani/codecanvas/blob/main/client/src/pages/Signup.jsx): auth page · User registration surface. · `client/src/pages/Signup.jsx`
- [Canvas page](https://github.com/shivanshusonwani/codecanvas/blob/main/client/src/pages/Canvas.jsx): workspace page · Authenticated editor workspace that combines editing, preview, and save flows. · `client/src/pages/Canvas.jsx`
- [Editor](https://github.com/shivanshusonwani/codecanvas/blob/main/client/src/components/MonacoEditor.jsx): code editor · Monaco-based HTML/CSS/JS editing surface. · `client/src/components/MonacoEditor.jsx`
- [Preview](https://github.com/shivanshusonwani/codecanvas/blob/main/client/src/components/CanvasPreview.jsx): render surface · Sandboxed live preview of the canvas output. · `client/src/components/CanvasPreview.jsx`
- [Auth state](https://github.com/shivanshusonwani/codecanvas/blob/main/client/src/context/AuthContext.jsx): client auth · Frontend authentication state and session boundary. · `client/src/context/AuthContext.jsx`
- [API client](https://github.com/shivanshusonwani/codecanvas/blob/main/client/src/api.js): http adapter · Single client-side integration point for backend requests. · `client/src/api.js`

### Server

Express API, domain logic, and persistence.

- [Server boot](https://github.com/shivanshusonwani/codecanvas/blob/main/server/server.js): node entry · Process entrypoint and startup wiring. · `server/server.js`
- [Express app](https://github.com/shivanshusonwani/codecanvas/blob/main/server/src/app.js): api composition · Composes middleware, routing, and server configuration. · `server/src/app.js`
- [Auth routes](https://github.com/shivanshusonwani/codecanvas/blob/main/server/src/routes/auth.routes.js): http routes · Registration and login endpoints. · `server/src/routes/auth.routes.js`
- [Canvas routes](https://github.com/shivanshusonwani/codecanvas/blob/main/server/src/routes/canvas.routes.js): http routes · Persistence endpoints for saved canvas content. · `server/src/routes/canvas.routes.js`
- [Auth logic](https://github.com/shivanshusonwani/codecanvas/blob/main/server/src/controllers/auth.controller.js): controller · Handles signup/login request behavior. · `server/src/controllers/auth.controller.js`
- [Canvas logic](https://github.com/shivanshusonwani/codecanvas/blob/main/server/src/controllers/canvas.controller.js): controller · Handles canvas save/read behavior. · `server/src/controllers/canvas.controller.js`
- [Auth guard](https://github.com/shivanshusonwani/codecanvas/blob/main/server/src/middleware/auth.middleware.js): middleware · Enforces authenticated access on protected endpoints. · `server/src/middleware/auth.middleware.js`
- [Users](https://github.com/shivanshusonwani/codecanvas/blob/main/server/src/models/user.model.js): data model · Persistent user entity. · `server/src/models/user.model.js`
- [Canvases](https://github.com/shivanshusonwani/codecanvas/blob/main/server/src/models/canvas.model.js): data model · Persistent canvas document entity. · `server/src/models/canvas.model.js`
- [Database](https://github.com/shivanshusonwani/codecanvas/blob/main/server/src/config/db.js): connection · Database connection setup used by the models. · `server/src/config/db.js`

## Connections

- App shell → Routes: mounts · Frontend entry renders the app shell.
- Routes → Navbar: shared chrome · App shell includes common navigation.
- Routes → Home page: routes · Public landing section.
- Routes → Login page: routes · Sign-in section.
- Routes → Signup page: routes · Registration section.
- Routes → Canvas page: routes · Authenticated editor workspace.
- Canvas page → Editor: edits · Canvas page hosts the code editor.
- Canvas page → Preview: renders · Canvas page pushes output to the preview surface.
- Canvas page → Auth state: checks auth · Canvas behavior depends on client auth state.
- Login page → Auth state: updates · Login flow updates frontend auth state.
- Signup page → Auth state: updates · Signup flow updates frontend auth state.
- Login page → API client: calls · Auth requests go through the API layer.
- Signup page → API client: calls · Registration requests go through the API layer.
- Canvas page → API client: saves · Workspace persistence uses the API client.
- API client → Auth routes: auth api · Frontend auth requests reach server auth endpoints.
- API client → Canvas routes: canvas api · Frontend canvas requests reach server canvas endpoints.
- Server boot → Express app: starts · Entry script boots the Express app.
- Express app → Auth routes: mounts · Express app exposes auth routes.
- Express app → Canvas routes: mounts · Express app exposes canvas routes.
- Auth routes → Auth logic: delegates · Auth routing passes to controller logic.
- Canvas routes → Canvas logic: delegates · Canvas routing passes to controller logic.
- Canvas routes → Auth guard: protects · Canvas access is gated by auth middleware.
- Auth logic → Users: reads/writes · Auth logic persists and looks up users.
- Canvas logic → Canvases: reads/writes · Canvas logic persists and retrieves canvases.
- Users → Database: uses · User model depends on the DB connection.
- Canvases → Database: uses · Canvas model depends on the DB connection.
