# kodejuice/leve architecture

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

- Interactive diagram: https://gitdiagram.com/kodejuice/leve
- Repository: https://github.com/kodejuice/leve

## Overview

##### Project type and purpose
- This is a full-stack, monolithic CMS built with Next.js.
- It serves both the website UI (React pages/components under “pages/” and “components/”) and a REST-style API (Next.js API routes under “pages/api/”) from the same codebase.
- It uses MongoDB as its primary data store and provides an admin interface for creating, editing, importing/exporting posts and quotes.

##### File structure
- Frontend/UI
– pages/ (Next.js pages: dynamic routes “[...id].js”, _app.js, _document.js, admin pages under pages/admin/)
– components/ (shared UI: home, pages, sections, admin subfolder)
– assets/ (SASS styles, fonts)
- Backend/API
– pages/api/ (auth/login.js, post endpoints, RSS and sitemap endpoints)
– utils/ (auth.js, db_requests.js, mdParser.js, swr.js, etc.)
– database/ (connection.js, functions.js, model.js)
- Data‐import tooling
– data/quotes/indexer (scripts to build quotes index at build-time)
– data/quotes/quotes.js (static quotes data)
- Configuration
– next.config.js, site_config.js, .env.example

##### README for architecture clues
- Confirms Next.js SSR/CSR model
- Requires MongoDB (external DB service)
- Mentions environment variables and build/run steps

##### Main components and interactions
a. Client (Browser)
– Sends HTTP requests for pages (SSR or CSR)
– Fetches JSON from Next.js API (via fetch/SWR in utils/swr.js)
– Loads static assets from public/ and compiled assets from assets/
b. Next.js Server
– SSR/SSG: pages/* use getServerSideProps/getStaticProps to call database functions
– API layer: pages/api/* handlers invoke database.functions via database/connection.js
– Admin UI pages call API endpoints for CRUD operations
– Data‐import scripts (data/quotes/indexer) run at build time or on-demand
c. Database (MongoDB)
– Hosts collections for posts, quotes, users (models defined in database/model.js)
– Connected via database/connection.js, used by database/functions.js
d. External services
– Google Analytics (component GoogleAnalytics.js)
– RSS feed generator (API endpoints pages/api/rss*.js)
– Sitemap (pages/api/sitemap.xml.js)

##### Architectural patterns & design principles
- Monolith with clear separation:
– “Views” = pages/ + components/
– “Controllers” = API route handlers (pages/api/)
– “Models” = database/model.js + database/functions.js
- SSR/SSG hybrid: some pages rendered server-side, others fetched client-side with SWR
- Build-time data injection: quotes indexed via local scripts in data/quotes/indexer
- Single codebase for both UI and API (Next.js strength)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client
    subgraph "Client Browser"
        BROWSER["Browser"]:::external
    end

    %% Next.js Server
    subgraph "Next.js Server"
        subgraph "UI Layer"
            PAGES["Next.js Pages (pages/)"]:::frontend
            COMPONENTS["Shared React Components (components/)"]:::frontend
        end
        subgraph "API Layer"
            API_AUTH["Auth API (pages/api/auth/login.js)"]:::server
            API_POST_LIST["Post List API (pages/api/post/list.js)"]:::server
            API_POST_ID["Post API (pages/api/post/[post_id].js)"]:::server
            API_POST_IMP["Import/Export API (pages/api/post/import_export.js)"]:::server
            API_RSS_CORE["RSS XML (pages/api/rss.xml.js)"]:::server
            API_RSS_CAT["RSS Category (pages/api/rss/[category].js)"]:::server
            API_SITEMAP["Sitemap (pages/api/sitemap.xml.js)"]:::server
        end
        subgraph "Utils & Logic"
            UTILS_AUTH["Auth Helpers (utils/auth.js)"]:::server
            UTILS_DBREQ["DB Requests (utils/db_requests.js)"]:::server
            UTILS_SWR["SWR Hook (utils/swr.js)"]:::server
            UTILS_MD["Markdown Parser (utils/mdParser.js)"]:::server
        end
        subgraph "Database Module"
            DB_CONN["Connection (database/connection.js)"]:::database
            DB_MODEL["Models (database/model.js)"]:::database
            DB_FUNCS["Functions (database/functions.js)"]:::database
        end
        subgraph "Data-Importer"
            INDEXER["Quote Indexer (data/quotes/indexer)"]:::external
            QUOTES_SRC["Static Quotes (data/quotes/quotes.js)"]:::external
        end
        subgraph "Config"
            NEXT_CONF["next.config.js"]:::config
            SITE_CONF["site_config.js"]:::config
            ENV_FILE[".env.example"]:::config
        end
    end

    %% External DB and Services
    MONGO["MongoDB"]:::database
    subgraph "External Services"
        GA["Google Analytics (components/GoogleAnalytics.js)"]:::external
    end

    %% Relationships
    BROWSER -->|"GET /"| PAGES
    BROWSER -->|"CSR fetch"| API_POST_LIST
    BROWSER -->|"CSR fetch"| API_AUTH
    API_AUTH -->|calls| UTILS_AUTH
    API_POST_LIST -->|calls| UTILS_DBREQ
    API_POST_ID -->|calls| UTILS_DBREQ
    API_POST_IMP -->|calls| UTILS_DBREQ
    API_RSS_CORE -->|calls| UTILS_MD
    API_RSS_CAT -->|calls| UTILS_MD
    API_SITEMAP -->|calls| UTILS_MD
    UTILS_DBREQ -->|uses| DB_CONN
    DB_CONN -->|connects to| MONGO
    DB_CONN -->|loads models| DB_MODEL
    DB_FUNCS -->|executes| DB_CONN
    INDEXER -->|reads| QUOTES_SRC
    INDEXER -->|populates| DB_FUNCS
    PAGES -->|SSR calls| DB_FUNCS
    COMPONENTS -->|tracks| GA

    %% Click Events
    click PAGES "https://github.com/kodejuice/leve/tree/main/pages/"
    click COMPONENTS "https://github.com/kodejuice/leve/tree/main/components/"
    click API_AUTH "https://github.com/kodejuice/leve/blob/main/pages/api/auth/login.js"
    click API_POST_LIST "https://github.com/kodejuice/leve/blob/main/pages/api/post/list.js"
    click API_POST_ID "https://github.com/kodejuice/leve/blob/main/pages/api/post/[post_id].js"
    click API_POST_IMP "https://github.com/kodejuice/leve/blob/main/pages/api/post/import_export.js"
    click API_RSS_CORE "https://github.com/kodejuice/leve/blob/main/pages/api/rss.xml.js"
    click API_RSS_CAT "https://github.com/kodejuice/leve/blob/main/pages/api/rss/[category].js"
    click API_SITEMAP "https://github.com/kodejuice/leve/blob/main/pages/api/sitemap.xml.js"
    click UTILS_AUTH "https://github.com/kodejuice/leve/blob/main/utils/auth.js"
    click UTILS_DBREQ "https://github.com/kodejuice/leve/blob/main/utils/db_requests.js"
    click UTILS_SWR "https://github.com/kodejuice/leve/blob/main/utils/swr.js"
    click UTILS_MD "https://github.com/kodejuice/leve/blob/main/utils/mdParser.js"
    click DB_CONN "https://github.com/kodejuice/leve/blob/main/database/connection.js"
    click DB_MODEL "https://github.com/kodejuice/leve/blob/main/database/model.js"
    click DB_FUNCS "https://github.com/kodejuice/leve/blob/main/database/functions.js"
    click INDEXER "https://github.com/kodejuice/leve/blob/main/data/quotes/indexer/index.js"
    click QUOTES_SRC "https://github.com/kodejuice/leve/blob/main/data/quotes/quotes.js"
    click NEXT_CONF "https://github.com/kodejuice/leve/blob/main/next.config.js"
    click SITE_CONF "https://github.com/kodejuice/leve/blob/main/site_config.js"
    click ENV_FILE "https://github.com/kodejuice/leve/blob/main/.env.example"
    click GA "https://github.com/kodejuice/leve/blob/main/components/GoogleAnalytics.js"

    %% Styles
    classDef frontend fill:#9f6,stroke:#333,stroke-width:1px
    classDef server fill:#6cf,stroke:#333,stroke-width:1px
    classDef database fill:#fc6,stroke:#333,stroke-width:1px
    classDef external fill:#c6f,stroke:#333,stroke-width:1px
    classDef config fill:#ccc,stroke:#333,stroke-width:1px
```
