# rmahsanfaraz/url-shortner architecture

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

- Interactive diagram: https://gitdiagram.com/rmahsanfaraz/url-shortner
- Repository: https://github.com/rmahsanfaraz/url-shortner

## Overview

##### Project type and purpose
- This is a monolithic, full-stack web application: a PHP-based URL shortener with a MySQL database.
- It provides both a user-facing UI (shorten URLs, copy/share, QR code) and a management UI (list, delete, analytics).

##### File structure
- Top-level files:
- index.php & manage.html → two entry points (main UI and management UI)
- config.php → central database configuration
- shorten.php, redirect.php, analytics.php, list.php, delete.php, qrcode.php → backend “API” endpoints
- style.css, scripts.js, manage.js → front-end assets
- database.sql, migration.sql → schema creation and migration
- No separate “frontend” or “backend” directories—but logically we can group by UI vs. API vs. data.

##### Examining the README
- Confirms MVC-style separation: UI pages call JavaScript, which issues HTTP requests to PHP endpoints.
- Describes all features, API contracts, and external dependency (Google Charts for QR codes).

a. Main components
1. Client (Web Browser)
- Renders index.php or manage.html
- Hosts JavaScript (scripts.js, manage.js) and CSS
2. Web Server / PHP Runtime
- Serves static assets and executes PHP scripts
- Houses all “controllers” (shorten.php, redirect.php, analytics.php, list.php, delete.php, qrcode.php)
- Uses config.php to connect to the database
3. MySQL Database
- Single table `urls` with columns for code, original URL, created_at, expires_at, last_accessed, visit_count
4. External Service
- Google Charts API (via qrcode.php) for QR code generation
b. Relationships and interactions
- Browser → HTTP GET/POST → Web Server
- index.php loads HTML/CSS/JS → JS calls shorten.php (POST), list.php (GET), delete.php (DELETE), analytics.php (GET), qrcode.php (GET)
- redirect.php is invoked by any visitor clicking the short URL → queries DB → issues HTTP redirect
- PHP endpoints ↔ MySQL over standard TCP (port 3306)
- qrcode.php → HTTP call to Google Charts → returns QR image URL
c. Architectural patterns and principles
- Monolithic, single-tier architecture deployed on one host
- Logical separation:
- Presentation layer (HTML/CSS/JS)
- Application/API layer (PHP scripts)
- Data layer (MySQL)
- REST-style endpoints for CRUD operations
- Configuration centralization in config.php
d. Key technologies
- Frontend: HTML5, CSS3, vanilla JavaScript
- Backend: PHP 7.x+ on Apache/Nginx
- Database: MySQL/MariaDB
- External: Google Charts API for QR

##### Detail
- PHP endpoint as its own sub-component
- Config.php as the single source of DB connection properties
- Optional expiration logic and visit_count update in the redirect flow
- Don’t overcomplicate: the goal is clarity of how browsers, PHP scripts, the DB, and the QR service interact

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Client: Web Browser"
        direction TB
        index["index.php"]:::frontend
        manage["manage.html"]:::frontend
        css["style.css"]:::frontend
        js["scripts.js"]:::frontend
        manageJs["manage.js"]:::frontend
    end

    %% Server Layer
    subgraph "Web Server / PHP App"
        direction TB
        config["config.php"]:::backend

        subgraph "UI Handlers"
            direction TB
            indexH["index.php"]:::backend
            manageH["manage.html"]:::backend
        end

        subgraph "API Endpoints"
            direction TB
            shorten["shorten.php"]:::backend
            redirect["redirect.php"]:::backend
            analytics["analytics.php"]:::backend
            list["list.php"]:::backend
            delete["delete.php"]:::backend
            qrcode["qrcode.php"]:::backend
        end

        subgraph "Database Scripts"
            direction TB
            db1["database.sql"]:::backend
            db2["migration.sql"]:::backend
        end
    end

    %% Database
    DB["MySQL Database"]:::db

    %% External Service
    External["Google Charts API"]:::external

    %% Client to Server Asset Loading
    index -->|loads| css
    index -->|loads| js
    manage -->|loads| css
    manage -->|loads| manageJs

    %% Client requests to UI Handlers
    index -->|HTTP GET| indexH
    manage -->|HTTP GET| manageH

    %% JS/API interactions
    js -->|POST to create| shorten
    js -->|GET list| list
    manageJs -->|GET analytics| analytics
    manageJs -->|DELETE URL| delete
    js -->|GET QR code| qrcode

    %% Redirect flow
    index -->|click short URL| redirect

    %% PHP App to Config and DB
    shorten -->|uses| config
    redirect -->|uses| config
    analytics -->|uses| config
    list -->|uses| config
    delete -->|uses| config
    config -->|connection| DB

    shorten -->|write/read| DB
    redirect -->|read/update| DB
    analytics -->|read| DB
    list -->|read| DB
    delete -->|write| DB

    %% QR code external call
    qrcode -->|HTTP request| External

    %% Styling classes
    classDef frontend fill:#cce5ff,stroke:#0366d6,color:#0366d6
    classDef backend fill:#d4edda,stroke:#28a745,color:#155724
    classDef db fill:#fff3cd,stroke:#d39e00,color:#856404,shape:cylinder
    classDef external fill:#e2e3e5,stroke:#6c757d,color:#383d41

    %% Click Events
    click index "https://github.com/rmahsanfaraz/url-shortner/blob/main/index.php"
    click manage "https://github.com/rmahsanfaraz/url-shortner/blob/main/manage.html"
    click css "https://github.com/rmahsanfaraz/url-shortner/blob/main/style.css"
    click js "https://github.com/rmahsanfaraz/url-shortner/blob/main/scripts.js"
    click manageJs "https://github.com/rmahsanfaraz/url-shortner/blob/main/manage.js"
    click config "https://github.com/rmahsanfaraz/url-shortner/blob/main/config.php"
    click shorten "https://github.com/rmahsanfaraz/url-shortner/blob/main/shorten.php"
    click redirect "https://github.com/rmahsanfaraz/url-shortner/blob/main/redirect.php"
    click analytics "https://github.com/rmahsanfaraz/url-shortner/blob/main/analytics.php"
    click list "https://github.com/rmahsanfaraz/url-shortner/blob/main/list.php"
    click delete "https://github.com/rmahsanfaraz/url-shortner/blob/main/delete.php"
    click qrcode "https://github.com/rmahsanfaraz/url-shortner/blob/main/qrcode.php"
    click db1 "https://github.com/rmahsanfaraz/url-shortner/blob/main/database.sql"
    click db2 "https://github.com/rmahsanfaraz/url-shortner/blob/main/migration.sql"
```
