# yuvii-b/url-shortener architecture

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

- Interactive diagram: https://gitdiagram.com/yuvii-b/url-shortener
- Repository: https://github.com/yuvii-b/url-shortener

## Overview

##### Project Type and Purpose
- This is a C++-based URL Shortening service with two front-ends:
- A terminal/console application (app.cpp)
- A lightweight HTTP server with web UI (using Crow-CPP)
- Core goal: take long URLs, generate short numeric codes, store/retrieve them in SQLite, and redirect browser requests.

##### File Structure
- Top level:
- Makefile, run.sh – build and orchestrate processes
- app.cpp – console UI launcher
- README.md – user/developer guide
- db/ – database module
- db.cpp/.hpp – SQLite wrapper
- libs/ – static libsqlite3.a + headers
- shortener/ – core business logic
- url_shortener.cpp/.hpp – hashing, code generation, lookup
- server/ – HTTP server
- http_server.cpp/.hpp – Crow routes for shortening & redirect
- server_test.cpp – unit tests for server functionality
- frontend/ – web UI assets
- main.cpp – entry to spin up web UI (if separate) or client code
- templates/index.html – user-friendly form

##### README for Architectural Clues
- Two executables: “server” (Crow HTTP service) and the CLI tool.
- Shared dependency on the core URL shortener library and SQLite DB.
- run.sh starts both processes and monitors for crashes.

a. Main Components
1. Users & Clients
– CLI user (terminal)
– Web browser
2. Applications
– Console App (app.cpp)
– HTTP Server (http_server.cpp + Crow)
3. Core Service Layer
– URL Shortener Library (shortener/url_shortener.cpp)
4. Data Store
– SQLite Database (db/db.cpp + libsqlite3.a)
5. Orchestration Script
– run.sh (process manager)
b. Relationships & Interactions
– CLI App → URL Shortener → SQLite (CREATE/READ)
– Browser → HTTP Server → URL Shortener → SQLite (LOOKUP)
– HTTP Server serves static files (templates/index.html)
– run.sh launches & supervises CLI App & HTTP Server
c. Architectural Patterns & Principles
– Layered Architecture: Presentation (CLI/HTTP) → Business Logic (Shortener) → Persistence (SQLite)
– Single Responsibility: distinct modules for DB access, core logic, HTTP handling, UI
– Dependency Injection (linking shared library code into both server and CLI at build time)
d. Key Technologies & Frameworks
– C++17
– Crow-CPP (HTTP server & routing)
– SQLite (embedded RDBMS)
– Makefile for build automation
– run.sh for process orchestration

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation Layer
    subgraph "Presentation Layer"
        CLI["Console Application"]:::presentation
        Browser["Web Browser"]:::external
        UIClient["Web UI Launcher"]:::presentation
    end

    %% Service Layer
    subgraph "Application & Service Layer"
        HTTPServer["HTTP Server"]:::service
        Shortener["URL Shortener Library"]:::service
        ServerTest["Server Unit Tests"]:::service
    end

    %% Persistence Layer
    subgraph "Persistence Layer"
        SQLiteModule["SQLite DB Module"]:::persistence
        SQLiteLib["SQLite Static Lib & Headers"]:::persistence
    end

    %% Orchestration & Build
    subgraph "Orchestration & Build"
        Run["run.sh"]:::orchestration
        MakefileNode["Makefile"]:::orchestration
        README["README.md"]:::orchestration
    end

    %% Relationships
    CLI -->|"shorten(URL)"| Shortener
    Browser -->|"POST /shorten"| HTTPServer
    HTTPServer -->|"shorten(URL)"| Shortener
    Shortener -->|"store/retrieve"| SQLiteModule
    SQLiteModule -->|"db file access"| SQLiteLib
    HTTPServer -->|"lookup(code)"| Shortener
    HTTPServer -->|"serve(static)"| Templates
    Templates["index.html"]:::presentation -->|serves| Browser
    HTTPServer -->|"redirect"| Browser
    ServerTest -.-> HTTPServer
    Run -->|"launches"| CLI
    Run -->|"launches"| HTTPServer
    MakefileNode -->|"builds"| CLI
    MakefileNode -->|"builds"| HTTPServer
    MakefileNode -->|"builds"| Shortener
    MakefileNode -->|"builds"| SQLiteModule

    %% Click Events
    click CLI "https://github.com/yuvii-b/url-shortener/blob/main/app.cpp"
    click HTTPServer "https://github.com/yuvii-b/url-shortener/blob/main/server/http_server.cpp"
    click HTTPServer "https://github.com/yuvii-b/url-shortener/blob/main/server/http_server.hpp"
    click ServerTest "https://github.com/yuvii-b/url-shortener/blob/main/server/server_test.cpp"
    click Shortener "https://github.com/yuvii-b/url-shortener/blob/main/shortener/url_shortener.cpp"
    click Shortener "https://github.com/yuvii-b/url-shortener/blob/main/shortener/url_shortener.hpp"
    click SQLiteModule "https://github.com/yuvii-b/url-shortener/blob/main/db/db.cpp"
    click SQLiteModule "https://github.com/yuvii-b/url-shortener/blob/main/db/db.hpp"
    click SQLiteLib "https://github.com/yuvii-b/url-shortener/blob/main/db/libs/libsqlite3.a"
    click SQLiteLib "https://github.com/yuvii-b/url-shortener/blob/main/db/libs/sqlite3.h"
    click UIClient "https://github.com/yuvii-b/url-shortener/blob/main/frontend/main.cpp"
    click Templates "https://github.com/yuvii-b/url-shortener/blob/main/frontend/templates/index.html"
    click Run "https://github.com/yuvii-b/url-shortener/blob/main/run.sh"
    click MakefileNode "https://github.com/yuvii-b/url-shortener/tree/main/Makefile"
    click README "https://github.com/yuvii-b/url-shortener/blob/main/README.md"

    %% Styles
    classDef presentation fill:#D6EAF8,stroke:#1F618D
    classDef service fill:#F9E79F,stroke:#B9770E
    classDef persistence fill:#ABEBC6,stroke:#239B56
    classDef orchestration fill:#E5E8E8,stroke:#566573
    classDef external fill:#F5B7B1,stroke:#922B21
```
