# ichsanna/url-shortener architecture

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

- Interactive diagram: https://gitdiagram.com/ichsanna/url-shortener
- Repository: https://github.com/ichsanna/url-shortener

## Overview

##### Main Components

A. Client Layer:
- Browser/Frontend (HTML/CSS/JavaScript)
- Static Assets (public/assets)
- View Templates (EJS)

B. Application Layer:
- Web Server (Express.js)
- Authentication Middleware
- Controllers
- Routers

C. Data Layer:
- MongoDB Database
- Models (User and Link)

##### Component Relationships

**[Client Browser] ←→ [Web Server (Express.js)]**
- ↑               ↑

- [Static Assets]  [Controllers]
- [EJS Views]         ↑

- [Models] ←→ [MongoDB]

##### Detailed Component Breakdown

A. Routes Layer (First Entry Point):
- webRouter (Web Interface)
- userRouter (User Management)
- linkRouter (URL Shortening Operations)

B. Middleware Layer:
- Authentication middleware
- It intercepting requests between routes and controllers

C. Controllers Layer:
- userController (User Operations)
- linkController (Link Operations)

D. Models Layer:
- userModel (User Schema)
- linkModel (Link Schema)

##### Data Flow Representation

- HTTP requests from client to server
- Database queries between models and MongoDB
- Authentication flow through middleware
- Response flow back to client

##### Additional Elements

Supporting components:
- Configuration files (configs/)
- Environment variables (.env)
- Static file serving (public/)
- View engine (EJS)

**[Client Layer]**
- ↓↑
**[Routing Layer]**
- ↓↑
**[Middleware Layer]**
- ↓↑
**[Controller Layer]**
- ↓↑
**[Model Layer]**
- ↓↑
**[Database Layer]**

Key Considerations:

##### MVC pattern clearly
- Models (models/)
- Views (public/views/)
- Controllers (controllers/)

##### Security features
- JWT authentication
- Middleware protection

##### URL shortening flow
- URL submission
- Shortening process
- Storage
- Retrieval

##### External dependencies
- MongoDB connection
- Environment configurations

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Client
        Browser["Browser/Client"]
        StaticAssets["Static Assets"]:::static
        Views["EJS Views"]:::views
    end

    subgraph Server
        Express["Express.js Server"]:::server
        
        subgraph Routes
            WebRouter["Web Router"]:::route
            UserRouter["User Router"]:::route
            LinkRouter["Link Router"]:::route
        end
        
        subgraph Middleware
            AuthMiddleware["Authentication"]:::security
        end
        
        subgraph Controllers
            UserController["User Controller"]:::controller
            LinkController["Link Controller"]:::controller
        end
        
        subgraph Models
            UserModel["User Model"]:::model
            LinkModel["Link Model"]:::model
        end
        
        subgraph Config
            ResponseFormat["Response Format"]:::config
            ResponseMessages["Response Messages"]:::config
        end
    end
    
    subgraph Database
        MongoDB[(MongoDB)]:::database
    end

    %% Connections
    Browser <--> Express
    Browser --> StaticAssets
    Browser --> Views
    
    Express --> WebRouter
    Express --> UserRouter
    Express --> LinkRouter
    
    WebRouter --> AuthMiddleware
    UserRouter --> AuthMiddleware
    LinkRouter --> AuthMiddleware
    
    AuthMiddleware --> UserController
    AuthMiddleware --> LinkController
    
    UserController --> UserModel
    LinkController --> LinkModel
    
    UserModel --> MongoDB
    LinkModel --> MongoDB
    
    UserController --> ResponseFormat
    LinkController --> ResponseFormat
    UserController --> ResponseMessages
    LinkController --> ResponseMessages

    %% Click Events
    click WebRouter "https://github.com/ichsanna/url-shortener/blob/master/routes/webRouter.js"
    click UserRouter "https://github.com/ichsanna/url-shortener/blob/master/routes/userRouter.js"
    click LinkRouter "https://github.com/ichsanna/url-shortener/blob/master/routes/linkRouter.js"
    click UserController "https://github.com/ichsanna/url-shortener/blob/master/controllers/userController.js"
    click LinkController "https://github.com/ichsanna/url-shortener/blob/master/controllers/linkController.js"
    click UserModel "https://github.com/ichsanna/url-shortener/blob/master/models/userModel.js"
    click LinkModel "https://github.com/ichsanna/url-shortener/blob/master/models/linkModel.js"
    click Views "https://github.com/ichsanna/url-shortener/tree/master/public/views"
    click StaticAssets "https://github.com/ichsanna/url-shortener/tree/master/public/assets"
    click AuthMiddleware "https://github.com/ichsanna/url-shortener/blob/master/middlewares/auth.js"
    click ResponseFormat "https://github.com/ichsanna/url-shortener/blob/master/configs/responseFormat.js"
    click ResponseMessages "https://github.com/ichsanna/url-shortener/blob/master/configs/responseMessages.js"

    %% Styles
    classDef server fill:#3498db
    classDef route fill:#2ecc71
    classDef security fill:#e74c3c
    classDef controller fill:#9b59b6
    classDef model fill:#f1c40f
    classDef database fill:#34495e
    classDef static fill:#95a5a6
    classDef views fill:#1abc9c
    classDef config fill:#e67e22

    %% Legend
    subgraph Legend
        ServerComponent["Server Component"]:::server
        RouteComponent["Route Component"]:::route
        SecurityComponent["Security Component"]:::security
        ControllerComponent["Controller Component"]:::controller
        ModelComponent["Model Component"]:::model
        DatabaseComponent["Database Component"]:::database
        StaticComponent["Static Component"]:::static
        ViewComponent["View Component"]:::views
        ConfigComponent["Config Component"]:::config
    end
```
