# apeacock1991/serverless-apps-on-cloudflare architecture

> Architecture diagram of the GitHub repository apeacock1991/serverless-apps-on-cloudflare, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/apeacock1991/serverless-apps-on-cloudflare
- Repository: https://github.com/apeacock1991/serverless-apps-on-cloudflare

## Overview

#### Purpose and Repository Shape
This is a set of independent Cloudflare serverless reference projects, not one deployable application. Each top-level project owns its runtime, dependencies, and `wrangler.toml`; shared behavior is conceptual rather than implemented as a common library. The examples progressively cover Workers, Pages/Next.js, D1, Queues, R2, Workers AI, Durable Objects, and external API integration.

#### Worker API Services
Two standalone TypeScript Cloudflare Workers expose service boundaries:
- Authentication Worker entry point: `authentication-service/src/index.ts`
- Photo HTTP API entry point: `photo-service/src/index.ts`

The photo service routes requests into create/list/detail handlers (`photo-service/src/handlers/create_image.ts`, `photo-service/src/handlers/get_images.ts`, `photo-service/src/handlers/get_single_image.ts`), keeping HTTP routing separate from persistence access.

#### Photo Persistence Layer
`photo-service` models image metadata with a D1 migration and a dedicated data-access module:
- Schema: `photo-service/migrations/0001_initial_creation.sql`
- Store boundary: `photo-service/src/data/image_store.ts`
- Worker environment typing/configuration: `photo-service/src/env.ts`, `photo-service/wrangler.toml`

Handlers use this store rather than embedding database operations, making D1 the material persistence boundary for photo metadata.

#### Image Analysis Pipeline
The queue-enabled image application is the clearest multi-stage flow:
1. A Next.js/Cloudflare Pages UI submits uploads through `image-app-with-queue-producer/src/app/api/files/route.ts`.
2. Upload and analysis-request logic is isolated in `image-app-with-queue-producer/src/lib/upload_image_for_analysis.ts`.
3. A queue consumer processes asynchronous analysis in `image-analysis-consumer/src/index.ts`, intended to use Cloudflare Workers AI.
4. Analysis results are persisted in the producer app’s D1 schema: `image-app-with-queue-producer/migrations/0001_create-image-analysis-table.sql`.
5. The browser polls for completed records via `image-app-with-queue-producer/src/hooks/use_poll_for_analysis.ts` and `src/lib/retrieve_image_analysis_query.ts`.

This separates request-time file ingestion from potentially slow AI processing through Cloudflare Queues.

#### Image Application Variants and Storage
`image-app` is a simpler image frontend/API variant, with its server entry route at `image-app/src/app/api/files/route.ts` and display component at `image-app/src/components/ImageList/index.tsx`. The queue-producing variant adds upload orchestration and polling (`image-app-with-queue-producer/src/hooks/use_handle_image_upload.ts`, `src/components/ImageList/index.tsx`). These applications are Next.js projects configured for Cloudflare deployment through their respective `wrangler.toml` files; R2 is the likely object-storage boundary represented by their file APIs and Cloudflare-focused setup.

#### Durable-Object Chat
The chat example combines a Worker HTTP/WebSocket entry with per-chat durable state:
- Entry/router: `chat-app-durable-objects/src/index.ts`
- Durable Object implementation: `chat-app-durable-objects/src/chat.ts`
- Client UI asset: `chat-app-durable-objects/src/app.html`

Requests connect clients to a Durable Object instance, which is the serialization and coordination boundary for chat-room state and real-time communication.

#### Weather Pages Application
`weather-app` is a Next.js frontend with both a server API route and parameterized page:
- Weather API: `weather-app/src/app/api/weather/route.ts`
- External-provider adapter: `weather-app/src/lib/get_external_weather_data.ts`
- Location page: `weather-app/src/app/weather/[location]/page.tsx`

The API route/adaptor boundary prevents UI components from directly depending on the external weather service, while Cloudflare deployment configuration lives in `weather-app/wrangler.toml`.

#### Runtime and Infrastructure Boundaries
Cloudflare Workers execute the service, consumer, and Durable Object examples; Cloudflare Pages-compatible Next.js applications provide the image and weather UIs. Per-project `wrangler.toml` files bind deployed resources such as D1, Queues, R2, Durable Objects, KV/cache, or AI according to the individual example. Browser clients communicate through route handlers or Worker endpoints; stateful and asynchronous concerns are delegated to Cloudflare-managed infrastructure rather than a central backend.

Main entry points: [`image-app-with-queue-producer/src/lib/upload_image_for_analysis.ts`](https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/image-app-with-queue-producer/src/lib/upload_image_for_analysis.ts) (Upload orchestration), [`image-analysis-consumer/src/index.ts`](https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/image-analysis-consumer/src/index.ts) (AI analysis consumer), [`photo-service/src/handlers/create_image.ts`](https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/photo-service/src/handlers/create_image.ts) (Photo handlers), [`photo-service/src/data/image_store.ts`](https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/photo-service/src/data/image_store.ts) (Image store)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_worker_services["Worker services"]
  node_auth_worker["Authentication Worker<br/>Worker entry<br/>[index.ts]"]
  node_photo_api["Photo HTTP API<br/>Worker entry<br/>[index.ts]"]
  node_photo_handlers["Photo handlers<br/>HTTP handlers<br/>[create_image.ts]"]
  node_photo_store["Image store<br/>D1 data access<br/>[image_store.ts]"]
  node_photo_schema["Photo metadata schema<br/>D1 migration"]
end

subgraph group_image_pipeline["Image applications"]
  node_simple_image_api["Simple image file API<br/>Next.js route<br/>[route.ts]"]
  node_simple_image_list["Simple image list<br/>UI component<br/>[index.tsx]"]
  node_upload_route["Upload API<br/>Next.js route<br/>[route.ts]"]
  node_upload_orchestrator["Upload orchestration<br/>upload and queue logic"]
  node_r2[("R2 object storage<br/>Cloudflare storage")]
  node_analysis_queue["Analysis queue<br/>Cloudflare Queue"]
  node_analysis_consumer["AI analysis consumer<br/>queue Worker<br/>[index.ts]"]
  node_workers_ai{{"Workers AI<br/>Cloudflare AI"}}
  node_analysis_schema["Analysis records schema<br/>D1 migration"]
  node_polling_ui["Analysis polling UI<br/>hook and query"]
end

subgraph group_chat["Durable chat"]
  node_chat_entry["Chat Worker<br/>HTTP/WebSocket entry<br/>[index.ts]"]
  node_chat_room[("Chat room state<br/>Durable Object<br/>[chat.ts]")]
  node_chat_client["Chat client<br/>HTML UI asset<br/>[app.html]"]
end

subgraph group_weather["Weather Pages app"]
  node_weather_page["Location weather page<br/>Next.js page<br/>[page.tsx]"]
  node_weather_api["Weather API<br/>Next.js route<br/>[route.ts]"]
  node_weather_adapter["Weather provider adapter<br/>external API adapter"]
  node_weather_provider{{"External weather service<br/>external API"}}
end

node_photo_api -->|"routes requests"| node_photo_handlers
node_photo_handlers -->|"uses"| node_photo_store
node_photo_store -->|"reads and writes"| node_photo_schema
node_simple_image_list -->|"loads files"| node_simple_image_api
node_simple_image_api -->|"stores files"| node_r2
node_upload_route -->|"delegates upload"| node_upload_orchestrator
node_upload_orchestrator -->|"stores upload"| node_r2
node_upload_orchestrator -->|"enqueues analysis"| node_analysis_queue
node_analysis_queue -->|"delivers job"| node_analysis_consumer
node_analysis_consumer -->|"runs inference"| node_workers_ai
node_analysis_consumer -->|"persists result"| node_analysis_schema
node_polling_ui -->|"polls records"| node_analysis_schema
node_chat_client -->|"HTTP and WebSocket"| node_chat_entry
node_chat_entry -->|"connects instance"| node_chat_room
node_weather_page -->|"requests weather"| node_weather_api
node_weather_api -->|"delegates provider access"| node_weather_adapter
node_weather_adapter -->|"fetches forecast"| node_weather_provider

click node_auth_worker "https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/authentication-service/src/index.ts"
click node_photo_api "https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/photo-service/src/index.ts"
click node_photo_handlers "https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/photo-service/src/handlers/create_image.ts"
click node_photo_store "https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/photo-service/src/data/image_store.ts"
click node_photo_schema "https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/photo-service/migrations/0001_initial_creation.sql"
click node_simple_image_api "https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/image-app/src/app/api/files/route.ts"
click node_simple_image_list "https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/image-app/src/components/ImageList/index.tsx"
click node_upload_route "https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/image-app-with-queue-producer/src/app/api/files/route.ts"
click node_upload_orchestrator "https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/image-app-with-queue-producer/src/lib/upload_image_for_analysis.ts"
click node_analysis_consumer "https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/image-analysis-consumer/src/index.ts"
click node_analysis_schema "https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/image-app-with-queue-producer/migrations/0001_create-image-analysis-table.sql"
click node_polling_ui "https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/image-app-with-queue-producer/src/hooks/use_poll_for_analysis.ts"
click node_chat_entry "https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/chat-app-durable-objects/src/index.ts"
click node_chat_room "https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/chat-app-durable-objects/src/chat.ts"
click node_chat_client "https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/chat-app-durable-objects/src/app.html"
click node_weather_page "https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/weather-app/src/app/weather/%5Blocation%5D/page.tsx"
click node_weather_api "https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/weather-app/src/app/api/weather/route.ts"
click node_weather_adapter "https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/weather-app/src/lib/get_external_weather_data.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_auth_worker,node_photo_api,node_photo_handlers,node_photo_store,node_photo_schema toneBlue
class node_simple_image_api,node_simple_image_list,node_upload_route,node_upload_orchestrator,node_r2,node_analysis_queue,node_analysis_consumer,node_workers_ai,node_analysis_schema,node_polling_ui toneAmber
class node_chat_entry,node_chat_room,node_chat_client toneMint
class node_weather_page,node_weather_api,node_weather_adapter,node_weather_provider toneRose
```

## Components

### Worker services

Independent Cloudflare Worker examples.

- [Authentication Worker](https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/authentication-service/src/index.ts): Worker entry · `authentication-service/src/index.ts`
- [Photo HTTP API](https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/photo-service/src/index.ts): Worker entry · `photo-service/src/index.ts`
- [Photo handlers](https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/photo-service/src/handlers/create_image.ts): HTTP handlers · Create, list, and detail request handlers. · `photo-service/src/handlers/create_image.ts`
- [Image store](https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/photo-service/src/data/image_store.ts): D1 data access · `photo-service/src/data/image_store.ts`
- [Photo metadata schema](https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/photo-service/migrations/0001_initial_creation.sql): D1 migration · `photo-service/migrations/0001_initial_creation.sql`

### Image applications

Pages-compatible image variants and async analysis flow.

- [Simple image file API](https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/image-app/src/app/api/files/route.ts): Next.js route · `image-app/src/app/api/files/route.ts`
- [Simple image list](https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/image-app/src/components/ImageList/index.tsx): UI component · `image-app/src/components/ImageList/index.tsx`
- [Upload API](https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/image-app-with-queue-producer/src/app/api/files/route.ts): Next.js route · `image-app-with-queue-producer/src/app/api/files/route.ts`
- [Upload orchestration](https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/image-app-with-queue-producer/src/lib/upload_image_for_analysis.ts): upload and queue logic · `image-app-with-queue-producer/src/lib/upload_image_for_analysis.ts`
- **R2 object storage**: Cloudflare storage · File APIs use R2 as the object-storage boundary.
- **Analysis queue**: Cloudflare Queue
- [AI analysis consumer](https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/image-analysis-consumer/src/index.ts): queue Worker · `image-analysis-consumer/src/index.ts`
- **Workers AI**: Cloudflare AI
- [Analysis records schema](https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/image-app-with-queue-producer/migrations/0001_create-image-analysis-table.sql): D1 migration · `image-app-with-queue-producer/migrations/0001_create-image-analysis-table.sql`
- [Analysis polling UI](https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/image-app-with-queue-producer/src/hooks/use_poll_for_analysis.ts): hook and query · Polls completed analysis records for the browser. · `image-app-with-queue-producer/src/hooks/use_poll_for_analysis.ts`

### Durable chat

WebSocket chat with per-room durable state.

- [Chat Worker](https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/chat-app-durable-objects/src/index.ts): HTTP/WebSocket entry · `chat-app-durable-objects/src/index.ts`
- [Chat room state](https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/chat-app-durable-objects/src/chat.ts): Durable Object · Serializes per-chat state and realtime coordination. · `chat-app-durable-objects/src/chat.ts`
- [Chat client](https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/chat-app-durable-objects/src/app.html): HTML UI asset · `chat-app-durable-objects/src/app.html`

### Weather Pages app

UI isolates its external provider behind an API route.

- [Location weather page](https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/weather-app/src/app/weather/%5Blocation%5D/page.tsx): Next.js page · `weather-app/src/app/weather/[location]/page.tsx`
- [Weather API](https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/weather-app/src/app/api/weather/route.ts): Next.js route · `weather-app/src/app/api/weather/route.ts`
- [Weather provider adapter](https://github.com/apeacock1991/serverless-apps-on-cloudflare/blob/main/weather-app/src/lib/get_external_weather_data.ts): external API adapter · `weather-app/src/lib/get_external_weather_data.ts`
- **External weather service**: external API

## Connections

- Photo HTTP API → Photo handlers: routes requests
- Photo handlers → Image store: uses
- Image store → Photo metadata schema: reads and writes
- Simple image list → Simple image file API: loads files
- Simple image file API → R2 object storage: stores files
- Upload API → Upload orchestration: delegates upload
- Upload orchestration → R2 object storage: stores upload
- Upload orchestration → Analysis queue: enqueues analysis
- Analysis queue → AI analysis consumer: delivers job
- AI analysis consumer → Workers AI: runs inference
- AI analysis consumer → Analysis records schema: persists result
- Analysis polling UI → Analysis records schema: polls records
- Chat client → Chat Worker: HTTP and WebSocket
- Chat Worker → Chat room state: connects instance
- Location weather page → Weather API: requests weather
- Weather API → Weather provider adapter: delegates provider access
- Weather provider adapter → External weather service: fetches forecast
