# businessfordevsav/linksqueeze architecture

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

- Interactive diagram: https://gitdiagram.com/businessfordevsav/linksqueeze
- Repository: https://github.com/businessfordevsav/linksqueeze

## Overview

##### Project type and purpose
- LinkSqueeze is a full-stack web application: a URL shortener service built on Node.js/Express with a server-rendered EJS frontend and a MongoDB backend.
- Its purpose is to generate unique short URLs, redirect users, and track analytics (visits, IP, browser, country).

##### File structure
- Top-level:
- src/ – the Express app (app.js, controllers/, routes/, middleware/, models/, services/, views/)
- public/ – static assets (CSS, JS, images)
- connection.js – MongoDB connection setup
- swagger.json – API specification
- tests/ – unit, integration, E2E tests with setup scripts
- MVC pattern:
- Models (src/models) interacting with MongoDB via Mongoose
- Controllers (src/controllers) handling business logic, calling services
- Views (src/views) EJS templates for UI
- Routes (src/routes) binding endpoints to controllers
- Cross-cutting concerns in middleware (securityHeaders, requestLogger, dbSanitize, xssProtection, errorLogger).
- Service layer (emailService, qrCodeService, s3Service) encapsulates external integrations.

##### Supplementary insights from README
- Technology stack: Node.js, Express, Mongoose, NanoID, GeoIP-lite, express-useragent, EJS.
- API endpoints: POST /url, GET /:shortId, GET /url/analytics, GET /shrtn (web interface).
- External services: MongoDB, optionally AWS S3 (s3proxy route), GeoIP-lite for geolocation, email for notifications.

a. Main components:
- Client (Web browser)
- Express Server
- MongoDB Database
- External libraries/services: GeoIP-lite, NanoID, express-useragent
- Optional AWS S3 (via s3proxy)
- Email service
b. Relationships & data flow:
1. Browser → HTTP Requests → Express Server
2. Server applies Middleware (security, logging, sanitization)
3. Routes → Controllers → Services → Models
4. Models ↔ MongoDB
5. Services ↔ External libs (GeoIP-lite, NanoID) or AWS S3/email
6. Controllers → Views → HTML → Browser
c. Architectural patterns & principles:
- MVC separation
- Layered architecture: routing → middleware → controllers → services → models
- Separation of static assets (public) vs dynamic content (EJS)
- Single‐responsibility in middleware for cross-cutting concerns
d. Key technologies & libraries:
- Express (HTTP server, routing, middleware)
- Mongoose (ODM for MongoDB)
- EJS (templating)
- NanoID (ID generation)
- GeoIP-lite & express-useragent (analytics)
- AWS SDK (S3 proxy)
- Jest (testing)

##### Detailed component breakdown
- Controllers & their routes: Auth, URL, Dashboard, S3Proxy.
- Service calls: qrCodeService (NanoID), emailService, s3Service (AWS S3), analytics (GeoIP-lite + express-useragent).
- Data layer: Mongoose models (User, URL, RequestLog) connecting to MongoDB.
- Frontend view layer: EJS templates under src/views and static files under public/.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Zone
    subgraph "Frontend" 
        direction TB
        Client["Web Browser"]:::frontend
        StaticAssets["Static Assets\n(public/)"]:::static
        Views["EJS Templates\n(src/views)"]:::frontend
    end

    %% Backend Zone
    subgraph "Backend"
        direction TB

        App["Express App\n(src/app.js)"]:::backend

        subgraph "Middleware Pipeline"
            direction TB
            SecHeaders["securityHeaders"]:::middleware
            ReqLogger["requestLogger"]:::middleware
            DBSanitize["dbSanitize"]:::middleware
            XSSProtect["xssProtection"]:::middleware
            ErrLogger["errorLogger"]:::middleware
            Validators["validators"]:::middleware
        end

        subgraph "Routes"
            direction TB
            AuthRoutes["Auth Routes\n(src/routes/auth.js)"]:::routes
            URLRoutes["URL Routes\n(src/routes/url.js)"]:::routes
            DashRoutes["Dashboard Routes\n(src/routes/dashboard.js)"]:::routes
            S3Route["S3 Proxy Route\n(src/routes/s3proxy.js)"]:::routes
        end

        subgraph "Controllers"
            direction TB
            AuthCtrl["AuthController\n(src/controllers/auth.js)"]:::controller
            URLCtrl["URLController\n(src/controllers/url.js)"]:::controller
            DashCtrl["DashboardController\n(src/controllers/dashboard.js)"]:::controller
            S3Ctrl["S3Proxy Controller\n(src/routes/s3proxy.js)"]:::controller
        end

        subgraph "Services"
            direction TB
            EmailSvc["Email Service\n(src/services/emailService.js)"]:::service
            QRCodeSvc["QR Code Service\n(src/services/qrCodeService.js)"]:::service
            S3Svc["S3 Service\n(src/services/s3Service.js)"]:::service
            GeoIP["GeoIP-lite\n(express-useragent)"]:::external
            NanoID["NanoID"]:::external
            EmailExt["Email Service"]:::external
        end

        subgraph "Models"
            direction TB
            UserModel["User Model\n(src/models/user.js)"]:::model
            URLModel["URL Model\n(src/models/url.js)"]:::model
            ReqLogModel["RequestLog Model\n(src/models/requestLog.js)"]:::model
        end

        DB["MongoDB Database\n(connection.js)"]:::database
    end

    %% Flows
    Client -->|HTTP Request| App
    App --> SecHeaders
    SecHeaders --> ReqLogger
    ReqLogger --> DBSanitize
    DBSanitize --> XSSProtect
    XSSProtect --> ErrLogger
    ErrLogger --> Validators
    Validators --> AuthRoutes
    Validators --> URLRoutes
    Validators --> DashRoutes
    Validators --> S3Route

    AuthRoutes --> AuthCtrl
    URLRoutes --> URLCtrl
    DashRoutes --> DashCtrl
    S3Route --> S3Ctrl

    AuthCtrl --> EmailSvc
    URLCtrl --> QRCodeSvc
    URLCtrl --> EmailSvc
    URLCtrl --> GeoIP
    URLCtrl --> NanoID
    URLCtrl --> URLModel
    URLCtrl --> ReqLogModel

    DashCtrl --> ReqLogModel
    S3Ctrl --> S3Svc

    EmailSvc --> EmailExt
    QRCodeSvc --> NanoID
    S3Svc --> S3Svc

    UserModel --> DB
    URLModel --> DB
    ReqLogModel --> DB

    App --> StaticAssets
    App --> Views
    Views --> Client

    %% Click Events
    click App "https://github.com/businessfordevsav/linksqueeze/blob/main/src/app.js"
    click DB "https://github.com/businessfordevsav/linksqueeze/blob/main/connection.js"
    click UserModel "https://github.com/businessfordevsav/linksqueeze/blob/main/src/models/user.js"
    click URLModel "https://github.com/businessfordevsav/linksqueeze/blob/main/src/models/url.js"
    click ReqLogModel "https://github.com/businessfordevsav/linksqueeze/blob/main/src/models/requestLog.js"
    click AuthCtrl "https://github.com/businessfordevsav/linksqueeze/blob/main/src/controllers/auth.js"
    click URLCtrl "https://github.com/businessfordevsav/linksqueeze/blob/main/src/controllers/url.js"
    click DashCtrl "https://github.com/businessfordevsav/linksqueeze/blob/main/src/controllers/dashboard.js"
    click S3Ctrl "https://github.com/businessfordevsav/linksqueeze/blob/main/src/routes/s3proxy.js"
    click AuthRoutes "https://github.com/businessfordevsav/linksqueeze/blob/main/src/routes/auth.js"
    click URLRoutes "https://github.com/businessfordevsav/linksqueeze/blob/main/src/routes/url.js"
    click DashRoutes "https://github.com/businessfordevsav/linksqueeze/blob/main/src/routes/dashboard.js"
    click S3Route "https://github.com/businessfordevsav/linksqueeze/blob/main/src/routes/s3proxy.js"
    click SecHeaders "https://github.com/businessfordevsav/linksqueeze/blob/main/src/middleware/securityHeaders.js"
    click ReqLogger "https://github.com/businessfordevsav/linksqueeze/blob/main/src/middleware/requestLogger.js"
    click DBSanitize "https://github.com/businessfordevsav/linksqueeze/blob/main/src/middleware/dbSanitize.js"
    click XSSProtect "https://github.com/businessfordevsav/linksqueeze/blob/main/src/middleware/xssProtection.js"
    click ErrLogger "https://github.com/businessfordevsav/linksqueeze/blob/main/src/middleware/errorLogger.js"
    click Validators "https://github.com/businessfordevsav/linksqueeze/blob/main/src/middleware/validators.js"
    click EmailSvc "https://github.com/businessfordevsav/linksqueeze/blob/main/src/services/emailService.js"
    click QRCodeSvc "https://github.com/businessfordevsav/linksqueeze/blob/main/src/services/qrCodeService.js"
    click S3Svc "https://github.com/businessfordevsav/linksqueeze/blob/main/src/services/s3Service.js"

    %% Styles
    classDef frontend fill:#f9f,stroke:#333,stroke-width:1px
    classDef static fill:#ccc,stroke:#333,stroke-width:1px
    classDef backend fill:#bbf,stroke:#333,stroke-width:1px
    classDef middleware fill:#ace,stroke:#333,stroke-width:1px
    classDef routes fill:#aef,stroke:#333,stroke-width:1px
    classDef controller fill:#8cf,stroke:#333,stroke-width:1px
    classDef service fill:#fae,stroke:#333,stroke-width:1px
    classDef model fill:#cfc,stroke:#333,stroke-width:1px
    classDef database fill:#2f2,stroke:#333,stroke-width:1px,stroke-dasharray: 5 5
    classDef external fill:#ffeb3b,stroke:#333,stroke-width:1px
```
