# viditagrawal56/url-shortner architecture

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

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

## Overview

##### Main Components
- Frontend:
- React app built with TypeScript, Vite, and TailwindCSS.
- Contains components, context providers, and pages such as Home, Login, Register, Dashboard, and Create URL.
- Backend:
- Go server with main entry point in server/cmd/main.go.
- Organized in internal packages which include:
▪ Auth: managing user authentication and JWT logic.
▪ Config: reading environment variables and configuration.
▪ DB: handling PostgreSQL connectivity and using GORM as ORM.
▪ Email: managing email notifications (SMTP service).
▪ Handlers: HTTP endpoints serving client requests.
▪ Models: defining data structures.
▪ URL Shortner: core business logic for creating and managing short URLs.
- Database:
- PostgreSQL database, set up via Docker Compose, hosting the URL shortner’s data.
- External Services:
- SMTP server for sending emails.
- (Implied) External clients/browsers that interact with the frontend.

##### Relationships and Interactions
- The frontend communicates with the backend through HTTP API endpoints. This is likely facilitated via the Chi router.
- The backend, after processing requests (like creating or retrieving shortened URLs, handling login, etc.), interacts with the PostgreSQL database using GORM.
- Authentication is managed through JWT tokens validated in both the frontend (possibly via protected routes) and verified on the backend.
- The email module triggers notifications via the SMTP service when certain events occur (such as a protected link being accessed).
- A configuration system (likely loaded at startup using Godotenv) ensures that both the backend and the email services function correctly with the required credentials and settings.

##### Architectural Patterns and Principles
- Separation of Concerns: Clear division between the frontend UI, backend business logic, and persistence (database).
- Modularization: Both frontend and backend have well-organized directory structures grouping related functionalities.
- MVC/Handler Pattern for Backend: Separate modules for models, handlers, and core logic.
- Use of Microservice Components: While this project is monolithic in terms of repository layout, internal modules (for auth, email, urlShortner) are designed to be distinct components handling different parts of the business logic.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Client Layer"
        reactApp["React App<br/>(React, Vite, TS, TailwindCSS)"]:::frontend
        pages["Pages: Home, Login, Register, Dashboard, Create URL"]:::frontend
        components["Components: UI Elements & Navigation"]:::frontend
        authContext["Auth Context Provider"]:::frontend
    end

    %% API Layer
    subgraph "API Layer"
        goServer["Go Server (Main Entry Point)"]:::backend
    end

    %% Backend Modules
    subgraph "Backend Modules"
        configModule["Config Module<br/>(Environment & Configuration)"]:::backend
        handlersModule["Handlers Module<br/>(HTTP Endpoints)"]:::backend
        authModule["Auth Module<br/>(User Authentication & JWT)"]:::backend
        urlShortenerModule["URL Shortener Module<br/>(Core Business Logic)"]:::backend
        modelsModule["Models Module<br/>(Data Structures)"]:::backend
        dbModule["Database Module<br/>(PostgreSQL Connectivity & GORM)"]:::backend
        emailModule["Email Module<br/>(SMTP Email Notifications)"]:::backend
    end

    %% Persistence Layer
    subgraph "Persistence Layer"
        postgres["PostgreSQL Database<br/>(Docker Compose)"]:::database
    end

    %% External Services
    subgraph "External Services"
        smtp["SMTP Email Service"]:::external
    end

    %% Connections
    reactApp -->|"HTTP_API"| goServer
    goServer -->|"loads_config"| configModule
    goServer -->|"routes"| handlersModule
    handlersModule -->|"auth_logic"| authModule
    handlersModule -->|"url_processing"| urlShortenerModule
    authModule -->|"uses_models"| modelsModule
    urlShortenerModule -->|"uses_models"| modelsModule
    authModule -->|"DB_access"| dbModule
    urlShortenerModule -->|"DB_access"| dbModule
    dbModule -->|"CRUD_ops"| postgres
    goServer -->|"triggers_email"| emailModule
    emailModule -->|"SMTP_call"| smtp

    %% Click Events
    click reactApp "https://github.com/viditagrawal56/url-shortner/tree/master/client/"
    click pages "https://github.com/viditagrawal56/url-shortner/tree/master/client/src/pages/"
    click components "https://github.com/viditagrawal56/url-shortner/tree/master/client/src/components/"
    click authContext "https://github.com/viditagrawal56/url-shortner/blob/master/client/src/context/AuthContext.tsx"
    click goServer "https://github.com/viditagrawal56/url-shortner/blob/master/server/cmd/main.go"
    click authModule "https://github.com/viditagrawal56/url-shortner/blob/master/server/internal/auth/userAuth.go"
    click configModule "https://github.com/viditagrawal56/url-shortner/blob/master/server/internal/config/config.go"
    click dbModule "https://github.com/viditagrawal56/url-shortner/blob/master/server/internal/db/db.go"
    click emailModule "https://github.com/viditagrawal56/url-shortner/blob/master/server/internal/email/emailService.go"
    click handlersModule "https://github.com/viditagrawal56/url-shortner/blob/master/server/internal/handlers/handlers.go"
    click modelsModule "https://github.com/viditagrawal56/url-shortner/blob/master/server/internal/models/models.go"
    click urlShortenerModule "https://github.com/viditagrawal56/url-shortner/blob/master/server/internal/urlShortner/urlShortner.go"
    click postgres "https://github.com/viditagrawal56/url-shortner/blob/master/server/docker-compose.dev.yaml"

    %% Styles
    classDef frontend fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef backend fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef database fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef external fill:#f8d7da,stroke:#721c24,stroke-width:2px;
```
