# tnfy-link/backend architecture

> Architecture diagram of the GitHub repository tnfy-link/backend, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tnfy-link/backend
- Repository: https://github.com/tnfy-link/backend

## Overview

##### Main Components

a) External Interface Layer:
- HTTP Server (Fiber Framework)
- API Endpoints (/api/v1/links/*)
- Rate Limiter Component

b) Core Application Layer:
- Links Module
- Stats Module
- ID Generator Module
- Configuration Module

c) Data Storage Layer:
- Redis Database

##### Component Relationships

- Client requests → HTTP Server → API Handlers
- API Handlers → Core Services → Redis Storage
- Stats Collection Flow (separate flow for analytics)

##### Detailed Component Breakdown

**┌─────────────────────────────────────┐**
- Client Layer                        │
- ┘
- ↓
**┌─────────────────────────────────────┐**
- API Layer (Fiber Framework)         │
- Link Shortening                   │
- Link Retrieval                    │
- Stats Retrieval                   │
- Rate Limiting                     │
- ┘
- ↓
**┌─────────────────────────────────────┐**
- Core Services                       │
- ┌─────────┐ ┌─────────┐ ┌────────┐ │
- Links   │ │ Stats   │ │ ID Gen │ │
- ┘ └─────────┘ └────────┘ │
- ┘
- ↓
**┌─────────────────────────────────────┐**
- Data Storage (Redis)                │
- ┘

##### Technical Details

Annotations for:
- Redis connection details
- API endpoints (/api/v1/links/*)
- Rate limiting configuration
- TTL management for links
- CORS configuration

a) Request Flow:
- Incoming HTTP request
- Rate limiting check
- URL processing
- ID generation
- Storage operation
- Response formation

b) Statistics Flow:
- UTM parameter extraction
- Stats aggregation
- Storage in Redis

##### Additional Elements

Callouts for:
- Environment configuration options
- Security measures
- Scaling points
- Error handling paths

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client
        C[Client]
    end

    subgraph API_Layer
        HS["HTTP Server"]:::api
        RateLimit["Rate Limiter"]:::security
        ParamValid["Parameter Validator"]:::api
        APIHandler["API Endpoints Handler"]:::api
    end

    subgraph Core_Services
        subgraph Links_Module
            LS["Links Service"]:::service
            LR["Links Repository"]:::service
            LD["Links Domain"]:::service
        end

        subgraph Stats_Module
            SS["Stats Service"]:::service
            SR["Stats Repository"]:::service
            SD["Stats DTO"]:::service
        end

        subgraph ID_Module
            IDGen["ID Generator"]:::service
            IDConfig["ID Module Config"]:::config
        end

        subgraph Config_Module
            CoreConfig["Core Config"]:::config
            ModConfig["Module Setup"]:::config
        end
    end

    subgraph Storage
        Redis[(Redis DB)]:::storage
    end

    subgraph Infrastructure
        Docker["Docker Container"]:::infra
        Compose["Docker Compose"]:::infra
    end

    C --> HS
    HS --> RateLimit
    RateLimit --> ParamValid
    ParamValid --> APIHandler
    APIHandler --> LS
    APIHandler --> SS

    LS --> LR
    LR --> Redis
    LS --> IDGen
    IDGen --> IDConfig

    SS --> SR
    SR --> Redis

    CoreConfig --> ModConfig
    ModConfig --> LS
    ModConfig --> SS
    ModConfig --> IDGen

    classDef api fill:#2374ab
    classDef security fill:#ff4d4d
    classDef service fill:#4CAF50
    classDef storage fill:#ffd700
    classDef config fill:#808080
    classDef infra fill:#a0522d

    click HS "https://github.com/tnfy-link/backend/blob/master/internal/api/module.go"
    click RateLimit "https://github.com/tnfy-link/backend/blob/master/internal/api/limiter/limiter.go"
    click LS "https://github.com/tnfy-link/backend/blob/master/internal/links/service.go"
    click LR "https://github.com/tnfy-link/backend/blob/master/internal/links/repository.go"
    click LD "https://github.com/tnfy-link/backend/blob/master/internal/links/domain.go"
    click SS "https://github.com/tnfy-link/backend/blob/master/internal/stats/service.go"
    click SR "https://github.com/tnfy-link/backend/blob/master/internal/stats/repository.go"
    click SD "https://github.com/tnfy-link/backend/blob/master/internal/stats/dto.go"
    click IDGen "https://github.com/tnfy-link/backend/blob/master/internal/id/id.go"
    click IDConfig "https://github.com/tnfy-link/backend/blob/master/internal/id/module.go"
    click CoreConfig "https://github.com/tnfy-link/backend/blob/master/internal/config/config.go"
    click ModConfig "https://github.com/tnfy-link/backend/blob/master/internal/config/module.go"
    click ParamValid "https://github.com/tnfy-link/backend/blob/master/internal/api/param/validator.go"
    click APIHandler "https://github.com/tnfy-link/backend/blob/master/internal/api/links.go"
    click Docker "https://github.com/tnfy-link/backend/tree/master/Dockerfile"
    click Compose "https://github.com/tnfy-link/backend/blob/master/compose.yaml"
```
