# mehrabmp/cut-it architecture

> Architecture diagram of the GitHub repository mehrabmp/cut-it, 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/mehrabmp/cut-it
- Repository: https://github.com/mehrabmp/cut-it

## Overview

##### Project Type & Purpose
- Cut It is a full-stack web application aimed at generating short links. It features user authentication, customizable short URLs, and various API endpoints for URL management.
- The README and file structure clearly indicate a Next.js frontend integrated with a backend API layer, database migrations (via Drizzle), and additional support for caching (using Redis/Upstash) and authentication (NextAuth.js).

##### Key Components
a. Frontend:
- UI/Styling Layer: Highlight the usage of Tailwind CSS and shadcn-ui (custom UI components).

b. API & Backend Server:
- API Endpoints: Illustrate the RESTful endpoints provided in src/app/api. Separate endpoints by functional areas such as authentication (src/app/api/auth/[...nextauth]), cron jobs (src/app/api/cron), and link management (src/server/api/link, src/server/api/user-link, etc.).
- Server Actions & Middleware: Include components from src/server/actions, src/server/middlewares, and authentication logic from src/server/auth.ts.
- Utility Libraries: Mention libraries under src/lib (like safe-actions, validations, QR code generation).

c. Database & ORM:
- Database: Represent Turso as the primary database, connecting via Drizzle ORM.
- Migrations: Show the “drizzle” directory which contains SQL migration files and corresponding snapshots under drizzle/meta.

d. External Services:
- Authentication Service: NextAuth.js (with providers and custom dialogs in src/components/auth).
- Cache: Redis integration via Upstash depicted by the file src/server/redis.ts.
- Deployment: Vercel as the deployment platform (indicated by vercel.json and Vercel-specific workflows).

- The API Server communicates with both the Database (via Drizzle ORM) and Cache (Redis/Upstash) to manage data.

##### Highlighted Architectural Patterns & Technologies
- Full-Stack Separation: Clearly distinguish between user interface components (frontend) and backend server/API components.
- ORM & Migration Pipeline: Represent the database interactions showing migration scripts flowing into the database, emphasizing the Drizzle ORM integration.
- Caching Layer: Include Redis as a separate, connected component to illustrate caching or session handling.
- Authentication Flow: Represent NextAuth’s role in handling login sessions with directional flows showing how authentication data passes from client to API and potentially external OAuth providers.

## Diagram (Mermaid)

```mermaid
flowchart TD
    UB("User Browser / Client"):::external
    FE("Next.js Frontend\n(Pages, UI Components, Tailwind CSS)"):::frontend
    API("API Server / Next.js API Routes"):::backend

    subgraph "Backend Layer"
        AuthAPI("Authentication API\n([...nextauth])"):::backend
        CronAPI("Cron Jobs API\n(cron)"):::backend
        LinkAPI("Link API\n(Link Management)"):::backend
        UserLinkAPI("User Link API\n(User Link Management)"):::backend
        UserAPI("User API\n(User Management)"):::backend
        Actions("Server Actions"):::backend
        Middlewares("Middlewares"):::backend
        AuthLogic("Authentication Logic"):::backend
        Utility("Utility Libraries"):::backend
    end

    DB("Database\n(Turso via Drizzle ORM)"):::database
    Migrations("Migrations\n(drizzle/ & drizzle/meta)"):::database
    DBSchema("DB Connection & Schema\n(src/server/db)"):::database

    Cache("Cache\n(Redis/Upstash)"):::cache

    NextAuth("NextAuth.js UI\n(Authentication Components)"):::external

    Deploy("Deployment\n(Vercel)"):::external
    CICD("CI/CD Workflows"):::external

    %% Connections
    UB -->|"visits"| FE
    FE -->|"HTTPrequest"| API
    FE -->|"authUI"| NextAuth

    API -->|"calls"| AuthAPI
    API -->|"calls"| CronAPI
    API -->|"calls"| LinkAPI
    API -->|"calls"| UserLinkAPI
    API -->|"calls"| UserAPI
    API -->|"executes"| Actions
    API -->|"uses"| Middlewares
    API -->|"executes"| AuthLogic
    API -->|"utilizes"| Utility

    API -->|"queries"| DB
    API -->|"caches"| Cache

    DB -->|"contains"| Migrations
    DB -->|"has"| DBSchema

    FE -->|"deployed_on"| Deploy
    API -->|"deployed_on"| Deploy
    Deploy -->|"CI_workflow"| CICD

    %% Click Events
    click FE "https://github.com/mehrabmp/cut-it/tree/main/src/app"
    click NextAuth "https://github.com/mehrabmp/cut-it/tree/main/src/components/auth"
    click AuthAPI "https://github.com/mehrabmp/cut-it/blob/main/src/app/api/auth/[...nextauth]/route.ts"
    click CronAPI "https://github.com/mehrabmp/cut-it/blob/main/src/app/api/cron/route.ts"
    click LinkAPI "https://github.com/mehrabmp/cut-it/blob/main/src/server/api/link.ts"
    click UserLinkAPI "https://github.com/mehrabmp/cut-it/blob/main/src/server/api/user-link.ts"
    click UserAPI "https://github.com/mehrabmp/cut-it/blob/main/src/server/api/user.ts"
    click Actions "https://github.com/mehrabmp/cut-it/tree/main/src/server/actions"
    click Middlewares "https://github.com/mehrabmp/cut-it/tree/main/src/server/middlewares"
    click AuthLogic "https://github.com/mehrabmp/cut-it/blob/main/src/server/auth.ts"
    click Utility "https://github.com/mehrabmp/cut-it/tree/main/src/lib"
    click Cache "https://github.com/mehrabmp/cut-it/blob/main/src/server/redis.ts"
    click Migrations "https://github.com/mehrabmp/cut-it/tree/main/drizzle"
    click DBSchema "https://github.com/mehrabmp/cut-it/tree/main/src/server/db"
    click Deploy "https://github.com/mehrabmp/cut-it/blob/main/vercel.json"
    click CICD "https://github.com/mehrabmp/cut-it/blob/main/.github/workflows/main.yml"

    %% Styles
    classDef frontend fill:#add8e6,stroke:#000,stroke-width:1px;
    classDef backend fill:#ffcccb,stroke:#000,stroke-width:1px;
    classDef database fill:#d1ffd1,stroke:#000,stroke-width:1px;
    classDef cache fill:#ffe4b5,stroke:#000,stroke-width:1px;
    classDef external fill:#f0e68c,stroke:#000,stroke-width:1px;
```
