# goatninja10/contentfuse architecture

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

## Overview

This project is an AI SaaS platform built on Next.js 13 with a modern full‑stack architecture. It combines a rich user interface (React with TailwindCSS) with server-side API routes and database connectivity (via Prisma and MySQL on PlanetScale). In addition, it integrates several third‑party services for authentication (Clerk), payments (Stripe), and AI tools (OpenAI and Replicate for image, video, conversation, and music generation) while leveraging Crisp for live chat support.

##### Main Components
- Frontend/UI Layer:
- Next.js App: Contains groups for (landing), (auth), and (dashboard) pages.
- Reusable Components: Located in the “components” directory (e.g., Navbar, Sidebar, Modal, Loader, etc.) and UI sub-components.
- Styling: TailwindCSS-based UI design including animations and responsive layouts.
- API / Backend Layer:
- Route Handlers: Located in “app/api” (e.g., endpoints for code, conversation, image, music, video, stripe, and webhook).
- Server Components: Next.js Server-Side rendering that fetches data directly from the database.
- Authentication & User Management:
- Clerk Integration: Provides authentication flows between (auth) routes and protected resources (dashboard, account pages).
- Payment System:
- Stripe Integration: Manages monthly subscriptions and payment confirmations.
- Database & ORM:
- Prisma: Interfaces with the MySQL (PlanetScale) Database for persistent storage.
- AI & External Services:
- OpenAI APIs and Replicate AI: Implemented within API endpoints and lib modules to generate images, videos, conversation text, or music.
- Utilities & Middleware:
- Global utilities and subscriptions (in “lib”), API limits, and request error handling.
- Middleware used for authentication or request processing.
- Third-Party Chat:
- Crisp integration for live chat support.

##### Relationships and Interactions
- The User interacts with the Frontend UI (landing page, auth pages, dashboard).
- On authentication actions, the Clerk service is invoked, and routes in the (auth) group handle sign‑in/sign‑up flows.
- After authentication, the user navigates to the dashboard pages which render data fetched by server components (using Prisma) and call API routes for work (e.g., image generation or conversation generation).
- API route handlers (in “app/api”) act as mediation points between the Next.js server and external services (Stripe, OpenAI, Replicate).
- The API layer connects to the database via Prisma for persistent application state.
- Components in “lib” serve as the supporting infrastructure (e.g., stripe helpers, API limit checking, subscription management).

##### Architectural Patterns & Design Principles
- Next.js App Router Pattern: Modularized folder structure with group routes (e.g., (auth), (dashboard), (landing)), layouts, and nested pages.
- Separation of Concerns: Clearly separating UI (components), logic (hooks, lib), and API endpoints.
- Reusability and Composability: Use of reusable React components and layouts.
- External Service Integration: Encapsulating third‑party services (Stripe, Clerk, OpenAI, Replicate) within helper modules.
- Defensive Design: Error handling via middleware, toaster notifications and API rate limits.

b. Next.js application:
- Split it into sections: Landing, Authentication, and Dashboard.
- Inside the Dashboard group, include sub-modules for each API related feature (code, conversation, image, music, video, settings).
- Database Layer: Represent Prisma as the ORM and an underlying MySQL (PlanetScale) database.
e. Supportive elements for error handling, middleware, and shared libraries (lib, hooks, constants).

## Diagram (Mermaid)

```mermaid
graph TD
    %% Global Nodes
    USER["Client (Browser)"]:::ext

    %% Frontend/UI Layer
    subgraph "Frontend/UI Layer"
        LND["Landing Pages (Next.js)"]:::frontend
        AUTH["Auth Pages (Next.js)"]:::frontend
        DASH["Dashboard Pages (Next.js)"]:::frontend
        COMP["Reusable Components"]:::frontend
        UI["UI Sub‑components"]:::frontend
        STYLING["Styling (TailwindCSS)"]:::frontend
    end

    %% Authentication Service
    CLERK["Clerk Auth"]:::ext

    %% Server Components and API Layer
    subgraph "Backend/API Layer"
        SERV["Server Components"]:::api

        subgraph "API Endpoints"
            CODE["Code API"]:::api
            CONV["Conversation API"]:::api
            IMG["Image API"]:::api
            MUS["Music API"]:::api
            VIDEO["Video API"]:::api
            STRIPE["Stripe API"]:::api
            WEBHOOK["Webhook API"]:::api
        end
        UTIL["Utilities & Middleware"]:::util
    end

    %% Payment System
    STRIPEPAY["Stripe Payment"]:::ext

    %% Database & ORM
    PRISMA["Prisma ORM"]:::db
    DB["MySQL DB (PlanetScale)"]:::db

    %% AI & External Services
    AINSVC["AI Services"]:::ext

    %% Additional Utilities
    HOOKS["Hooks & Constants"]:::util

    %% Third-Party Chat
    CRISP["Crisp Chat"]:::ext

    %% Connections from Client
    USER -->|"Loads"| LND
    USER -->|"Loads"| AUTH
    USER -->|"Loads"| DASH

    %% Frontend Internal Connections
    AUTH -->|"Uses"| CLERK
    COMP -->|"Embeds"| CRISP

    %% Dashboard interactions
    DASH -->|"Renders"| SERV

    %% Server to API Layer
    SERV -->|"Calls"| CODE
    SERV -->|"Calls"| CONV
    SERV -->|"Calls"| IMG
    SERV -->|"Calls"| MUS
    SERV -->|"Calls"| VIDEO
    SERV -->|"Calls"| STRIPE
    SERV -->|"Calls"| WEBHOOK

    %% API to Utility and DB Connections
    CODE ---|"Middleware"| UTIL
    CONV ---|"Middleware"| UTIL
    IMG ---|"Middleware"| UTIL
    MUS ---|"Middleware"| UTIL
    VIDEO ---|"Middleware"| UTIL
    STRIPE ---|"Middleware"| UTIL
    WEBHOOK ---|"Middleware"| UTIL

    SERV -->|"DB Query"| PRISMA
    PRISMA -->|"Persistent Storage"| DB

    %% Payment Flow
    STRIPE -->|"Payment Confirmation"| STRIPEPAY

    %% AI Integrations
    IMG -->|"AI_Call"| AINSVC
    VIDEO -->|"AI_Call"| AINSVC
    CONV -->|"AI_Call"| AINSVC
    MUS -->|"AI_Call"| AINSVC

    %% Additional Utility Connections
    UTIL --- HOOKS

    %% Click Events for Frontend/UI Layer
    click LND "https://github.com/goatninja10/contentfuse/blob/main/app/(landing)/page.tsx"
    click LND "https://github.com/goatninja10/contentfuse/blob/main/app/(landing)/layout.tsx"
    click AUTH "https://github.com/goatninja10/contentfuse/blob/main/app/(auth)/(routes)/sign-in/[[...sign-in]]/page.tsx"
    click AUTH "https://github.com/goatninja10/contentfuse/blob/main/app/(auth)/(routes)/sign-up/[[...sign-up]]/page.tsx"
    click AUTH "https://github.com/goatninja10/contentfuse/blob/main/app/(auth)/layout.tsx"
    click DASH "https://github.com/goatninja10/contentfuse/blob/main/app/(dashboard)/(routes)/dashboard/page.tsx"
    click DASH "https://github.com/goatninja10/contentfuse/blob/main/app/(dashboard)/layout.tsx"
    click COMP "https://github.com/goatninja10/contentfuse/tree/main/components/"
    click UI "https://github.com/goatninja10/contentfuse/tree/main/components/ui/"
    click STYLING "https://github.com/goatninja10/contentfuse/blob/main/app/globals.css"
    click STYLING "https://github.com/goatninja10/contentfuse/blob/main/tailwind.config.js"

    %% Click Events for API / Backend Layer
    click CODE "https://github.com/goatninja10/contentfuse/blob/main/app/api/code/route.ts"
    click CONV "https://github.com/goatninja10/contentfuse/blob/main/app/api/conversation/route.ts"
    click IMG "https://github.com/goatninja10/contentfuse/blob/main/app/api/image/route.ts"
    click MUS "https://github.com/goatninja10/contentfuse/blob/main/app/api/music/route.ts"
    click VIDEO "https://github.com/goatninja10/contentfuse/blob/main/app/api/video/route.ts"
    click STRIPE "https://github.com/goatninja10/contentfuse/blob/main/app/api/stripe/route.ts"
    click WEBHOOK "https://github.com/goatninja10/contentfuse/blob/main/app/api/webhook/route.ts"
    click SERV "https://github.com/goatninja10/contentfuse/blob/main/app/layout.tsx"

    %% Click Events for Payment System
    click STRIPEPAY "https://github.com/goatninja10/contentfuse/blob/main/lib/stripe.ts"
    click STRIPEPAY "https://github.com/goatninja10/contentfuse/blob/main/lib/subscription.ts"

    %% Click Events for Database & ORM
    click PRISMA "https://github.com/goatninja10/contentfuse/blob/main/lib/prismadb.ts"
    click DB "https://github.com/goatninja10/contentfuse/blob/main/prisma/schema.prisma"

    %% Click Events for Utilities & Middleware
    click UTIL "https://github.com/goatninja10/contentfuse/blob/main/lib/api-limit.ts"
    click UTIL "https://github.com/goatninja10/contentfuse/blob/main/lib/utils.ts"
    click UTIL "https://github.com/goatninja10/contentfuse/blob/main/middleware.ts"
    click HOOKS "https://github.com/goatninja10/contentfuse/blob/main/hooks/use-pro-modal.tsx"
    click HOOKS "https://github.com/goatninja10/contentfuse/blob/main/constants.ts"

    %% Click Events for Third‑Party Chat
    click CRISP "https://github.com/goatninja10/contentfuse/blob/main/components/crisp-chat.tsx"
    click CRISP "https://github.com/goatninja10/contentfuse/blob/main/components/crisp-provider.tsx"

    %% Styles
    classDef frontend fill:#e3f2fd,stroke:#1976d2,stroke-width:2px;
    classDef api fill:#f3e5f5,stroke:#8e24aa,stroke-width:2px;
    classDef ext fill:#ffebee,stroke:#d32f2f,stroke-width:2px;
    classDef db fill:#e8f5e9,stroke:#388e3c,stroke-width:2px;
    classDef util fill:#fff3e0,stroke:#f57c00,stroke-width:2px;
```
