# ronishg27/url-shortener architecture

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

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

## Overview

##### Main Components
- Frontend (UI): The client‑facing part is built within the Next.js app. Under “src/app” you have pages like:
- The landing page (app/page.tsx)
- The dashboard (app/dashboard/page.tsx)
- Dynamic pages: paths such as “[...shortId]” and “info/[shortId]/page.tsx”
- Additionally, components under “src/components” (like url‑shortener.tsx and verify‑passkey.tsx) represent building blocks for the UI.
- API Routes: Under “src/app/api,” separate route handlers exist for URL functionality:
- “api/url” (including a dynamic route “[...shortId]/route.ts” and a static “route.ts”)
- “api/all‑urls” (with its own route.ts)
This indicates that the frontend is interacting with backend API endpoints that process URL shortener requests.
- Server‑Side Logic & Utilities: The “src/lib” and “src/utils” directories contain helper functions and utility methods (e.g., “shortenUrl.ts”, “connectDB.ts”, “getUrlDetails.ts”). This layer represents business logic such as database connectivity and URL processing.
- Data Models: The “src/models/url.model.ts” defines the data schema/model for URLs, which likely interacts with a connected database.
- Configuration & Styles: There are configuration files (next.config.mjs, postcss.config.mjs, tailwind.config.ts, etc.) and global CSS (src/app/globals.css) that coordinate styling and project settings.

##### Relationships and Data Flows
- Client Requests (User Browsing): Users interact with the frontend pages which use the UI components (like the URL shortener widget) to send requests.
- Business Logic Flow: Within the API routes, functions from “src/utils” and “src/lib” process the requests. The “connectDB.ts” utility suggests that API handlers establish a connection to the database.
- Data Persistence: The URL model (“src/models/url.model.ts”) represents the database schema used for storing and retrieving URL data.
- Feedback Loop: Results then flow back through the API to update the frontend UI (dashboard, individual URL details, etc.).

##### Architectural Patterns & Technologies
- Next.js App Router: The project uses file‑based routing (both for pages and API endpoints) following Next.js conventions.
- Separation of Concerns: The code cleanly separates UI components, API logic, business utilities, and data models.
- Dynamic Routing & API Layer: The dynamic route segments ([...shortId]) indicate that the system supports dynamic URL handling.
- Tailwind CSS & UI Components: The presence of Tailwind config and a “components/ui” directory implies that UI styling is modularized using reusable components.
- Database Connectivity: “connectDB.ts” and “url.model.ts” hint at a traditional backend service architecture interacting with a database (the specifics of which may be determined by what’s in “connectDB.ts”).

##### Detailed Elements
- Labels: Clearly name each component (e.g., “Dashboard Page,” “URL API Handler,” “Database Connector”).

## Diagram (Mermaid)

```mermaid
graph TD
    %% User
    U("User Browser"):::user

    %% Frontend Layer
    subgraph "Frontend (Next.js Pages & Components)"
        LP("Landing Page"):::frontend
        DP("Dashboard Page"):::frontend
        DSP("Dynamic Short URL Page"):::frontend
        DIP("Dynamic Info Page"):::frontend
        USW("URL Shortener Widget"):::frontend
        PV("Passkey Verification"):::frontend
    end

    %% API Routes
    subgraph "API Routes"
        API_Static("URL API Handler (Static)"):::api
        API_Dynamic("URL API Handler (Dynamic)"):::api
        API_All("Get All URLs API"):::api
    end

    %% Business Logic & Utilities
    subgraph "Business Logic & Utilities"
        DBConn("Database Connector"):::business
        Shorten("URL Shortening Logic"):::business
        GetDetails("URL Detail Retrieval"):::business
        Helpers("Additional Helpers"):::business
    end

    %% Data Model / Database
    subgraph "Data Model / Database"
        URLModel("URL Schema"):::database
    end

    %% Connections: User to Frontend Pages
    U -->|"browses"| LP
    U -->|"browses"| DP
    U -->|"browses"| DSP
    U -->|"browses"| DIP

    %% Connections: Frontend to API Routes (HTTP Requests via AJAX)
    LP -->|"HTTP_Request"| API_Static
    DP -->|"HTTP_Request"| API_All
    DSP -->|"HTTP_Request"| API_Dynamic
    DIP -->|"HTTP_Request"| API_Static
    USW -->|"AJAX_call"| API_Static
    PV -->|"AJAX_call"| API_Static

    %% Connections: API Routes to Business Logic
    API_Static -->|"Process_Request"| Shorten
    API_Dynamic -->|"Process_Request"| GetDetails
    API_All -->|"Fetch_Data"| Helpers

    %% Connections: Business Logic to Data Model / Database
    DBConn -->|"DB_Query"| URLModel
    Shorten -->|"DB_Update"| URLModel
    GetDetails -->|"DB_Query"| URLModel
    Helpers -->|"DB_Query"| URLModel

    %% Connections: API Routes invoke Database connection
    API_Static -->|"Init_DB"| DBConn
    API_Dynamic -->|"Init_DB"| DBConn
    API_All -->|"Init_DB"| DBConn

    %% Click Events
    click LP "https://github.com/ronishg27/url-shortener/blob/main/src/app/page.tsx"
    click DP "https://github.com/ronishg27/url-shortener/blob/main/src/app/dashboard/page.tsx"
    click DSP "https://github.com/ronishg27/url-shortener/blob/main/src/app/[...shortId]/route.ts"
    click DIP "https://github.com/ronishg27/url-shortener/blob/main/src/app/info/[shortId]/page.tsx"
    click USW "https://github.com/ronishg27/url-shortener/blob/main/src/components/url-shortener.tsx"
    click PV "https://github.com/ronishg27/url-shortener/blob/main/src/components/verify-passkey.tsx"
    click API_Static "https://github.com/ronishg27/url-shortener/blob/main/src/app/api/url/route.ts"
    click API_Dynamic "https://github.com/ronishg27/url-shortener/blob/main/src/app/api/url/[...shortId]/route.ts"
    click API_All "https://github.com/ronishg27/url-shortener/blob/main/src/app/api/all-urls/route.ts"
    click DBConn "https://github.com/ronishg27/url-shortener/blob/main/src/utils/connectDB.ts"
    click Shorten "https://github.com/ronishg27/url-shortener/blob/main/src/utils/shortenUrl.ts"
    click GetDetails "https://github.com/ronishg27/url-shortener/blob/main/src/utils/getUrlDetails.ts"
    click URLModel "https://github.com/ronishg27/url-shortener/blob/main/src/models/url.model.ts"
    click Helpers "https://github.com/ronishg27/url-shortener/blob/main/src/lib/utils.ts"

    %% Styles
    classDef user fill:#ef9a9a,stroke:#b71c1c,stroke-width:2px;
    classDef frontend fill:#bbdefb,stroke:#0d47a1,stroke-width:2px;
    classDef api fill:#ffcc80,stroke:#e65100,stroke-width:2px;
    classDef business fill:#c8e6c9,stroke:#1b5e20,stroke-width:2px;
    classDef database fill:#ffe082,stroke:#ff6f00,stroke-width:2px;
```
