# michniks2001/ingredibud architecture

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

- Interactive diagram: https://gitdiagram.com/michniks2001/ingredibud
- Repository: https://github.com/michniks2001/ingredibud

## Overview

##### Project Type and Purpose
- Full-stack web application built with Next.js (App Router) and TypeScript.
- Purpose: AI-powered recipe generator (“Ingredibud”) that leverages Google Search + Gemini AI to fetch grounding sources and synthesize beginner-friendly recipes.
- Includes user authentication, recipe browsing, and a responsive, animated UI.

##### High-Level Components
- Frontend (UI)
– Next.js App Router pages in `app/` (e.g., `/`, `/recipes`, `/about`, `/faq`, `/login`, `/signup`, `/dashboard`)
– Reusable React components in `components/` (navbar, recipe-form)
– Global CSS (Tailwind) in `app/globals.css`
– Auth state management via React Context in `contexts/auth-context.tsx`

- API Layer (Backend Routes)
– Next.js API routes under `app/api/`:
· `auth` (login, logout, signup, me)
· `generate-recipe` (calls Google Search + Gemini AI)
· `recipes` (list & single recipe endpoints)
– Each route implements server-side logic, calls helper functions in `lib/`, and interacts with the database.

- Services & Libraries
– `lib/googleSearch` (built-in in `lib/auth.ts`, `lib/markdown.ts`, `lib/mongodb.ts`)
– MongoDB driver & connection logic in `lib/mongodb.ts`
– Markdown sanitization & rendering in `lib/markdown.ts`
– Auth helper functions (token creation, cookie handling) in `lib/auth.ts`

- External Systems
– Google Search API or scraping layer for grounding sources
– Google Generative AI (Gemini) via `GEMINI_API_KEY`
– MongoDB Atlas (or self-hosted) for user and recipe storage
– Vercel (deployment platform)

##### Interactions and Data Flow
1. User (Browser)
- Sends HTTP(S) request to Next.js server (page load, navigation)
- Interacts with client-side React UI
- Triggers API calls (e.g., form submission for login/signup, recipe generation)
2. Next.js Server / API Routes
- SSR/SSG for pages; client-side JS hydration
- API route receives request → validates/authenticates (via cookies/context) → calls relevant `lib/` functions
- For recipe generation:
a. Call Google Search to fetch URLs → normalize URLs → fetch content
b. Pass grounding sources + user inputs to Gemini AI → receive recipe in Markdown
c. Sanitize + convert to HTML → return JSON payload
- For auth: create JWT or session cookie → store user record in MongoDB → return user data
- For recipes CRUD: read/write from MongoDB
3. Client-Side State
- `auth-context` stores current user, provides login/logout/signup wrappers
- UI components render based on auth state and API responses

##### Architectural Patterns & Principles
- Separation of Concerns: UI ↔ API ↔ Data
- React Context for cross-cutting auth state
- Next.js App Router for file-based routing (pages & API)
- Layered Helpers in `lib/` for DB, auth, markdown
- External service integration abstracted behind API routes
- Responsive design (mobile-first with Tailwind)

##### Putting It All Together
- Every critical component (pages, components, context, routes, lib, external services, DB) appears.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global Entities
    browser["Browser"]:::external

    %% Next.js App Layer
    subgraph "Next.js App Server"
        direction TB
        subgraph "UI Layer"
            direction TB
            ui["Next.js UI (SSR/CSR)"]:::frontend
            subgraph "Pages"
                direction TB
                home["Home Page"]:::frontend
                about["About Page"]:::frontend
                recipesList["Recipes Listing Page"]:::frontend
                faq["FAQ Page"]:::frontend
                loginPage["Login Page"]:::frontend
                signupPage["Signup Page"]:::frontend
                dashboard["Dashboard Page"]:::frontend
                layout["App Layout & Globals"]:::frontend
            end
            subgraph "Components"
                direction TB
                navbar["Navigation Bar"]:::frontend
                recipeForm["Recipe Form Component"]:::frontend
            end
            authContext["Auth Context"]:::frontend
        end

        subgraph "API Layer"
            direction TB
            subgraph "Auth Routes"
                direction TB
                authLogin["Login API"]:::api
                authLogout["Logout API"]:::api
                authSignup["Signup API"]:::api
                authMe["Get Current User API"]:::api
            end
            genRecipe["Generate Recipe API"]:::api
            subgraph "Recipe CRUD Routes"
                direction TB
                recipesListCreate["List/Create Recipes API"]:::api
                recipeItem["Get/Update/Delete Recipe API"]:::api
            end
        end
    end

    %% Libraries
    subgraph "Lib Helpers"
        direction TB
        authLib["lib/auth.ts"]:::lib
        markdownLib["lib/markdown.ts"]:::lib
        mongoLib["lib/mongodb.ts"]:::lib
    end

    %% External Services & DB
    googleSearch["Google Search Service"]:::external
    geminiAI["Gemini AI Service"]:::external
    mongoDB["MongoDB Atlas"]:::database

    %% Connections
    browser -->|"HTTP GET/POST"| ui
    ui -->|"API Call"| authLogin
    ui -->|"API Call"| authLogout
    ui -->|"API Call"| authSignup
    ui -->|"API Call"| authMe
    ui -->|"API Call"| genRecipe
    ui -->|"API Call"| recipesListCreate
    ui -->|"API Call"| recipeItem

    authLogin -->|"calls"| authLib
    authLogout -->|"calls"| authLib
    authSignup -->|"calls"| authLib
    authMe -->|"calls"| authLib

    genRecipe -->|"uses"| googleSearch
    genRecipe -->|"uses"| geminiAI
    genRecipe -->|"sanitizes via"| markdownLib

    recipesListCreate -->|"reads/writes"| mongoDB
    recipeItem -->|"reads/writes"| mongoDB
    authLogin -->|"writes"| mongoDB
    authSignup -->|"writes"| mongoDB
    authMe -->|"reads"| mongoDB

    %% UI internals
    ui --> home
    ui --> about
    ui --> recipesList
    ui --> faq
    ui --> loginPage
    ui --> signupPage
    ui --> dashboard
    ui --> layout
    ui --> navbar
    ui --> recipeForm
    ui --> authContext

    %% Responses
    genRecipe -->|"JSON Response"| ui
    authLogin -->|"JSON Response + Cookie"| ui
    authSignup -->|"JSON Response + Cookie"| ui
    authMe -->|"JSON Response"| ui
    recipesListCreate -->|"JSON Response"| ui
    recipeItem -->|"JSON Response"| ui

    %% Click Events
    click home "https://github.com/michniks2001/ingredibud/blob/main/app/page.tsx"
    click about "https://github.com/michniks2001/ingredibud/blob/main/app/about/page.tsx"
    click recipesList "https://github.com/michniks2001/ingredibud/blob/main/app/recipes/page.tsx"
    click faq "https://github.com/michniks2001/ingredibud/blob/main/app/faq/page.tsx"
    click loginPage "https://github.com/michniks2001/ingredibud/blob/main/app/login/page.tsx"
    click signupPage "https://github.com/michniks2001/ingredibud/blob/main/app/signup/page.tsx"
    click dashboard "https://github.com/michniks2001/ingredibud/blob/main/app/dashboard/page.tsx"
    click layout "https://github.com/michniks2001/ingredibud/blob/main/app/layout.tsx"
    click navbar "https://github.com/michniks2001/ingredibud/blob/main/components/navbar.tsx"
    click recipeForm "https://github.com/michniks2001/ingredibud/blob/main/components/recipe-form.tsx"
    click authContext "https://github.com/michniks2001/ingredibud/blob/main/contexts/auth-context.tsx"
    click authLogin "https://github.com/michniks2001/ingredibud/blob/main/app/api/auth/login/route.ts"
    click authLogout "https://github.com/michniks2001/ingredibud/blob/main/app/api/auth/logout/route.ts"
    click authSignup "https://github.com/michniks2001/ingredibud/blob/main/app/api/auth/signup/route.ts"
    click authMe "https://github.com/michniks2001/ingredibud/blob/main/app/api/auth/me/route.ts"
    click genRecipe "https://github.com/michniks2001/ingredibud/blob/main/app/api/generate-recipe/route.ts"
    click recipesListCreate "https://github.com/michniks2001/ingredibud/blob/main/app/api/recipes/route.ts"
    click recipeItem "https://github.com/michniks2001/ingredibud/blob/main/app/api/recipes/[id]/route.ts"
    click authLib "https://github.com/michniks2001/ingredibud/blob/main/lib/auth.ts"
    click markdownLib "https://github.com/michniks2001/ingredibud/blob/main/lib/markdown.ts"
    click mongoLib "https://github.com/michniks2001/ingredibud/blob/main/lib/mongodb.ts"

    %% Styles
    classDef frontend fill:#AED6F1,stroke:#21618C,stroke-width:1px
    classDef api fill:#A9DFBF,stroke:#196F3D,stroke-width:1px
    classDef lib fill:#F9E79F,stroke:#B9770E,stroke-width:1px
    classDef external fill:#F5B041,stroke:#B9770E,stroke-width:1px
    classDef database fill:#D2B4DE,stroke:#6C3483,stroke-width:1px
```
