# ipenywis/roastui architecture

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

- Interactive diagram: https://gitdiagram.com/ipenywis/roastui
- Repository: https://github.com/ipenywis/roastui

## Overview

##### Main System Components

a) Client Layer:
- Next.js Frontend (src/app directory)
- Preview System (src/app/(preview))
- Authentication UI (src/app/(main)/(auth))
- Component Library (src/components)

b) API Layer:
- Authentication API (src/app/api/auth)
- Roast API (src/app/api/roast)
- Subscription API (src/app/api/subscribe)
- Webhook Handlers (src/app/api/webhooks)

c) Core Services:
- AI Service Integration (src/lib/ai.ts)
- Image Processing Service (src/services/imageService.ts)
- Payment Service (src/services/paymentService.ts)
- User Service (src/services/userService.ts)

d) Infrastructure:
- SQLite Database with LiteFS
- Cloudflare R2 Storage
- OpenRouter AI Integration
- esbuild WASM Compiler

##### Component Relationships

a) Frontend Flow:
**User Interface → API Layer → Core Services → External Services**

b) Data Flow:
**Client → API Routes → Services → Database/Storage**

c) AI Processing Flow:
**Design Upload → Image Processing → AI Analysis → Preview Generation**

##### Special Architectural Elements

Unique aspects:

a) Preview System:
- Browser-based Code Compilation (src/lib/bundler)
- Real-time Preview Rendering (src/components/previewRenderer)
- Streaming AI Responses (src/app/api/roast-streaming)

b) State Management:
- Design Preview Store (src/lib/stores/designPreviewStore.ts)
- Preview View Store (src/lib/stores/previewViewStore.ts)

##### Key Integration Points

Crucial connections:

- Authentication flow with NextAuth
- OpenRouter AI model integration
- Cloudflare R2 storage integration
- esbuild WASM compilation system
- Payment processing with Stripe

##### Deployment Architecture

A section showing:

- Fly.io VPS deployment
- Multi-region distribution
- LiteFS database replication
- Static asset delivery

## Diagram (Mermaid)

```mermaid
graph TB
    %% Styles
    classDef frontend fill:#3498db,stroke:#2980b9,color:white
    classDef api fill:#2ecc71,stroke:#27ae60,color:white
    classDef service fill:#f1c40f,stroke:#f39c12,color:black
    classDef infra fill:#e74c3c,stroke:#c0392b,color:white
    classDef external fill:#9b59b6,stroke:#8e44ad,color:white

    %% Infrastructure Layer
    subgraph Infrastructure
        DB[SQLite + LiteFS]:::infra
        R2[Cloudflare R2]:::infra
        AI[OpenRouter AI]:::external
        WASM[esbuild WASM]:::infra
    end

    %% Core Services Layer
    subgraph CoreServices
        AIS[AI Service]:::service
        IPS[Image Processing]:::service
        PS[Payment Service]:::service
        US[User Service]:::service
        BCC[Browser Code Compiler]:::service
        DPS[Design Preview Store]:::service
        PVS[Preview View Store]:::service
    end

    %% API Layer
    subgraph APILayer
        AuthAPI[Authentication API]:::api
        RoastAPI[Roast API]:::api
        SubAPI[Subscription API]:::api
        WebhookAPI[Webhook Handlers]:::api
        StreamAPI[Streaming API]:::api
    end

    %% Client Layer
    subgraph ClientLayer
        Frontend[Next.js Frontend]:::frontend
        Preview[Preview System]:::frontend
        AuthUI[Authentication UI]:::frontend
        CompLib[Component Library]:::frontend
        PRenderer[Preview Renderer]:::frontend
    end

    %% Relationships
    Frontend --> AuthAPI & RoastAPI & SubAPI
    Preview --> StreamAPI
    AuthUI --> AuthAPI
    RoastAPI --> AIS
    AIS --> AI
    IPS --> R2
    PS --> WebhookAPI
    BCC --> WASM
    PRenderer --> BCC
    StreamAPI --> AIS
    AuthAPI --> US
    US --> DB
    DPS --> PRenderer
    PVS --> Preview

    %% Click Events
    click Frontend "https://github.com/ipenywis/roastui/tree/main/src/app/(main)"
    click Preview "https://github.com/ipenywis/roastui/tree/main/src/app/(preview)"
    click AuthUI "https://github.com/ipenywis/roastui/tree/main/src/app/(main)/(auth)"
    click CompLib "https://github.com/ipenywis/roastui/tree/main/src/components"
    click AuthAPI "https://github.com/ipenywis/roastui/blob/main/src/app/api/auth/[...nextauth]"
    click RoastAPI "https://github.com/ipenywis/roastui/tree/main/src/app/api/roast"
    click SubAPI "https://github.com/ipenywis/roastui/tree/main/src/app/api/subscribe"
    click WebhookAPI "https://github.com/ipenywis/roastui/tree/main/src/app/api/webhooks"
    click AIS "https://github.com/ipenywis/roastui/blob/main/src/lib/ai.ts"
    click IPS "https://github.com/ipenywis/roastui/blob/main/src/services/imageService.ts"
    click PS "https://github.com/ipenywis/roastui/blob/main/src/services/paymentService.ts"
    click US "https://github.com/ipenywis/roastui/blob/main/src/services/userService.ts"
    click BCC "https://github.com/ipenywis/roastui/tree/main/src/lib/bundler"
    click PRenderer "https://github.com/ipenywis/roastui/tree/main/src/components/previewRenderer"
    click StreamAPI "https://github.com/ipenywis/roastui/tree/main/src/app/api/roast-streaming"
    click DPS "https://github.com/ipenywis/roastui/blob/main/src/lib/stores/designPreviewStore.ts"
    click PVS "https://github.com/ipenywis/roastui/blob/main/src/lib/stores/previewViewStore.ts"

    %% Legend
    subgraph Legend
        F[Frontend]:::frontend
        A[API]:::api
        S[Service]:::service
        I[Infrastructure]:::infra
        E[External]:::external
    end
```
