# josephng95/thorshop architecture

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

- Interactive diagram: https://gitdiagram.com/josephng95/thorshop
- Repository: https://github.com/josephng95/thorshop

## Overview

##### Project type and purpose
- thorshop is a full-stack web application built with Node.js and Express.
- It provides a public shop frontend (views/shop, public assets) and a backend “Thor” admin panel (views/thor, controllers/backend).
- It uses LowDB (a file-based JSON database) to store data (models/*.js + database/db.js) and Bootstrap for styling.

##### File structure
- Top-level:
- app.js & bin/www – HTTP server bootstrap
- controllers/ – request handlers divided into frontend (shop) and backend (Thor)
- routes/ – Express router definitions (index.js, thor.js)
- models/ – data models (category, product, sell, user, auth)
- database/ – LowDB config and example JSON
- views/ – Jade templates for shop and Thor UI
- public/ & resource/ – static assets (CSS, JS, images)
- middlewares/ – custom middleware (utils, file upload, auth checks)
- utils/ – helper functions and constants

##### README
- Confirms use of Node.js, Express, LowDB, Bootstrap.

a. Main components
- Browser (Client) – Shop UI & Thor Admin UI
- Web Server (Express app.js + bin/www)
- Controllers & Routes – request processing layer
- Models & Database Adapter (LowDB)
- File System (JSON files for LowDB)
- Static Asset Server (public/ & resource/)
- Middleware Layer (authentication, file upload, utilities)
- External Libraries (Express, LowDB, Jade, Bootstrap)

b. Relationships & interactions
1. Client → HTTP Request → Express Routes
2. Routes → Controllers (frontend or backend)
3. Controllers → Models → Database (read/write JSON)
4. Controllers → Render Jade views or return JSON
5. Client ← HTTP Response (HTML + CSS/JS or JSON)
6. Static Asset Requests → public/ or resource/ directory
7. Middleware intercepts requests (auth, uploads, logging)

c. Architectural patterns
- MVC pattern:
- M: models/ + database/db.js
- V: views/ (Jade templates) + public assets
- C: controllers/ + routes/
- Layered Architecture:
- Presentation (views, public)
- Application (routes, controllers)
- Domain (models, utils)
- Infrastructure (database, file system)
- Single Node.js process hosting both frontend and admin backend, separated by route prefixes (e.g., /thor)

d. Key technologies
- Node.js & Express as HTTP server framework
- LowDB for JSON-based persistence
- Jade (Pug) for server-side templating
- Bootstrap & custom CSS/JS for UI
- Custom middleware (middlewares/thor.js) for admin authentication
- Utility modules (utils/*.js) for string, date, upload, constants

##### Detailed capture of architectural elements
- Bin/www script initializing the Express app.
- Environment configuration (.env.example) feeding into app.js.
- Out how models import database/db.js to perform CRUD.
- How utils and middlewares are injected into routes/controllers.
- Static URL prefix (/css, /js, /images) to public/ and resource/ folders.

## Diagram (Mermaid)

```mermaid
flowchart TD
    ClientShop["Shop Browser"]:::client
    ClientAdmin["Thor Admin Browser"]:::client

    subgraph "Express Server"
        direction TB

        subgraph "Initialization"
            direction TB
            Env[".env.example"]:::config
            AppJS["app.js"]:::config
            WWW["bin/www"]:::config
        end

        subgraph "Presentation Layer"
            direction TB
            ViewsShop["views/shop/"]:::presentation
            ViewsThor["views/thor/"]:::presentation
            ViewsLayout["views/layout.jade"]:::presentation
            ViewsPartials["views/partials/"]:::presentation
            ViewsError["views/error.jade"]:::presentation
        end

        subgraph "Static Asset Server"
            direction TB
            Public["public/"]:::presentation
            Resource["resource/"]:::presentation
        end

        subgraph "Middleware & Utils"
            direction TB
            MiddlewareThor["middlewares/thor.js"]:::crosscut
            UtilsConstant["utils/constant.js"]:::crosscut
            UtilsDate["utils/date.js"]:::crosscut
            UtilsString["utils/string.js"]:::crosscut
            UtilsUpload["utils/upload.js"]:::crosscut
        end

        subgraph "Application Layer"
            direction TB
            subgraph "Routes"
                direction TB
                RouteIndex["routes/index.js"]:::application
                RouteThor["routes/thor.js"]:::application
            end
            subgraph "Controllers"
                direction TB
                FEProduct["controllers/frontend/product.js"]:::application
                FEIndex["controllers/index.js"]:::application
                BEAuth["controllers/backend/auth.js"]:::application
                BEHome["controllers/backend/home.js"]:::application
                BECategory["controllers/backend/category.js"]:::application
                BEProduct["controllers/backend/product.js"]:::application
                BESell["controllers/backend/sell.js"]:::application
                ThorEntry["controllers/thor.js"]:::application
            end
        end

        subgraph "Data Layer"
            direction TB
            ModelsAuth["models/auth.js"]:::data
            ModelsCategory["models/category.js"]:::data
            ModelsProduct["models/product.js"]:::data
            ModelsSell["models/sell.js"]:::data
            ModelsUser["models/user.js"]:::data
            DBAdapter["database/db.js"]:::data
            DBFile["database/database.example.json"]:::data
        end
    end

    %% Client to Server
    ClientShop -->|"HTTP Request"| RouteIndex
    ClientShop -->|"HTTP Request"| RouteThor
    ClientAdmin -->|"HTTP Request /thor"| RouteThor

    %% Routes to Controllers
    RouteIndex -->|"calls"| FEIndex
    RouteIndex -->|"calls"| FEProduct
    RouteThor -->|"calls"| ThorEntry
    ThorEntry -->|"dispatch"/n frontend| BEHome
    ThorEntry -->|"dispatch"/n category| BECategory
    ThorEntry -->|"dispatch"/n product| BEProduct
    ThorEntry -->|"dispatch"/n sell| BESell
    ThorEntry -->|"dispatch"/n auth| BEAuth

    %% Middleware usage
    RouteThor -->|"uses"| MiddlewareThor
    RouteIndex -->|"uses"| UtilsUpload
    FEProduct -->|"uses"| UtilsString
    BEProduct -->|"uses"| UtilsDate

    %% Controllers to Models
    FEProduct -->|"CRUD"| ModelsProduct
    FEIndex -->|"read"| ModelsCategory
    BECategory -->|"CRUD"| ModelsCategory
    BEProduct -->|"CRUD"| ModelsProduct
    BESell -->|"CRUD"| ModelsSell
    BEAuth -->|"CRUD"| ModelsAuth
    BEAuth -->|"CRUD"| ModelsUser

    %% Models to DB
    ModelsAuth -->|"reads/writes"| DBAdapter
    ModelsCategory -->|"reads/writes"| DBAdapter
    ModelsProduct -->|"reads/writes"| DBAdapter
    ModelsSell -->|"reads/writes"| DBAdapter
    ModelsUser -->|"reads/writes"| DBAdapter
    DBAdapter -->|"stores JSON"| DBFile

    %% Presentation response
    FEIndex -->|"renders"| ViewsShop
    FEProduct -->|"renders"| ViewsShop
    BEHome -->|"renders"| ViewsThor
    BECategory -->|"renders"| ViewsThor
    BEProduct -->|"renders"| ViewsThor
    BESell -->|"renders"| ViewsThor
    BEAuth -->|"renders"| ViewsThor

    %% Static assets to Client
    Public -->|"serves"| ClientShop
    Public -->|"serves"| ClientAdmin
    Resource -->|"serves"| ClientShop

    %% Click Events
    click AppJS "https://github.com/josephng95/thorshop/blob/develop/app.js"
    click WWW "https://github.com/josephng95/thorshop/tree/develop/bin/www"
    click RouteIndex "https://github.com/josephng95/thorshop/blob/develop/routes/index.js"
    click RouteThor "https://github.com/josephng95/thorshop/blob/develop/routes/thor.js"
    click FEProduct "https://github.com/josephng95/thorshop/blob/develop/controllers/frontend/product.js"
    click FEIndex "https://github.com/josephng95/thorshop/blob/develop/controllers/index.js"
    click BEAuth "https://github.com/josephng95/thorshop/blob/develop/controllers/backend/auth.js"
    click BEHome "https://github.com/josephng95/thorshop/blob/develop/controllers/backend/home.js"
    click BECategory "https://github.com/josephng95/thorshop/blob/develop/controllers/backend/category.js"
    click BEProduct "https://github.com/josephng95/thorshop/blob/develop/controllers/backend/product.js"
    click BESell "https://github.com/josephng95/thorshop/blob/develop/controllers/backend/sell.js"
    click ThorEntry "https://github.com/josephng95/thorshop/blob/develop/controllers/thor.js"
    click ModelsAuth "https://github.com/josephng95/thorshop/blob/develop/models/auth.js"
    click ModelsCategory "https://github.com/josephng95/thorshop/blob/develop/models/category.js"
    click ModelsProduct "https://github.com/josephng95/thorshop/blob/develop/models/product.js"
    click ModelsSell "https://github.com/josephng95/thorshop/blob/develop/models/sell.js"
    click ModelsUser "https://github.com/josephng95/thorshop/blob/develop/models/user.js"
    click DBAdapter "https://github.com/josephng95/thorshop/blob/develop/database/db.js"
    click DBFile "https://github.com/josephng95/thorshop/blob/develop/database/database.example.json"
    click ViewsShop "https://github.com/josephng95/thorshop/tree/develop/views/shop/"
    click ViewsThor "https://github.com/josephng95/thorshop/tree/develop/views/thor/"
    click ViewsLayout "https://github.com/josephng95/thorshop/blob/develop/views/layout.jade"
    click ViewsPartials "https://github.com/josephng95/thorshop/tree/develop/views/partials/"
    click ViewsError "https://github.com/josephng95/thorshop/blob/develop/views/error.jade"
    click Public "https://github.com/josephng95/thorshop/tree/develop/public/"
    click Resource "https://github.com/josephng95/thorshop/tree/develop/resource/"
    click MiddlewareThor "https://github.com/josephng95/thorshop/blob/develop/middlewares/thor.js"
    click UtilsConstant "https://github.com/josephng95/thorshop/blob/develop/utils/constant.js"
    click UtilsDate "https://github.com/josephng95/thorshop/blob/develop/utils/date.js"
    click UtilsString "https://github.com/josephng95/thorshop/blob/develop/utils/string.js"
    click UtilsUpload "https://github.com/josephng95/thorshop/blob/develop/utils/upload.js"
    click Env "https://github.com/josephng95/thorshop/blob/develop/.env.example"
    classDef client fill:#f9f,stroke:#333,stroke-width:1px
    classDef presentation fill:#def,stroke:#036,stroke-width:1px
    classDef application fill:#dfd,stroke:#060,stroke-width:1px
    classDef data fill:#fed,stroke:#960,stroke-width:1px
    classDef crosscut fill:#ccc,stroke:#666,stroke-width:1px
    classDef config fill:#eef,stroke:#339,stroke-width:1px
```
