# thapatechnical/urlshortener architecture

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

- Interactive diagram: https://gitdiagram.com/thapatechnical/urlshortener
- Repository: https://github.com/thapatechnical/urlshortener

## Overview

##### Main Components
a. Client (Browser) – renders EJS views, interacts via HTTP/HTTPS
b. Web Server / Application Layer – Node.js + Express.js
c. Business Logic Layer – Controllers → Services → Validators → Models
d. Persistence Layer –
- MongoDB via Mongoose (e.g., user profiles)
- MySQL via Prisma & Drizzle ORM (e.g., link records, migrations)
e. External Services –
- OAuth Providers (Google, GitHub)
- Email Service (Nodemailer + MJML templates)
- File Storage (via Multer for profile images)
f. Static Assets – CSS, images, served from /public

##### Relationships & Data Flow
- Client sends HTTP requests (GET/POST) → Express Router (routes/*.js)
- Request passes through global and route‑specific middleware (e.g., verify-auth‑middleware.js)
- Router invokes appropriate Controller (controllers/*.js)
- Controller calls Validators (validators/*.js) to enforce input rules
- Controller delegates to Services (services/*.js) for business logic
- Service interacts with Models (models/*.js) using ORM APIs (Mongoose, Prisma, Drizzle)
- Model operations translate to queries on MongoDB or MySQL
- Some Services invoke lib utilities (e.g., get-html-from-mjml-template.js, nodemailer.js) to send email
- Responses flow back up to Controller → rendered via EJS views (views/**/*.ejs) → HTTP response to client

##### Architectural Patterns & Design Principles
- Layered (MVC‑like) architecture: Separation of concerns between routing, controller logic, services, and data access
- Modular design: Each feature (auth, shortener) lives in its own route/controller/service/validator directory
- Multi‑ORM strategy: Use of Mongoose for MongoDB, Prisma & Drizzle ORM for MySQL, plus Drizzle migrations for schema versioning
- Security: JWT & OAuth, request validators, auth middleware
- Templating: Server‑side rendering with EJS + partials
- Asynchronous, event‑driven I/O (Node.js)

##### Detailed Component Breakdown
a. Client
- Index page (views/index.ejs)
- Auth pages under views/auth
- Shortener UI (views/edit-shortlink.ejs, views/report.ejs)
b. Express Server (app.js) & Config (config/*.js)
c. Routes (routes/*.js) – entry points
d. Middleware (middlewares/verify-auth-middleware.js) – guard protected routes
e. Controllers → Services → Models → DB
f. Drizzle Migrations (drizzle/migration/) – manage MySQL schema versions
g. Email subsystem (emails/*.mjml + lib/send-email.js)
h. Validators (validators/*.js) – enforce schema on incoming requests

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Side
    subgraph "Client Side"
        Browser["Browser (EJS Views)"]:::client
        Static["Static Assets"]:::static
    end
    Browser -->|HTTP GET/POST| Server["Express Server (app.js)"]:::server
    Browser -->|requests| Static

    click Browser "https://github.com/thapatechnical/urlshortener/tree/main/views/"
    click Static "https://github.com/thapatechnical/urlshortener/tree/main/public/"
    click Server "https://github.com/thapatechnical/urlshortener/blob/main/app.js"

    %% Configuration
    subgraph "Configuration" 
        DBConfig["DB Config (Mongoose)"]:::config
        Constants["Constants"]:::config
    end
    Server --> DBConfig
    Server --> Constants

    click DBConfig "https://github.com/thapatechnical/urlshortener/blob/main/config/db.js"
    click Constants "https://github.com/thapatechnical/urlshortener/blob/main/config/constants.js"

    %% Routes
    subgraph "Routes"
        AuthRoutes["Auth Routes"]:::routes
        ShortRoutes["Shortener Routes"]:::routes
    end
    Server --> AuthRoutes
    Server --> ShortRoutes

    click AuthRoutes "https://github.com/thapatechnical/urlshortener/blob/main/routes/auth.routes.js"
    click ShortRoutes "https://github.com/thapatechnical/urlshortener/blob/main/routes/shortener.routes.js"

    %% Middleware
    AuthRoutes --> Middleware["verify-auth-middleware"]:::middleware
    ShortRoutes --> Middleware
    click Middleware "https://github.com/thapatechnical/urlshortener/blob/main/middlewares/verify-auth-middleware.js"

    %% Controllers & Validators
    subgraph "Controllers & Validators"
        AuthController["Auth Controller"]:::controller
        ShortController["Shortener Controller"]:::controller
        AuthValidator["Auth Validator"]:::validator
        ShortValidator["Shortener Validator"]:::validator
    end
    AuthRoutes --> AuthController
    ShortRoutes --> ShortController
    AuthController --> AuthValidator
    ShortController --> ShortValidator

    click AuthController "https://github.com/thapatechnical/urlshortener/blob/main/controllers/auth.controller.js"
    click ShortController "https://github.com/thapatechnical/urlshortener/blob/main/controllers/postshortener.controller.js"
    click AuthValidator "https://github.com/thapatechnical/urlshortener/blob/main/validators/auth-validator.js"
    click ShortValidator "https://github.com/thapatechnical/urlshortener/blob/main/validators/shortener-validator.js"

    %% Services
    subgraph "Services"
        AuthService["Auth Services"]:::service
        ShortService["Shortener Services"]:::service
    end
    AuthController --> AuthService
    ShortController --> ShortService

    click AuthService "https://github.com/thapatechnical/urlshortener/blob/main/services/auth.services.js"
    click ShortService "https://github.com/thapatechnical/urlshortener/blob/main/services/shortener.services.js"

    %% Persistence Layer
    subgraph "Persistence Layer"
        MongoModel["Shortener Model (Mongoose)"]:::db
        MySQLSchema["Drizzle Schema"]:::db
        Migrations["Drizzle Migrations"]:::db
        DrizzleConfig["Drizzle Config"]:::db
        DataFixtures["Data Fixtures"]:::db
    end
    AuthService --> MongoModel
    ShortService --> MongoModel
    ShortService --> MySQLSchema
    MySQLSchema --> Migrations
    DrizzleConfig --> Migrations
    Migrations --> DataFixtures

    click MongoModel "https://github.com/thapatechnical/urlshortener/blob/main/models/shortener.model.js"
    click MySQLSchema "https://github.com/thapatechnical/urlshortener/blob/main/drizzle/schema.js"
    click Migrations "https://github.com/thapatechnical/urlshortener/tree/main/drizzle/migration/"
    click DrizzleConfig "https://github.com/thapatechnical/urlshortener/blob/main/drizzle.config.js"
    click DataFixtures "https://github.com/thapatechnical/urlshortener/blob/main/data/links.json"

    %% Email Subsystem
    subgraph "Email Subsystem"
        MJML["MJML Templates"]:::external
        HTMLConv["get-html-from-mjml-template.js"]:::lib
        NodemailerLib["nodemailer.js"]:::lib
        SendEmail["send-email.js"]:::lib
    end
    AuthService -->|send email| MJML
    MJML --> HTMLConv
    HTMLConv --> NodemailerLib
    NodemailerLib --> SendEmail

    click MJML "https://github.com/thapatechnical/urlshortener/tree/main/emails/"
    click HTMLConv "https://github.com/thapatechnical/urlshortener/blob/main/lib/get-html-from-mjml-template.js"
    click NodemailerLib "https://github.com/thapatechnical/urlshortener/blob/main/lib/nodemailer.js"
    click SendEmail "https://github.com/thapatechnical/urlshortener/blob/main/lib/send-email.js"

    %% External Services
    subgraph "External Services"
        OAuth["OAuth Providers"]:::external
        FileStorage["File Storage (Multer)"]:::external
    end
    AuthService -->|OAuth| OAuth
    AuthService -->|profile images| FileStorage

    %% Databases
    subgraph "Databases"
        MongoDB["MongoDB"]:::database
        MySQL["MySQL"]:::database
    end
    MongoModel -->|Mongoose ORM| MongoDB
    MySQLSchema -->|Drizzle ORM| MySQL

    %% Legend via Styles
    classDef client fill:#D6EAF8,stroke:#3498DB
    classDef static fill:#EBF5FB,stroke:#5DADE2
    classDef server fill:#D5F5E3,stroke:#27AE60
    classDef config fill:#FCF3CF,stroke:#F1C40F
    classDef routes fill:#FADBD8,stroke:#E74C3C
    classDef middleware fill:#F5B7B1,stroke:#C0392B
    classDef controller fill:#E8DAEF,stroke:#8E44AD
    classDef validator fill:#EAF2F8,stroke:#5499C7
    classDef service fill:#FDEDEC,stroke:#E74C3C
    classDef db fill:#D2B4DE,stroke:#6C3483
    classDef lib fill:#F0B27A,stroke:#D35400
    classDef external fill:#F9E79F,stroke:#B7950B
    classDef database fill:#ABEBC6,stroke:#1D8348
```
