# dhruv-jangid/nextblog architecture

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

- Interactive diagram: https://gitdiagram.com/dhruv-jangid/nextblog
- Repository: https://github.com/dhruv-jangid/nextblog

## Overview

##### Project Type & Purpose
- MetaPress is a mono-repo full-stack blogging platform built with Next.js 16 (App Router), React 19, Bun, and Turborepo.
- It provides server-rendered pages, rich content creation (TipTap), advanced auth & moderation, admin dashboard, Redis caching, and AI integration (MCP).

##### High-Level File Structure & Architectural Patterns
- apps/web/
- src/app/   – Next.js App Router (page.tsx, layout.tsx, API routes under src/app/api/)
- src/components/   – Reusable UI & provider components
- src/core/   – Business logic services (auth, blog, comment, image, mail, mcp, cache, user)
- src/db/   – Drizzle ORM schema, relations, Snowflake ID generator
- src/lib/   – Utility functions (auth-client, image upload/NSFW check, profanity, slugify, Redis client)
- src/shared/   – Shared schemas & types for API contracts
- Turborepo orchestrates build/test tasks; Bun is the package manager/runtime.
- Next.js API routes act as serverless “controllers,” invoking core services and repositories.

##### Key Components
A. Frontend (Browser / Next.js)
- Next.js pages & Server Components (App Router)
- UI components & providers (Tailwind, shadcn/ui, TipTap editor)
- TensorFlow.js & NSFWJS models in public/models for client-side NSFW detection
- Calls backend via built-in API routes (HTTP fetch)

B. API Layer (Next.js Serverless Functions)
- Endpoints under src/app/api/:
- /api/auth/[...all] → Better Auth integration (sign-in/up, session)
- /api/cron/sync-likes → Scheduled/cron jobs for like count sync
- /api/[transport] → MCP HTTP/SSE/WS endpoints
- Each route invokes core services & uses Zod for request validation

C. Business Logic & Services (Core Layer)
- AuthService: user auth flows (email/password, OAuth, JWT sessions)
- BlogService, CommentService, ImageService, MailService, CacheService, MCPService, UserService
- Each service calls its repository & handles domain rules (rate-limit, profanity)

D. Data Layer
- Repositories (e.g., blog.repository.ts) using Drizzle ORM
- PostgreSQL (Neon or local) as primary DB
- Redis (optional/Upstash) for caching & session data

E. External Services
- Cloudinary for image upload/optimization & NSFW API
- Obscenity library for profanity filtering
- Model Context Protocol (MCP) via @modelcontextprotocol/sdk & @vercel/mcp-adapter
- Email via Nodemailer
- OAuth providers (Google, GitHub)
- Docker Compose wraps PostgreSQL & Redis for local dev

F. Infrastructure & Dev Tools
- Turborepo (monorepo orchestration)
- Bun: package manager, runtime, build (bun dev, bun build)
- Vercel for deployment, Docker Compose for local containers
- CI/Hooks: Husky, ESLint, TypeScript checks

##### Relationships & Data Flows
- Browser → Next.js page (SSR/SSG) → React Server/Client Components
- Browser (fetch) → Next.js API Routes → Core Services → Repositories → PostgreSQL
- Core Services ↔ Redis (cache reads/writes via CacheService)
- Core Services → External APIs (Cloudinary, Obscenity, MCP)
- Cron Endpoint → SyncLikes Service → DB
- Admin UI → Admin API endpoints → Core User/Blog services
- AI UI/Background → MCP endpoints (HTTP, SSE, WS)

##### Architectural Patterns & Principles
- Layered: Controllers (API routes) → Services → Repositories → DB
- Separation of concerns: UI vs. business logic vs. data access
- Serverless functions for APIs, functions as first-class citizens
- Monorepo with isolated workspaces (Turborepo)
- Infrastructure as code (Docker Compose, bunfig.toml, turbo.json)
- Cache-Aside pattern with Redis
- Model-View-Service pattern in core modules

##### Final Checklist
- [ ] Clear component labels
- [ ] Group modules by folder or layer
- [ ] Call out tech stack at each layer (Next.js 16, React 19, Drizzle, Bun, Turborepo)
- [ ] Note environment: local (Docker) vs. production (Vercel)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Browser
    Browser["Browser"]:::external

    subgraph "Frontend Layer"
        direction TB
        NextRouter["Next.js App Router"]:::frontend
        UIComponents["UI Components & Providers"]:::frontend
        NSFWModels["TensorFlow.js & NSFW Models"]:::frontend
    end

    %% API Layer
    subgraph "API Layer"
        direction TB
        AuthAPI["/api/auth/[...all]"]:::api
        CronAPI["/api/cron/sync-likes"]:::api
        MCPAPI["/api/[transport] (HTTP/SSE/WS)"]:::api
    end

    %% Service Layer
    subgraph "Core Services"
        direction TB
        AuthService["AuthService"]:::service
        BlogService["BlogService"]:::service
        CommentService["CommentService"]:::service
        ImageService["ImageService"]:::service
        MailService["MailService"]:::service
        CacheService["CacheService"]:::service
        MCPService["MCPService"]:::service
        UserService["UserService"]:::service
    end

    %% Data Layer
    subgraph "Data Layer"
        direction TB
        BlogRepo["BlogRepository"]:::service
        CommentRepo["CommentRepository"]:::service
        UserRepo["UserRepository"]:::service
        PostgreSQL["PostgreSQL"]:::datastore
        Redis["Redis"]:::redis
        DrizzleSchema["Drizzle ORM Schema & Relations"]:::datastore
        Snowflake["Snowflake ID Generator"]:::datastore
    end

    %% External Services
    subgraph "External Services"
        direction TB
        Cloudinary["Cloudinary"]:::external
        Obscenity["Obscenity Filter"]:::external
        MCP["Model Context Protocol"]:::external
        OAuth["OAuth Providers"]:::external
        EmailSvc["Nodemailer Email"]:::external
        DockerCompose["Docker Compose (Postgres & Redis)"]:::external
        Vercel["Vercel Deployment"]:::external
    end

    %% Dev Tools
    subgraph "Dev & Infra"
        direction TB
        Turborepo["Turborepo"]:::devtools
        Bun["Bun Runtime/Build"]:::devtools
        Husky["Husky Hooks"]:::devtools
        NextConfig["Next.js Config"]:::devtools
        DrizzleConfig["Drizzle Config"]:::devtools
    end

    %% Connections
    Browser -->|"renders SSR/SSG"| NextRouter
    NextRouter --> UIComponents
    UIComponents --> NSFWModels
    UIComponents -->|"fetch|HTTP"| AuthAPI
    UIComponents -->|"fetch|HTTP"| BlogService

    AuthAPI -->|"invokes"| AuthService
    CronAPI -->|"invokes"| BlogService
    MCPAPI -->|"invokes"| MCPService

    AuthService --> UserRepo
    BlogService --> BlogRepo
    CommentService --> CommentRepo
    UserService --> UserRepo
    ImageService --> Cloudinary
    ImageService --> NSFWModels
    MailService --> EmailSvc
    CacheService --> Redis
    MCPService --> MCP

    BlogRepo --> DrizzleSchema
    CommentRepo --> DrizzleSchema
    UserRepo --> DrizzleSchema
    DrizzleSchema --> PostgreSQL
    Snowflake --> PostgreSQL

    Redis -->|Cache-Aside| BlogService
    Redis -->|Cache-Aside| CommentService

    DockerCompose --> PostgreSQL
    DockerCompose --> Redis
    Vercel --> NextRouter

    Turborepo --> NextRouter
    Bun --> NextRouter
    Husky --> NextRouter
    NextConfig --> NextRouter
    DrizzleConfig --> DrizzleSchema

    %% Click Events
    click NextRouter "https://github.com/dhruv-jangid/nextblog/tree/master/apps/web/src/app"
    click UIComponents "https://github.com/dhruv-jangid/nextblog/tree/master/apps/web/src/components"
    click NSFWModels "https://github.com/dhruv-jangid/nextblog/tree/master/apps/web/public/models"
    click AuthAPI "https://github.com/dhruv-jangid/nextblog/blob/master/apps/web/src/app/api/auth/[...all]/route.ts"
    click CronAPI "https://github.com/dhruv-jangid/nextblog/blob/master/apps/web/src/app/api/cron/sync-likes/route.ts"
    click MCPAPI "https://github.com/dhruv-jangid/nextblog/blob/master/apps/web/src/app/api/[transport]/route.ts"
    click AuthService "https://github.com/dhruv-jangid/nextblog/blob/master/apps/web/src/core/auth/auth.service.ts"
    click BlogService "https://github.com/dhruv-jangid/nextblog/blob/master/apps/web/src/core/blog/blog.service.ts"
    click BlogRepo "https://github.com/dhruv-jangid/nextblog/blob/master/apps/web/src/core/blog/blog.repository.ts"
    click CommentService "https://github.com/dhruv-jangid/nextblog/blob/master/apps/web/src/core/comment/comment.service.ts"
    click CommentRepo "https://github.com/dhruv-jangid/nextblog/blob/master/apps/web/src/core/comment/comment.repository.ts"
    click ImageService "https://github.com/dhruv-jangid/nextblog/blob/master/apps/web/src/core/image/image.service.ts"
    click MailService "https://github.com/dhruv-jangid/nextblog/blob/master/apps/web/src/core/mail/mail.service.ts"
    click CacheService "https://github.com/dhruv-jangid/nextblog/blob/master/apps/web/src/core/cache/cache.service.ts"
    click MCPService "https://github.com/dhruv-jangid/nextblog/blob/master/apps/web/src/core/mcp/mcp.service.ts"
    click UserService "https://github.com/dhruv-jangid/nextblog/blob/master/apps/web/src/core/user/user.service.ts"
    click UserRepo "https://github.com/dhruv-jangid/nextblog/blob/master/apps/web/src/core/user/user.repository.ts"
    click authClient "https://github.com/dhruv-jangid/nextblog/blob/master/apps/web/src/lib/auth-client.ts"
    click checkNudity "https://github.com/dhruv-jangid/nextblog/blob/master/apps/web/src/lib/image/check-nudity.ts"
    click uploadImage "https://github.com/dhruv-jangid/nextblog/blob/master/apps/web/src/lib/image/upload-image.ts"
    click profanity "https://github.com/dhruv-jangid/nextblog/blob/master/apps/web/src/lib/profanity.ts"
    click slugify "https://github.com/dhruv-jangid/nextblog/blob/master/apps/web/src/lib/slugify.ts"
    click redisLib "https://github.com/dhruv-jangid/nextblog/blob/master/apps/web/src/lib/redis.ts"
    click utils "https://github.com/dhruv-jangid/nextblog/blob/master/apps/web/src/lib/utils.ts"
    click DrizzleSchema "https://github.com/dhruv-jangid/nextblog/blob/master/apps/web/src/db/schema.ts"
    click DrizzleRelations "https://github.com/dhruv-jangid/nextblog/blob/master/apps/web/src/db/relations.ts"
    click Snowflake "https://github.com/dhruv-jangid/nextblog/blob/master/apps/web/src/db/snowflake.ts"
    click DBIndex "https://github.com/dhruv-jangid/nextblog/blob/master/apps/web/src/db/index.ts"
    click Shared "https://github.com/dhruv-jangid/nextblog/tree/master/apps/web/src/shared"
    click DockerCompose "https://github.com/dhruv-jangid/nextblog/blob/master/apps/web/docker-compose.yml"
    click Turborepo "https://github.com/dhruv-jangid/nextblog/blob/master/turbo.json"
    click Bun "https://github.com/dhruv-jangid/nextblog/blob/master/bunfig.toml"
    click Husky "https://github.com/dhruv-jangid/nextblog/tree/master/ .husky/pre-commit"
    click NextConfig "https://github.com/dhruv-jangid/nextblog/blob/master/apps/web/next.config.ts"
    click DrizzleConfig "https://github.com/dhruv-jangid/nextblog/blob/master/apps/web/drizzle.config.ts"

    %% Styles
    classDef frontend fill:#B3D4FC,stroke:#0366d6,color:#0366d6;
    classDef api fill:#C6F6D5,stroke:#2F855A,color:#2F855A;
    classDef service fill:#FEEBC8,stroke:#DD6B20,color:#DD6B20;
    classDef datastore fill:#FBE5A6,stroke:#DD6B20,color:#8B4513;
    classDef redis fill:#E2E8F0,stroke:#718096,color:#2D3748;
    classDef external fill:#E9D8FD,stroke:#6B46C1,color:#6B46C1;
    classDef devtools fill:#E6FFFA,stroke:#2C7A7B,color:#2C7A7B;
```
