# zeenox-stack/url-shortener-backend-with-go architecture

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

- Interactive diagram: https://gitdiagram.com/zeenox-stack/url-shortener-backend-with-go
- Repository: https://github.com/zeenox-stack/url-shortener-backend-with-go

## Overview

##### Overall Project Type and Purpose
- This is a backend microservice solely responsible for URL shortening.
- It consists of a single executable, as seen with main.go and its dependencies in go.mod and go.sum.
- The service is designed for handling HTTP requests (both POST for URL creation and GET for redirection) and provides a lightweight in-memory datastore.

##### Key Components
- Client Applications: Represent users (or any client like cURL, JavaScript Fetch, or Axios) that interact with the service.
- HTTP Server: The Go application running on localhost:8000.
- POST /shorten endpoint – accepts a JSON payload with the URL to be shortened.
- GET /<hashed_key> endpoint – processes redirection to the original URL.
- URL Shortener Service:
- Logic for processing input requests.
- Invokes URL hashing using SHA-256.
- Implements up to 1000 URL storage with automatic reset.
- In-Memory Data Store:
- A URL map used for storing mappings between short keys and original URLs.
- Incorporates sync.Mutex for concurrent access safety.
- (Optional) Maintenance/Reset Handler:
- Although not explicitly detailed, it is noted that storage resets after 1000 URLs. This mechanism could be represented as part of the URL Shortener Service.

##### Relationships and Interactions
- URL Shortener Service to the In-Memory Data Store, emphasizing that it writes to and reads from this store.

##### Architectural Patterns and Design Principles
- The service follows a layered architecture:
- Presentation Layer: HTTP request handling.
- Business Logic Layer: URL hashing, validation, URL mapping, and storage management.
- Data Layer: In-memory map.
- Concurrency control is a key design point (via sync.Mutex) – highlighting the importance of thread safety when multiple requests are processed in parallel.

## Diagram (Mermaid)

```mermaid
graph TB
    %% Define nodes
    Client["Client (Browser/cURL)"]:::client
    HTTPServer["HTTP Server (Go Application)"]:::server
    URLShortener["URL Shortener Service (Hashing/Mapping Logic)"]:::service
    DataStore["In-Memory URL Storage (Map with sync.Mutex)"]:::datastore

    %% Flow of requests
    Client -->|"POST/GET request"| HTTPServer
    HTTPServer -->|"Process HTTP request"| URLShortener
    URLShortener -->|"Store/Retrieve URL Mapping"| DataStore
    URLShortener -->|"Trigger automatic reset after 1000 entries"| DataStore

    %% Click events for component mapping
    click HTTPServer "https://github.com/zeenox-stack/url-shortener-backend-with-go/blob/main/main.go"
    click URLShortener "https://github.com/zeenox-stack/url-shortener-backend-with-go/blob/main/main.go"
    click DataStore "https://github.com/zeenox-stack/url-shortener-backend-with-go/blob/main/main.go"

    %% Styles
    classDef client fill:#a6cee3,stroke:#000,stroke-width:1px;
    classDef server fill:#b2df8a,stroke:#000,stroke-width:1px;
    classDef service fill:#fb9a99,stroke:#000,stroke-width:1px;
    classDef datastore fill:#fdbf6f,stroke:#000,stroke-width:1px;
```
