# akhileshthykkat/url-shortener architecture

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

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

## Overview

##### Main Components

A. Client Layer
- Web Browsers/API Clients
- Interaction with the API endpoints

B. Application Layer (Node.js + Express)
- Authentication Service
- URL Shortening Service
- Analytics Service
- Rate Limiting Middleware

C. Caching Layer
- Redis Cache (Upstash)
- Caching of frequently accessed URLs

D. Database Layer
- PostgreSQL Database (Neon)
- Different data models (URLs, Users, Analytics)

##### Component Relationships

**[Client Layer]**
- ↓ ↑
**[Load Balancer/API Gateway]**
- ↓ ↑
**[Application Layer]**

- ↓
- [Rate Limiter]
- ↓
- [Auth Middleware]
- ↓
**[Services Layer]**

- ↓    ↓    ↓
**[URL] [User] [Analytics]**
- /
- /
- ↓  ↓  ↓
**[Redis Cache]**
- ↓  ↑
**[PostgreSQL]**

##### Detailed Component Breakdown

A. API Layer (from src/routers):
- auth.js → Authentication routes
- shorten.js → URL shortening routes
- analytics.js → Analytics routes

B. Services Layer (from src/services):
- url.js → URL management
- user.js → User management
- clickEvent.js → Click tracking
- dailyStat.js → Daily statistics
- deviceStat.js → Device analytics
- osStat.js → Operating system analytics

C. Data Models (from src/models):
- URL
- User
- ClickEvent
- DailyStat
- DeviceStat
- OsStat

##### Additional Elements

A. Security Components:
- Google OAuth2 integration
- JWT token validation
- Rate limiting

B. Infrastructure Components:
- Docker container
- Render.com hosting
- Upstash Redis
- Neon PostgreSQL

##### The scalability points (where load balancing can be implemented)

This will provide a clear understanding of both the current architecture and potential scaling points for future growth.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client["Client Layer"]
        Browser["Web Browsers"]:::client
        APIClient["API Clients"]:::client
    end

    subgraph Gateway["API Gateway Layer"]
        LB["Load Balancer"]:::gateway
        RateLimit["Rate Limiter"]:::middleware
        click RateLimit "https://github.com/AkhileshThykkat/URL-Shortener/blob/main/src/middlewares/rateLimiter.js"
        Auth["Auth Middleware"]:::middleware
        click Auth "https://github.com/AkhileshThykkat/URL-Shortener/blob/main/src/middlewares/checkToken.js"
    end

    subgraph AppLayer["Application Layer"]
        Express["Express App"]:::app
        click Express "https://github.com/AkhileshThykkat/URL-Shortener/blob/main/src/index.js"
        
        subgraph Routes["API Routes"]
            AuthRoutes["Authentication Routes"]:::route
            click AuthRoutes "https://github.com/AkhileshThykkat/URL-Shortener/blob/main/src/routers/auth.js"
            URLRoutes["URL Routes"]:::route
            click URLRoutes "https://github.com/AkhileshThykkat/URL-Shortener/blob/main/src/routers/shorten.js"
            AnalyticsRoutes["Analytics Routes"]:::route
            click AnalyticsRoutes "https://github.com/AkhileshThykkat/URL-Shortener/blob/main/src/routers/analytics.js"
        end

        subgraph Services["Service Layer"]
            AuthService["Auth Service"]:::service
            click AuthService "https://github.com/AkhileshThykkat/URL-Shortener/blob/main/src/services/user.js"
            URLService["URL Service"]:::service
            click URLService "https://github.com/AkhileshThykkat/URL-Shortener/blob/main/src/services/url.js"
            AnalyticsService["Analytics Service"]:::service
            click AnalyticsService "https://github.com/AkhileshThykkat/URL-Shortener/blob/main/src/services/clickEvent.js"
        end
    end

    subgraph Cache["Caching Layer"]
        Redis["Redis Cache<br/>(Upstash)"]:::cache
        click Redis "https://github.com/AkhileshThykkat/URL-Shortener/blob/main/src/utils/redisCache.js"
    end

    subgraph DB["Database Layer"]
        Postgres["PostgreSQL<br/>(Neon)"]:::database
        click Postgres "https://github.com/AkhileshThykkat/URL-Shortener/blob/main/src/config/db.js"
        
        subgraph Models["Data Models"]
            URLModel["URL Model"]:::model
            click URLModel "https://github.com/AkhileshThykkat/URL-Shortener/blob/main/src/models/URL.js"
            UserModel["User Model"]:::model
            click UserModel "https://github.com/AkhileshThykkat/URL-Shortener/blob/main/src/models/User.js"
            ClickModel["Click Event Model"]:::model
            click ClickModel "https://github.com/AkhileshThykkat/URL-Shortener/blob/main/src/models/ClickEvent.js"
            StatModel["Statistics Models"]:::model
            click StatModel "https://github.com/AkhileshThykkat/URL-Shortener/blob/main/src/models/DailyStat.js"
        end
    end

    %% Connections
    Browser --> LB
    APIClient --> LB
    LB --> RateLimit
    RateLimit --> Auth
    Auth --> Express
    Express --> Routes
    AuthRoutes --> AuthService
    URLRoutes --> URLService
    AnalyticsRoutes --> AnalyticsService
    AuthService --> Redis
    URLService --> Redis
    AnalyticsService --> Redis
    Redis --> Postgres
    AuthService --> Models
    URLService --> Models
    AnalyticsService --> Models

    %% Styles
    classDef client fill:#2374ab
    classDef gateway fill:#57a773
    classDef middleware fill:#ffd151
    classDef app fill:#41729f
    classDef route fill:#5885af
    classDef service fill:#274472
    classDef cache fill:#9b5de5
    classDef database fill:#ef476f
    classDef model fill:#ff6b6b

    %% Legend
    subgraph Legend
        Client1["Client"]:::client
        Gateway1["Gateway"]:::gateway
        Middleware1["Middleware"]:::middleware
        App1["Application"]:::app
        Route1["Route"]:::route
        Service1["Service"]:::service
        Cache1["Cache"]:::cache
        Database1["Database"]:::database
        Model1["Model"]:::model
    end
```
