# pateljayy/urlshortner architecture

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

- Interactive diagram: https://gitdiagram.com/pateljayy/urlshortner
- Repository: https://github.com/pateljayy/urlshortner

## Overview

##### Main Components
- Frontend View Layer – Represented by the EJS templates found in the “views” directory (e.g., home.ejs, login.ejs, signup.ejs). These files are responsible for rendering the user interface.
- Backend API/Controller Layer – Consists of the controllers (e.g., controllers/url.js, controllers/user.js) which implement the business logic for URL shortening and user operations.
- Routing Layer – Found in the “routes” directory (e.g., routes/url.js, routes/user.js, routes/staticRouter.js) which maps HTTP endpoints to the appropriate controllers.
- Middleware – The “middlewares/auth.js” file ensures that routes requiring authentication are protected by verifying JWT tokens.
- Data Access Layer – The models (e.g., models/url.js, models/user.js) define the MongoDB schemas for storing URL data and user information.
- Authentication Service – Located in “service/auth.js” which handles JWT creation and validation for secure access.
- Configuration/Entry Point – The “index.js” file is the entry point of the application where the Express app is initialized, middlewares are set up, and routes are attached.
- External Systems – A MongoDB database instance (as per the README) and external authentication (JWT) means.

##### Lay Out Relationships and Interactions
- Client (Browser) interacts with the server through HTTP requests which hit the web server.
- The Express application in “index.js” directs the incoming requests via defined routes in the “routes” folder.
- Routes forward requests to appropriate controllers, which then might interact with the data models.
- The controllers use the “service/auth.js” for user authentication and authorization.
- The middleware “middlewares/auth.js” intercepts requests to verify user tokens before allowing access to protected routes.
- Model operations are performed against the MongoDB database using Mongoose models (in the “models” folder).
- Finally, the controller returns responses, possibly rendering dynamic views with EJS from the “views” folder.

##### Architectural Patterns & Design Principles
- MVC Pattern – The separation of concerns between models, views, and controllers is key.
- RESTful Routing – The “routes” directory organizes endpoints that likely follow RESTful standards.
- Middleware Usage – Implementation of cross-cutting concerns like authentication via “middlewares/auth.js”.
- Service Abstraction – The dedicated “service/auth.js” file shows abstraction of authentication logic away from controllers.

##### Relevant Technologies/Frameworks
- Node.js and Express – For the server and routing logic.
- EJS – As the view templating engine.
- MongoDB & Mongoose – For database and schema modeling.
- JWT – For secure user authentication.
- GitHub Actions (via the .github/workflows folder) – For CI/CD, ensuring code quality or automated testing.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Client Layer"
        A1["Client Browser"]:::client
    end

    %% Entry Point
    B1["Express Server"]:::entry
    A1 -->|"HTTP_Request"| B1

    %% Routing Layer
    subgraph "Routing Layer"
        R1["URL Route"]:::routes
        R2["User Route"]:::routes
        R3["Static Router"]:::routes
    end
    B1 --> R1
    B1 --> R2
    B1 --> R3

    %% Middleware for protected routes
    R1 -->|"RequiresAuth"| D1["Middleware"]:::middleware
    R2 -->|"RequiresAuth"| D1

    %% Controllers Layer (for dynamic requests)
    subgraph "Controllers Layer"
        Cn1["URL Controller"]:::controller
        Cn2["User Controller"]:::controller
    end
    D1 --> Cn1
    D1 --> Cn2

    %% Controllers invoke Authentication Service and Data Access
    Cn1 -->|"CallAuthService"| F1["Authentication Service"]:::service
    Cn2 --> F1
    Cn1 -->|"DataOp"| M1["URL Model"]:::model
    Cn2 -->|"DataOp"| M2["User Model"]:::model

    %% Models interact with MongoDB
    M1 -->|"DBOp"| H1[(MongoDB)]:::database
    M2 --> H1

    %% Controllers send dynamic responses to Views Layer
    Cn1 -->|"RenderDynamic"| I1["Views Layer"]:::view
    Cn2 --> I1

    %% Static Router sends static content directly to Views Layer
    R3 -->|"ServeStatic"| I1

    %% Views Layer with EJS templates
    subgraph "Views Layer"
        V1["Home"]:::view
        V2["Login"]:::view
        V3["Signup"]:::view
    end
    I1 --> V1
    I1 --> V2
    I1 --> V3

    %% Rendered views return responses to Client Browser
    V1 -->|"RenderHTML"| A1
    V2 --> A1
    V3 --> A1

    %% Click Events
    click B1 "https://github.com/pateljayy/urlshortner/blob/master/index.js"
    click R1 "https://github.com/pateljayy/urlshortner/blob/master/routes/url.js"
    click R2 "https://github.com/pateljayy/urlshortner/blob/master/routes/user.js"
    click R3 "https://github.com/pateljayy/urlshortner/blob/master/routes/staticRouter.js"
    click D1 "https://github.com/pateljayy/urlshortner/blob/master/middlewares/auth.js"
    click Cn1 "https://github.com/pateljayy/urlshortner/blob/master/controllers/url.js"
    click Cn2 "https://github.com/pateljayy/urlshortner/blob/master/controllers/user.js"
    click F1 "https://github.com/pateljayy/urlshortner/blob/master/service/auth.js"
    click M1 "https://github.com/pateljayy/urlshortner/blob/master/models/url.js"
    click M2 "https://github.com/pateljayy/urlshortner/blob/master/models/user.js"
    click V1 "https://github.com/pateljayy/urlshortner/blob/master/views/home.ejs"
    click V2 "https://github.com/pateljayy/urlshortner/blob/master/views/login.ejs"
    click V3 "https://github.com/pateljayy/urlshortner/blob/master/views/signup.ejs"

    %% Style definitions
    classDef client fill:#ffcc00,stroke:#333,stroke-width:2px;
    classDef entry fill:#ccffcc,stroke:#333,stroke-width:2px;
    classDef routes fill:#ccccff,stroke:#333,stroke-width:2px;
    classDef middleware fill:#e0e0e0,stroke:#333,stroke-width:2px;
    classDef controller fill:#ffcccc,stroke:#333,stroke-width:2px;
    classDef service fill:#f2d9e6,stroke:#333,stroke-width:2px;
    classDef model fill:#d9f2d9,stroke:#333,stroke-width:2px;
    classDef view fill:#fff2cc,stroke:#333,stroke-width:2px;
    classDef database fill:#cceeff,stroke:#333,stroke-width:2px;
```
