# prasadborse02/short-url architecture

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

- Interactive diagram: https://gitdiagram.com/prasadborse02/short-url
- Repository: https://github.com/prasadborse02/short-url

## Overview

##### Main Components
- Frontend: Built with React, Vite, and supporting files for assets, components (such as Navbar, Footer, Analytics, URL Shortener), and configuration (.env).
- Backend: A Node.js/Express server organized into subfolders (config, controllers, errors, middlewares, models, routes, services, and utils). Key functionalities include:
- URL shortening and redirection
- Click tracking and analytics
- Request logging and error handling
- Database: MySQL used as persistent storage via Sequelize ORM.
- Caching: Redis is used for fast URL resolution caching.
- Asynchronous Processing: Apache Kafka handles click event processing in an event-driven fashion.
- Infrastructure: Although parts of the infrastructure (like Redis, MySQL, Kafka, and Zookeeper) are set up via an external Docker Compose project, it is essential to represent them as part of the overall ecosystem.

##### Component Relationships and Interactions
- The Backend processes incoming requests and routes them via controllers. Show how the request passes from the API layer (routes) to the controllers and then to the services.
- Cross-cutting concerns such as logging (using Winston) and utilities (for code generation, user-agent parsing) as part of the backend’s internal modules.

##### Architectural Patterns and Design Principles
- Emphasis on a microservices-style or modular architecture within the backend (even if it’s implemented within a single Node.js project) with clear separation of concerns (controllers, services, models, etc.).
- Use of a cache-first approach and event-driven processing (with Redis and Kafka respectively) for performance optimizations.
- Separation between the frontend (UI layer) and backend (API/business logic layer).

##### Relevant Technologies
- Frontend: React, Vite, React Router DOM, TypeScript (if applicable), along with environment variables (.env).
- Backend: Node.js, Express, Sequelize (for MySQL), Redis (caching), Kafka.js (for Kafka interface), Winston (logging), GeoIP-lite (for location detection).
- Infrastructure: Docker Compose for setting up supporting systems (MySQL, Redis, Kafka, Zookeeper).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Frontend Layer"
        FrontendUI["React/Vite Frontend"]:::ui
    end

    %% Backend Layer
    subgraph "Backend Layer"
        BackendAPI["Express API Server"]:::backend

        subgraph "Configurations"
            MysqlConfig["MySQL Config"]:::backendConfig
            RedisConfig["Redis Config"]:::backendConfig
            KafkaConfig["Kafka Config"]:::backendConfig
            LoggerConfig["Logger Config"]:::backendConfig
        end

        subgraph "Controllers & Services"
            UrlController["URL Controller"]:::backend
            UrlService["URL Service"]:::backend
            ClickProcessor["Click Event Processor"]:::backend
        end

        subgraph "Models & Middleware"
            UrlModel["URL Model"]:::backend
            ClickModel["Click Model"]:::backend
            RequestLogger["Request Logger Middleware"]:::backend
            CodeGenerator["Code Generator"]:::utility
            UserAgentHelper["User Agent Helper"]:::utility
        end
    end

    %% Infrastructure Layer
    subgraph "Infrastructure"
        MySQLDB["MySQL Database"]:::database
        RedisDB["Redis Cache"]:::infra
        KafkaBus["Kafka Event Bus"]:::infra
        Zookeeper["Zookeeper Coordination"]:::infra
    end

    %% Data Flow
    FrontendUI -->|"POST/GET_API"| BackendAPI
    BackendAPI -->|"routes"| UrlController
    BackendAPI -->|"logs_request"| RequestLogger
    UrlController -->|"calls"| UrlService
    UrlService -->|"persists"| UrlModel
    UrlService -->|"stores_data"| MySQLDB
    UrlService -->|"queries_cache"| RedisDB
    UrlService -->|"generates_code"| CodeGenerator
    UrlService -->|"parses_agent"| UserAgentHelper
    ClickProcessor -->|"emits_event"| KafkaBus
    KafkaBus -->|"processes"| ClickModel

    %% Click Events
    click FrontendUI "https://github.com/prasadborse02/short-url/tree/main/frontend/"
    click BackendAPI "https://github.com/prasadborse02/short-url/tree/main/backend/"
    click MysqlConfig "https://github.com/prasadborse02/short-url/blob/main/backend/config/mysql.js"
    click RedisConfig "https://github.com/prasadborse02/short-url/blob/main/backend/config/redis.js"
    click KafkaConfig "https://github.com/prasadborse02/short-url/blob/main/backend/config/kafka.js"
    click LoggerConfig "https://github.com/prasadborse02/short-url/blob/main/backend/config/logger.js"
    click UrlController "https://github.com/prasadborse02/short-url/blob/main/backend/controllers/urlController.js"
    click UrlService "https://github.com/prasadborse02/short-url/blob/main/backend/services/urlService.js"
    click ClickProcessor "https://github.com/prasadborse02/short-url/blob/main/backend/services/clickEventProcessor.js"
    click UrlModel "https://github.com/prasadborse02/short-url/blob/main/backend/models/url.js"
    click ClickModel "https://github.com/prasadborse02/short-url/blob/main/backend/models/click.js"
    click RequestLogger "https://github.com/prasadborse02/short-url/blob/main/backend/middlewares/requestLogger.js"
    click CodeGenerator "https://github.com/prasadborse02/short-url/blob/main/backend/utils/codeGenerator.js"
    click UserAgentHelper "https://github.com/prasadborse02/short-url/blob/main/backend/utils/userAgentHelper.js"

    %% Styles
    classDef ui fill:#ADD8E6,stroke:#0000FF,stroke-width:2px;
    classDef backend fill:#90EE90,stroke:#008000,stroke-width:2px;
    classDef backendConfig fill:#98FB98,stroke:#006400,stroke-width:2px;
    classDef database fill:#FFC0CB,stroke:#FF1493,stroke-width:2px;
    classDef infra fill:#FFE4B5,stroke:#FF8C00,stroke-width:2px;
    classDef utility fill:#D3D3D3,stroke:#696969,stroke-width:2px;
```
