# yingtu35/shortenme architecture

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

- Interactive diagram: https://gitdiagram.com/yingtu35/shortenme
- Repository: https://github.com/yingtu35/shortenme

## Overview

##### Main Components
- Frontend/UI Layer: Represented by the templates folder. This includes HTML pages (e.g., index.html, shorten.html, not-found.html, etc.) and static assets like styles.css that display the web interface to users.
- API/Handlers: The internal/api package is where the HTTP handlers are defined. This layer handles incoming HTTP requests (e.g., for shortening URLs and reporting click counts) and orchestrates the application logic.
- Configuration: The internal/config package holds application configuration logic. This component loads and manages configuration parameters (such as reading the environment variables from .env) that influence the behavior of the other layers.
- Data Storage & Business Logic: The internal/store package manages URL mappings and click count data. With implementations using Redis (redis.go) and additional logic (base62.go) for encoding/decoding, this represents the stateful backend service that maintains the URL shortening records.
- Application Entry Point: The cmd/app/main.go file is the primary entry point that ties all these modules together, sets up the HTTP server, initializes configurations, and connects to Redis.
- External Dependencies: Redis is used as a backing store for URL data and click counts. Docker configurations and CI/CD workflows (in the .dockerfile, .github/workflows/) are also part of the ecosystem for build and deployment automation.

##### Relationships and Interactions
- The frontend (templates) interacts with the API layer. For example, when a user submits a long URL, the browser sends a request that the API (internal/api) processes.
- The API handlers then interact with the store layer. This involves converting the long URL to a shortened version (using base62 encoding logic in internal/store) and updating click counts.
- The configuration component is used by the main application to pass parameters (such as rate limiting or Redis connection info) to relevant parts of the system.
- Finally, the application deploys using Docker (see .dockerfile) and is validated by CI/CD workflows shown in the .github directory.

##### Architectural Patterns and Design Principles
- Separation of Concerns: You have clearly defined modules in the codebase – API handling, configuration, data storage, and the UI layer are all separated.
- MVC-like Flow: Although not a classic MVC framework, your components work similarly – the API (Controller) processes input and interacts with the store (Model), and the templates serve as the View.
- Dependency Injection & Config-driven: The application initializes its configuration (internal/config) and then wires everything together in main.go, ensuring components are decoupled and testable.
- Use of External Systems: Integration with Redis for state persistence and Docker for containerized deployment emphasizes modern cloud and container practices.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client
    User["Client/Browser"]:::client

    %% Presentation Layer
    subgraph "Presentation Layer"
        Templates["Templates (HTML/CSS)"]:::frontend
    end

    %% API Layer
    subgraph "API Layer"
        API["API/Handlers (internal/api)"]:::api
    end

    %% Backend Layer
    subgraph "Backend Layer"
        Config["Configuration (internal/config)"]:::config
        MainApp["App Entry Point (main.go)"]:::main
        Store["Data Storage & Business Logic (Store)"]:::store
        Redis["Redis (External Data Store)"]:::external
    end

    %% Deployment & Integration
    subgraph "Deployment & Integration"
        Docker["Docker Config"]:::deploy
        CICD["CI/CD Workflows"]:::deploy
    end

    %% Flow Connections
    User -->|"HTTPRequest"| Templates
    Templates -->|"SubmitURL"| API
    API -->|"UsesConfig"| Config
    API -->|"CallsStore"| Store
    Store -->|"Connects"| Redis
    MainApp -->|"InitializedBy"| Config
    MainApp -->|"Wires"| API
    MainApp -->|"Wires"| Store
    Docker -->|"Deploys"| MainApp
    CICD -->|"Validates"| MainApp

    %% Click Events
    click Templates "https://github.com/yingtu35/shortenme/tree/main/templates/"
    click API "https://github.com/yingtu35/shortenme/tree/main/internal/api/"
    click Config "https://github.com/yingtu35/shortenme/blob/main/internal/config/config.go"
    click Store "https://github.com/yingtu35/shortenme/tree/main/internal/store/"
    click MainApp "https://github.com/yingtu35/shortenme/blob/main/cmd/app/main.go"
    click Docker "https://github.com/yingtu35/shortenme/blob/main/.dockerfile"
    click CICD "https://github.com/yingtu35/shortenme/tree/main/.github/workflows/"

    %% Styles
    classDef client fill:#ffd1dc,stroke:#cc338b,stroke-width:2px,color:#000;
    classDef frontend fill:#f4d03f,stroke:#aa7f39,stroke-width:2px,color:#000;
    classDef api fill:#85c1e9,stroke:#2e86c1,stroke-width:2px,color:#000;
    classDef config fill:#a9dfbf,stroke:#45b39d,stroke-width:2px,color:#000;
    classDef main fill:#fad7a0,stroke:#f39c12,stroke-width:2px,color:#000;
    classDef store fill:#f5b7b1,stroke:#ec7063,stroke-width:2px,color:#000;
    classDef external fill:#d2b4de,stroke:#8e44ad,stroke-width:2px,color:#000;
    classDef deploy fill:#f9e79f,stroke:#f1c40f,stroke-width:2px,color:#000;
```
