# imtiajrex/artspace architecture

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

- Interactive diagram: https://gitdiagram.com/imtiajrex/artspace
- Repository: https://github.com/imtiajrex/artspace

## Overview

##### Project Type and Purpose
- This is a full-stack web application (“ArtSpace”) built with SvelteKit (handles both frontend UI and backend API) and MySQL for persistence, with Cloudinary used for art asset storage.
- Users (artists) can sign up, upload art, view others’ art, leave comments and ratings. Admins have a protected area to manage users, moderate content, and view dashboards.

##### High-Level Architecture Components
A. Client (Browser)
- Renders SvelteKit pages (SSR + SPA hydration)
- Sends HTTP requests to API routes for login, signup, art listing, posting comments/ratings
- Uploads images via client code (e.g., Cloudinary widget or direct upload endpoint)

B. SvelteKit Server
1. Routing Layer
– File-based routes under src/routes/ for page rendering (+layout.svelte, +page.svelte)
– API endpoints under src/routes/api/** with +server.js handlers
2. Server Utilities (src/lib/server)
– db.ts: wraps MySQL connection/query logic
– cloudinary.ts: handles image upload and URL generation
– adminTokenChecker.ts: middleware to verify admin JWT tokens
3. Component Library (src/lib/components)
– Shared Svelte UI components (buttons, cards, forms, navbars)
– Admin-specific components (dashboard cards, moderation buttons)
– Home feed components, user profile UI elements

C. MySQL Database
– Stores users, admins, art metadata, comments, ratings
– Accessed by db.ts using SQL queries

D. Cloudinary (External Service)
– Stores uploaded image files
– Provides CDN URLs for art images

##### Relationships and Data Flows
- Browser → SvelteKit Server (Pages)
– User navigates: SSR fetches data via load functions that call internal API or db.ts
- Browser → SvelteKit Server (API Endpoints)
– AJAX/Fetch requests to /api/* for CRUD operations (signup, login, art create/list, comments, ratings)
- SvelteKit Server → MySQL
– db.ts executes queries and returns results to API handlers or load functions
- SvelteKit Server → Cloudinary
– cloudinary.ts uploads image binaries and returns secure URLs stored in MySQL
- AdminTokenChecker middleware
– Intercepts admin API routes, verifies JWT, allows/denies access

##### Key Architectural Patterns and Design Principles
– File-based routing (SvelteKit) enforces clear separation of pages vs API
– Layered separation: Presentation (components & pages), Application logic (API handlers & load functions), Infrastructure (db.ts, cloudinary.ts)
– Middleware for auth/authorization (adminTokenChecker)
– Single codebase for frontend SSR + backend API simplifies deployment

– Clear labels on each component (e.g., “+page.svelte / +server.js”, “db.ts”)
– Arrowheads indicating request/response or data flow direction

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Browser
    Browser["Browser\n(SvelteKit UI SSR + SPA)"]:::ui

    %% SvelteKit Server
    subgraph "SvelteKit Server"
        direction TB
        %% Routing Layer
        subgraph "Routing Layer"
            direction TB
            %% Page Rendering
            P1["src/routes/+page.svelte"]:::ui
            P2["src/routes/+layout.svelte"]:::ui
            P3["src/routes/login/+page.svelte"]:::ui
            P4["src/routes/signup/+page.svelte"]:::ui
            P5["src/routes/post/[id]/+page.svelte"]:::ui
            P6["src/routes/post/[id]/+page.js"]:::backend
            P7["src/routes/user/[id]/+page.svelte"]:::ui
            P8["src/routes/(admin)/+layout.svelte"]:::ui
            P9["src/routes/(admin)/admin/+page.svelte"]:::ui
            P10["src/routes/(admin)/admin/manage-admins/+page.svelte"]:::ui
            P11["src/routes/(admin)/admin/moderate-posts/+page.svelte"]:::ui
            P12["src/routes/(admin)/admin/profile/+page.svelte"]:::ui
            P13["src/routes/admin/login/+page.svelte"]:::ui
            %% API Endpoints
            A1["src/routes/api/signup/+server.js"]:::backend
            A2["src/routes/api/login/+server.js"]:::backend
            A3["src/routes/api/arts/+server.js"]:::backend
            A4["src/routes/api/arts/[search]/+server.js"]:::backend
            A5["src/routes/api/art/[id]/+server.js"]:::backend
            A6["src/routes/api/comments/[id]/+server.js"]:::backend
            A7["src/routes/api/ratings/[id]/+server.js"]:::backend
            A8["src/routes/api/admin/login/+server.js"]:::backend
            A9["src/routes/api/admin/dashboard/+server.js"]:::backend
            A10["src/routes/api/admin/manage/+server.js"]:::backend
            A11["src/routes/api/admin/manage/[id]/+server.js"]:::backend
            A12["src/routes/api/admin/moderate/+server.js"]:::backend
            A13["src/routes/api/admin/moderate/[id]/+server.js"]:::backend
            A14["src/routes/api/admin/profile/[id]/+server.js"]:::backend
        end

        %% Server Utilities
        subgraph "Server Utilities"
            direction TB
            U1["src/lib/server/db.ts"]:::util
            U2["src/lib/server/cloudinary.ts"]:::util
            U3["src/lib/server/adminTokenChecker.ts"]:::util
        end

        %% Component Library
        subgraph "Component Library"
            direction TB
            C1["src/lib/components/button.svelte"]:::ui
            C2["src/lib/components/input.svelte"]:::ui
            C3["src/lib/components/logo.svelte"]:::ui
            C4["src/lib/components/search-bar.svelte"]:::ui
            C5["src/lib/components/select.svelte"]:::ui
            C6["src/lib/components/home/card.svelte"]:::ui
            C7["src/lib/components/home/header.svelte"]:::ui
            C8["src/lib/components/home/posts.svelte"]:::ui
            C9["src/lib/components/home/uploader.svelte"]:::ui
            C10["src/lib/components/home/user.svelte"]:::ui
            C11["src/lib/components/admin/navbar.svelte"]:::ui
            C12["src/lib/components/admin/modal.svelte"]:::ui
            C13["src/lib/components/admin/dashboard/StatCard.svelte"]:::ui
            C14["src/lib/components/admin/dashboard/page-links.svelte"]:::ui
            C15["src/lib/components/admin/manage/add-admin.svelte"]:::ui
            C16["src/lib/components/admin/manage/edit-admin.svelte"]:::ui
            C17["src/lib/components/admin/manage/delete.svelte"]:::ui
            C18["src/lib/components/admin/moderation/moderation-btn.svelte"]:::ui
            C19["src/lib/components/admin/moderation/post-card.svelte"]:::ui
            C20["src/lib/components/admin/profile/editModal.svelte"]:::ui
        end
    end

    %% External Services
    MySQL["MySQL Database"]:::ext
    Cloudinary["Cloudinary"]:::ext

    %% Connections
    Browser -->|"Page Requests"| P1
    Browser -->|"Page Requests"| P2
    Browser -->|"API Requests"| A1
    Browser -->|"API Requests"| A2
    Browser -->|"API Requests"| A3

    A1 --> U1
    A2 --> U1
    A3 --> U1
    A4 --> U1
    A5 --> U1
    A6 --> U1
    A7 --> U1
    A8 --> U1
    A9 --> U1
    A10 --> U1
    A11 --> U1
    A12 --> U1
    A13 --> U1
    A14 --> U1

    A3 --> U2
    A5 --> U2

    U1 --> MySQL
    U2 --> Cloudinary

    %% Click Events for routing pages
    click P1 "https://github.com/imtiajrex/artspace/blob/master/src/routes/+page.svelte"
    click P2 "https://github.com/imtiajrex/artspace/blob/master/src/routes/+layout.svelte"
    click P3 "https://github.com/imtiajrex/artspace/blob/master/src/routes/login/+page.svelte"
    click P4 "https://github.com/imtiajrex/artspace/blob/master/src/routes/signup/+page.svelte"
    click P5 "https://github.com/imtiajrex/artspace/blob/master/src/routes/post/[id]/+page.svelte"
    click P6 "https://github.com/imtiajrex/artspace/blob/master/src/routes/post/[id]/+page.js"
    click P7 "https://github.com/imtiajrex/artspace/blob/master/src/routes/user/[id]/+page.svelte"
    click P8 "https://github.com/imtiajrex/artspace/blob/master/src/routes/(admin)/+layout.svelte"
    click P9 "https://github.com/imtiajrex/artspace/blob/master/src/routes/(admin)/admin/+page.svelte"
    click P10 "https://github.com/imtiajrex/artspace/blob/master/src/routes/(admin)/admin/manage-admins/+page.svelte"
    click P11 "https://github.com/imtiajrex/artspace/blob/master/src/routes/(admin)/admin/moderate-posts/+page.svelte"
    click P12 "https://github.com/imtiajrex/artspace/blob/master/src/routes/(admin)/admin/profile/+page.svelte"
    click P13 "https://github.com/imtiajrex/artspace/blob/master/src/routes/admin/login/+page.svelte"

    %% Click Events for API endpoints
    click A1 "https://github.com/imtiajrex/artspace/blob/master/src/routes/api/signup/+server.js"
    click A2 "https://github.com/imtiajrex/artspace/blob/master/src/routes/api/login/+server.js"
    click A3 "https://github.com/imtiajrex/artspace/blob/master/src/routes/api/arts/+server.js"
    click A4 "https://github.com/imtiajrex/artspace/blob/master/src/routes/api/arts/[search]/+server.js"
    click A5 "https://github.com/imtiajrex/artspace/blob/master/src/routes/api/art/[id]/+server.js"
    click A6 "https://github.com/imtiajrex/artspace/blob/master/src/routes/api/comments/[id]/+server.js"
    click A7 "https://github.com/imtiajrex/artspace/blob/master/src/routes/api/ratings/[id]/+server.js"
    click A8 "https://github.com/imtiajrex/artspace/blob/master/src/routes/api/admin/login/+server.js"
    click A9 "https://github.com/imtiajrex/artspace/blob/master/src/routes/api/admin/dashboard/+server.js"
    click A10 "https://github.com/imtiajrex/artspace/blob/master/src/routes/api/admin/manage/+server.js"
    click A11 "https://github.com/imtiajrex/artspace/blob/master/src/routes/api/admin/manage/[id]/+server.js"
    click A12 "https://github.com/imtiajrex/artspace/blob/master/src/routes/api/admin/moderate/+server.js"
    click A13 "https://github.com/imtiajrex/artspace/blob/master/src/routes/api/admin/moderate/[id]/+server.js"
    click A14 "https://github.com/imtiajrex/artspace/blob/master/src/routes/api/admin/profile/[id]/+server.js"

    %% Click Events for utilities
    click U1 "https://github.com/imtiajrex/artspace/blob/master/src/lib/server/db.ts"
    click U2 "https://github.com/imtiajrex/artspace/blob/master/src/lib/server/cloudinary.ts"
    click U3 "https://github.com/imtiajrex/artspace/blob/master/src/lib/server/adminTokenChecker.ts"

    %% Click Events for components
    click C1 "https://github.com/imtiajrex/artspace/blob/master/src/lib/components/button.svelte"
    click C2 "https://github.com/imtiajrex/artspace/blob/master/src/lib/components/input.svelte"
    click C3 "https://github.com/imtiajrex/artspace/blob/master/src/lib/components/logo.svelte"
    click C4 "https://github.com/imtiajrex/artspace/blob/master/src/lib/components/search-bar.svelte"
    click C5 "https://github.com/imtiajrex/artspace/blob/master/src/lib/components/select.svelte"
    click C6 "https://github.com/imtiajrex/artspace/blob/master/src/lib/components/home/card.svelte"
    click C7 "https://github.com/imtiajrex/artspace/blob/master/src/lib/components/home/header.svelte"
    click C8 "https://github.com/imtiajrex/artspace/blob/master/src/lib/components/home/posts.svelte"
    click C9 "https://github.com/imtiajrex/artspace/blob/master/src/lib/components/home/uploader.svelte"
    click C10 "https://github.com/imtiajrex/artspace/blob/master/src/lib/components/home/user.svelte"
    click C11 "https://github.com/imtiajrex/artspace/blob/master/src/lib/components/admin/navbar.svelte"
    click C12 "https://github.com/imtiajrex/artspace/blob/master/src/lib/components/admin/modal.svelte"
    click C13 "https://github.com/imtiajrex/artspace/blob/master/src/lib/components/admin/dashboard/StatCard.svelte"
    click C14 "https://github.com/imtiajrex/artspace/blob/master/src/lib/components/admin/dashboard/page-links.svelte"
    click C15 "https://github.com/imtiajrex/artspace/blob/master/src/lib/components/admin/manage/add-admin.svelte"
    click C16 "https://github.com/imtiajrex/artspace/blob/master/src/lib/components/admin/manage/edit-admin.svelte"
    click C17 "https://github.com/imtiajrex/artspace/blob/master/src/lib/components/admin/manage/delete.svelte"
    click C18 "https://github.com/imtiajrex/artspace/blob/master/src/lib/components/admin/moderation/moderation-btn.svelte"
    click C19 "https://github.com/imtiajrex/artspace/blob/master/src/lib/components/admin/moderation/post-card.svelte"
    click C20 "https://github.com/imtiajrex/artspace/blob/master/src/lib/components/admin/profile/editModal.svelte"

    %% Styles
    classDef ui fill:#ccf,stroke:#333,stroke-width:1px
    classDef backend fill:#cfc,stroke:#333,stroke-width:1px
    classDef util fill:#ffe0b2,stroke:#333,stroke-width:1px
    classDef ext fill:#e0e0e0,stroke:#333,stroke-width:1px
```
