# aduboue/img-studio architecture

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

- Interactive diagram: https://gitdiagram.com/aduboue/img-studio
- Repository: https://github.com/aduboue/img-studio

## Overview

##### Main Components
- Frontend – The “app” directory contains page components, layouts, and UI components. There is a clear separation into “studio” pages (and their subdirectories for editing, generation, and library views) that provide the interactive user interface.
- API Endpoints – The “app/api” folder holds endpoints for various functionalities such as cloud storage actions, editing utilities, and third-party integrations (Google-auth, Imagen, Gemini, Vertex segmentation, Firestore actions, etc.). These endpoints serve as the bridge between the UI and backend services.
- Business Logic / Context – The “app/context” directory (with app-context.tsx) manages global state and likely supports communication between UI components and API routes.
- External Cloud Services:
- Cloud Build: Driven by “cloudbuild.yaml”, this component builds container images, triggered manually or on releases.
- Cloud Run: Hosts the containerized Next.js application. The README details deployment instructions for a Cloud Run service.
- Cloud Storage Buckets: Three buckets are used (for raw output content, shared library content, and configuration files).
- Firestore: Used as the database for storing application metadata with dedicated security rules.
- Identity-Aware Proxy (IAP) & OAuth: Provides secure access management to the deployed application.
- Load Balancer and Cloud DNS: Manage incoming traffic and SSL termination for the public facing endpoints.
- Artifact Registry: Hosts container images built via Cloud Build.

##### Out the Relationships and Interactions
- User Request Flow:
- A user’s web browser makes an HTTPS request.
- The request first goes through a Load Balancer (with SSL certification) and then via Cloud DNS.
- IAP intercepts the request to perform authentication before routing it to the Cloud Run service.
- Cloud Run Service:
- Once authenticated, the Cloud Run container (which runs the Next.js app) renders pages from the “studio” directory and serves API endpoints.
- The API endpoints invoke business logic (e.g., image editing or generation actions) and communicate with external services.
- External Communication:
- API endpoints call Google Cloud Storage for saving or retrieving generated images and configuration files.
- Integration with Vertex AI (Imagen, Gemini, and Vertex segmentation models) is triggered via the API endpoints to perform AI-based image processing.
- Firestore is queried for metadata storage and retrieval.
- Build Pipeline:
- Cloud Build is set up to monitor the GitHub repository and build the container image.
- Once built, the artifact is deployed to Cloud Run for serving the application.

##### Architectural Patterns and Technologies
- Separation of Concerns – The Next.js project cleanly separates UI components, API endpoints, and global context.
- Serverless Architecture – Cloud Run provides a managed container environment that scales based on traffic without requiring full infrastructure management.
- Microservices-like API Routing – The “app/api” folder organizes various API actions as individual endpoints which are then called by UI components.
- Integration with GCP Services – The design uses several GCP components (Cloud Build, Cloud Run, Firestore, Cloud Storage, IAP) coordinated to provide secure and scalable application services.
- CI/CD Pipeline – Cloud Build and related configuration files (Dockerfile, cloudbuild.yaml) suggest an automated build and deployment process.

##### Detail
- Your labels are specific – include role names and service names where appropriate (like the Cloud Run service, service accounts, etc.).
- Use annotations to indicate why certain components (like IAP or the Load Balancer) are configured in a particular way as per the infrastructure setup guide.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Request Flow
    Browser("Browser"):::frontend
    LB("Load Balancer & Cloud DNS (SSL)"):::security
    IAP("IAP & OAuth"):::security

    Browser -->|"HTTPS"| LB
    LB -->|"DNS Routing"| IAP
    IAP -->|"Auth Pass"| CloudRun

    %% Cloud Run Service: Next.js App
    subgraph "Cloud Run: Next.js App"
        direction TB
        Studio("Studio Pages (UI)"):::frontend
        click Studio "https://github.com/aduboue/img-studio/tree/main/app/(studio)"
        AppContext("Global Context (App Context)"):::backend
        click AppContext "https://github.com/aduboue/img-studio/blob/main/app/context/app-context.tsx"
        
        subgraph "API Endpoints" 
            direction TB
            CloudStorageActions("Cloud Storage Actions"):::backend
            click CloudStorageActions "https://github.com/aduboue/img-studio/tree/main/app/api/cloud-storage"
            
            EditUtilities("Edit Utilities"):::backend
            click EditUtilities "https://github.com/aduboue/img-studio/blob/main/app/api/edit-utils.tsx"
            
            ExportUtilities("Export Utilities"):::backend
            click ExportUtilities "https://github.com/aduboue/img-studio/blob/main/app/api/export-utils.tsx"
            
            FirestoreActions("Firestore Actions"):::backend
            click FirestoreActions "https://github.com/aduboue/img-studio/tree/main/app/api/firestore"
            
            GeminiIntegration("Gemini Integration"):::backend
            click GeminiIntegration "https://github.com/aduboue/img-studio/tree/main/app/api/gemini"
            
            ImagenIntegration("Imagen Integration"):::backend
            click ImagenIntegration "https://github.com/aduboue/img-studio/tree/main/app/api/imagen"
            
            VertexAISegmentation("Vertex AI Segmentation"):::backend
            click VertexAISegmentation "https://github.com/aduboue/img-studio/tree/main/app/api/vertex-seg"
        end
    end

    %% External Cloud Services
    subgraph "External Cloud Services"
        direction TB
        StorageBuckets("Cloud Storage Buckets"):::cloud
        FirestoreDB("Firestore Database"):::cloud
        VertexAI("Vertex AI Service"):::cloud
    end

    %% CI/CD and Configuration
    CloudBuild("Cloud Build Service"):::cicd
    click CloudBuild "https://github.com/aduboue/img-studio/blob/main/cloudbuild.yaml"
    
    Docker("Dockerfile"):::config
    click Docker "https://github.com/aduboue/img-studio/tree/main/Dockerfile"
    
    NextConfig("Next.js Config"):::config
    click NextConfig "https://github.com/aduboue/img-studio/blob/main/next.config.mjs"

    %% Connections: Deployment Pipeline
    CloudBuild -->|"Deploys"| CloudRun

    Docker ---|"Container Config"| CloudRun
    NextConfig ---|"App Config"| CloudRun

    %% Connections: API Endpoints to External Services
    CloudStorageActions -->|"save/retrieve"| StorageBuckets
    FirestoreActions -->|"query/modify"| FirestoreDB
    EditUtilities ---|"triggers"| VertexAI
    ExportUtilities ---|"triggers"| VertexAI
    GeminiIntegration ---|"calls"| VertexAI
    ImagenIntegration ---|"calls"| VertexAI
    VertexAISegmentation ---|"calls"| VertexAI

    %% Styles
    classDef frontend fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef backend fill:#FFC0CB,stroke:#000,stroke-width:2px;
    classDef cloud fill:#90EE90,stroke:#000,stroke-width:2px;
    classDef cicd fill:#FFD700,stroke:#000,stroke-width:2px;
    classDef security fill:#FFA07A,stroke:#000,stroke-width:2px;
    classDef config fill:#D3D3D3,stroke:#000,stroke-width:2px;

    %% Cloud Run Node
    CloudRun("Cloud Run: Next.js App"):::backend
    %% Ensure Cloud Run encloses the subgraph (implicit grouping)
    
    %% Connect subgraph components to Cloud Run node for clarity
    Studio --- CloudRun
    AppContext --- CloudRun
    CloudStorageActions --- CloudRun
    EditUtilities --- CloudRun
    ExportUtilities --- CloudRun
    FirestoreActions --- CloudRun
    GeminiIntegration --- CloudRun
    ImagenIntegration --- CloudRun
    VertexAISegmentation --- CloudRun

    %% Link Cloud Run with External Services (dashed for external call)
    CloudRun ---|"API Calls"| StorageBuckets
    CloudRun ---|"API Calls"| FirestoreDB
    CloudRun ---|"API Calls"| VertexAI
```
