# bharabhi01/shorturl-go architecture

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

- Interactive diagram: https://gitdiagram.com/bharabhi01/shorturl-go
- Repository: https://github.com/bharabhi01/shorturl-go

## Overview

##### Project Type and Purpose
- This project is a microservice-style, full-stack backend application written in Go. Its primary purpose is to offer a scalable URL shortening service.
- Key features include generating short URLs from long ones, redirecting users, rate limiting via middleware, and performance improvements using Redis caching alongside PostgreSQL for persistent storage.

##### File Structure
- Top-level directories such as "api", "cmd", "config", and "internal" indicate separation of concerns:
- The "api" folder contains HTTP handlers and middleware (e.g., logging and rate limiting), which suggests that incoming HTTP requests are managed here.
- The "cmd" directory holds executable commands, specifically the server launch (cmd/server/main.go) and test-related utilities (cmd/test).
- The "config" directory is used for configuration settings.
- The "internal" directory is split into:
- "repository": Handles data interactions (database and cache logic) and defines the data models.
- "service": Contains business logic that processes URL shortening and redirection.
- The use of separate directories emphasizes the architectural principle of modularity and clear separation of the data layer, business logic, and API layer.

##### Additional Insights from the README
- The README gives an overview of the tech stack (Go, Gin for the web framework, PostgreSQL, and Redis).
- It outlines the API endpoints (GET for redirects, POST for URL creation, and a health check), which helps in mapping out the communication flow.

## Diagram (Mermaid)

```mermaid
flowchart TD
    Client("Client/Browser"):::client

    subgraph "API Layer"
        Logger("Logger Middleware"):::api
        RateLimiter("Rate Limiter Middleware"):::api
        HttpHandlers("HTTP Handlers"):::api
    end

    subgraph "Service Layer"
        URLService("URL Service"):::service
    end

    subgraph "Repository/Data Access"
        URLRepo("URL Repository"):::repository
        DBConn(("Database Connection (PostgreSQL)")):::db
        RedisCache(("Redis Caching")):::cache
    end

    Config("Configuration Management"):::config

    Client -->|"sends_request"| Logger
    Logger -->|"forwards_request"| RateLimiter
    RateLimiter -->|"routes_to"| HttpHandlers
    HttpHandlers -->|"calls"| URLService
    URLService -->|"invokes"| URLRepo
    URLRepo -->|"queries/inserts"| DBConn
    URLRepo -->|"caches_data_in"| RedisCache
    Config -->|"provides_settings"| Logger
    Config -->|"provides_settings"| RateLimiter
    Config -->|"provides_settings"| HttpHandlers
    Config -->|"provides_settings"| URLService

    click HttpHandlers "https://github.com/bharabhi01/shorturl-go/blob/main/api/handlers/url_handlers.go"
    click Logger "https://github.com/bharabhi01/shorturl-go/blob/main/api/middleware/logger.go"
    click RateLimiter "https://github.com/bharabhi01/shorturl-go/blob/main/api/middleware/rate_limiter.go"
    click URLService "https://github.com/bharabhi01/shorturl-go/blob/main/internal/service/url_service.go"
    click URLRepo "https://github.com/bharabhi01/shorturl-go/blob/main/internal/repository/url_repository.go"
    click DBConn "https://github.com/bharabhi01/shorturl-go/blob/main/internal/repository/db.go"
    click RedisCache "https://github.com/bharabhi01/shorturl-go/blob/main/internal/repository/cache.go"
    click Config "https://github.com/bharabhi01/shorturl-go/blob/main/config/config.go"

    classDef client fill:#b3cde0,stroke:#000,stroke-width:1px;
    classDef api fill:#fdae6b,stroke:#d35400,stroke-width:2px;
    classDef service fill:#a1d99b,stroke:#238b45,stroke-width:2px;
    classDef repository fill:#9ecae1,stroke:#3182bd,stroke-width:2px;
    classDef db fill:#fc9272,stroke:#de2d26,stroke-width:2px;
    classDef cache fill:#fdd49e,stroke:#f16913,stroke-width:2px;
    classDef config fill:#c7e9c0,stroke:#31a354,stroke-width:2px;
```
