# dhruv-sachdev1313/5minpastebin architecture

> Architecture diagram of the GitHub repository dhruv-sachdev1313/5minpastebin, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/dhruv-sachdev1313/5minpastebin
- Repository: https://github.com/dhruv-sachdev1313/5minpastebin

## Overview

##### Project type and purpose
- This is a lightweight full-stack web application (“5minpastebin.com”) that provides ephemeral pastebin functionality: users paste text/code into a web UI, receive a link, and the paste self-destructs after five minutes.
- It consists of two Go binaries (an API service and a web service), a Redis data store, and a minimal frontend using Go HTML templates, HTMX for dynamic behaviors, and Tailwind CSS for styling.

##### File structure
- Top-level binaries in cmd/
- cmd/api/main.go – entry point for the pastebin API service
- cmd/web/… – entry point for the web UI server plus embedded assets (templates, JS, CSS)
- internal/
- internal/server/ – HTTP route definitions and server setup
- internal/database/ – Redis client, TTL logic, persistence abstraction
- internal/pkg/ – shared utility code
- Configuration and orchestration
- docker-compose.yml – defines Redis container
- Dockerfile – containerizes the Go application
- Makefile – common build, test, run scripts

##### README for additional insights
- Tech stack: Go backend, Redis storage, HTMX, Templ, and Tailwind CSS
- Deployment: uses Docker for local Redis; no external dependencies or third-party services
- No user accounts or configuration—stateless HTTP endpoints, ephemeral data

##### Components
a. Frontend (Browser)
- HTML templates (base.templ, home.templ, viewpaste.templ, notfound.templ)
- Static assets (Tailwind‐generated CSS, HTMX JavaScript)
- HTMX interactions triggering AJAX calls to the API
b. Web UI service (cmd/web)
- Serves initial HTML pages and static assets
- Renders templates using the Go “Templ” engine
- Embeds or proxies HTMX scripts
c. API service (cmd/api)
- Exposes RESTful or JSON endpoints for paste creation, retrieval
- Routes defined in internal/server/routes.go
- HTTP server logic in internal/server/server.go
d. Data store (Redis)
- Hosted in Docker container (docker-compose.yml)
- Provides TTL functionality for automatic paste expiration
- Accessed via internal/database/database.go
e. Shared code (internal/pkg)
- Reusable utilities, constants, helpers
f. Developer tools & environments
- Makefile targets (build, test, run, watch, docker-run)
- Docker + Docker Compose

##### Relationships and interactions
- User’s browser ⟶ Web UI service (HTTP GET home page)
- Browser + HTMX ⟶ API service (HTTP POST create or GET retrieve paste)
- API service ⟶ Redis (SET with TTL, GET)
- API service returns JSON or HTTP status, HTMX updates the browser dynamically
- Separate processes for web UI and API (could be behind the same domain or proxied via NGINX in production)

##### Architectural patterns and design principles
- Separation of concerns: distinct API and UI services
- Layered architecture: HTTP layer (internal/server), business logic (internal/pkg), data access (internal/database)
- Ephemeral data pattern: Redis TTL for auto-cleanup
- Minimal external dependencies for performance and privacy

##### Relevant technologies and frameworks
- Go 1.24+ for both services
- HTMX for client-side interactivity
- Templ engine for server-side HTML rendering
- Tailwind CSS for utility-first styling
- Redis for in-memory persistence with TTL
- Docker + Docker Compose for local development

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Frontend Layer
    subgraph "Frontend (Browser)" 
        direction TB
        Browser["User Browser"]:::frontend
    end

    subgraph "Frontend Assets & Templates" 
        direction TB
        subgraph "HTML Templates"
            direction TB
            BaseTempl["base.templ"]:::frontend
            HomeTempl["home.templ"]:::frontend
            ViewPasteTempl["viewpaste.templ"]:::frontend
            NotFoundTempl["notfound.templ"]:::frontend
        end
        subgraph "Static Assets"
            direction TB
            JSAssets["assets/js/ (HTMX)"]:::frontend
            CSSAsset["styles/input.css"]:::frontend
            EFS["efs.go"]:::frontend
        end
    end

    %% Backend Services
    subgraph "Backend Services"
        direction TB
        WebUI["Web UI Service\n(cmd/web)"]:::backend
        APIService["API Service\n(cmd/api/main.go)"]:::backend
    end

    %% Server & Routing Layer
    subgraph "HTTP Server & Routing" 
        direction TB
        ServerGo["server.go"]:::backend
        RoutesGo["routes.go"]:::backend
        RoutesTest["routes_test.go"]:::backend
    end

    %% Data Layer
    subgraph "Data Store Integration" 
        direction TB
        DBGo["database.go"]:::datastore
        DBTest["database_test.go"]:::datastore
    end

    %% Shared Utilities
    SharedUtil["general.go (internal/pkg)"]:::backend

    %% Data Store
    subgraph "Redis (Docker Container)"
        direction TB
        Redis["Redis with TTL"]:::datastore
    end

    %% Developer Tools & Infrastructure
    subgraph "Dev Tools & Infra" 
        direction TB
        DockerCompose["docker-compose.yml"]:::infra
        Dockerfile["Dockerfile"]:::infra
        Makefile["Makefile"]:::infra
        Air[".air.toml"]:::infra
        GoMod["go.mod / go.sum"]:::infra
    end

    %% Connections
    Browser -->|"GET /"| WebUI
    WebUI -->|"renders"| BaseTempl
    WebUI -->|"renders"| HomeTempl
    WebUI -->|"renders"| ViewPasteTempl
    WebUI -->|"renders"| NotFoundTempl
    WebUI -->|"serves"| JSAssets
    WebUI -->|"serves"| CSSAsset
    WebUI -->|"embeds"| EFS

    Browser -->|"HTMX AJAX"| APIService
    APIService -->|"uses HTTP layer"| ServerGo
    APIService -->|"defines routes"| RoutesGo
    APIService -->|"tested by"| RoutesTest
    ServerGo -->|"calls DB client"| DBGo
    DBGo -->|"tested by"| DBTest
    APIService -->|"calls"| DBGo
    DBGo -->|"connects to"| Redis
    Redis -->|"TTL 5min expiration"| Redis

    APIService -->|"JSON response"| Browser
    SharedUtil -.-> WebUI
    SharedUtil -.-> APIService

    DockerCompose --> Redis
    DockerCompose --> APIService
    DockerCompose --> WebUI
    Dockerfile --> APIService
    Dockerfile --> WebUI
    Makefile --> APIService
    Makefile --> WebUI
    .air.toml --> WebUI
    GoMod --> WebUI
    GoMod --> APIService

    %% Click Events
    click BaseTempl "https://github.com/dhruv-sachdev1313/5minpastebin/blob/main/cmd/web/base.templ"
    click HomeTempl "https://github.com/dhruv-sachdev1313/5minpastebin/blob/main/cmd/web/home.templ"
    click ViewPasteTempl "https://github.com/dhruv-sachdev1313/5minpastebin/blob/main/cmd/web/viewpaste.templ"
    click NotFoundTempl "https://github.com/dhruv-sachdev1313/5minpastebin/blob/main/cmd/web/notfound.templ"
    click JSAssets "https://github.com/dhruv-sachdev1313/5minpastebin/tree/main/cmd/web/assets/js"
    click CSSAsset "https://github.com/dhruv-sachdev1313/5minpastebin/blob/main/cmd/web/styles/input.css"
    click EFS "https://github.com/dhruv-sachdev1313/5minpastebin/blob/main/cmd/web/efs.go"
    click WebUI "https://github.com/dhruv-sachdev1313/5minpastebin/tree/main/cmd/web/"
    click APIService "https://github.com/dhruv-sachdev1313/5minpastebin/blob/main/cmd/api/main.go"
    click ServerGo "https://github.com/dhruv-sachdev1313/5minpastebin/blob/main/internal/server/server.go"
    click RoutesGo "https://github.com/dhruv-sachdev1313/5minpastebin/blob/main/internal/server/routes.go"
    click RoutesTest "https://github.com/dhruv-sachdev1313/5minpastebin/blob/main/internal/server/routes_test.go"
    click DBGo "https://github.com/dhruv-sachdev1313/5minpastebin/blob/main/internal/database/database.go"
    click DBTest "https://github.com/dhruv-sachdev1313/5minpastebin/blob/main/internal/database/database_test.go"
    click SharedUtil "https://github.com/dhruv-sachdev1313/5minpastebin/blob/main/internal/pkg/general.go"
    click DockerCompose "https://github.com/dhruv-sachdev1313/5minpastebin/blob/main/docker-compose.yml"
    click Dockerfile "https://github.com/dhruv-sachdev1313/5minpastebin/tree/main/Dockerfile"
    click Makefile "https://github.com/dhruv-sachdev1313/5minpastebin/tree/main/Makefile"
    click Air "https://github.com/dhruv-sachdev1313/5minpastebin/blob/main/.air.toml"
    click GoMod "https://github.com/dhruv-sachdev1313/5minpastebin/blob/main/go.mod"

    %% Styles
    classDef frontend fill:#E0F7FA,stroke:#0277BD,color:#000
    classDef backend fill:#EAFAE0,stroke:#2E7D32,color:#000
    classDef datastore fill:#FFE0B2,stroke:#EF6C00,color:#000
    classDef infra fill:#ECECEC,stroke:#757575,color:#000
```
