How apeacock1991/serverless-apps-on-cloudflare works
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: upload_image_for_analysis.ts Upload orchestration · index.ts AI analysis consumer · create_image.ts Photo handlers · image_store.ts Image store
Components 22
Worker services
Independent Cloudflare Worker examples.
- Authentication Worker
authentication-service/src/index.ts - Photo HTTP API
photo-service/src/index.ts - Photo handlers · Create, list, and detail request handlers.
photo-service/src/handlers/create_image.ts - Image store
photo-service/src/data/image_store.ts - Photo metadata schema
photo-service/migrations/0001_initial_creation.sql
Image applications
Pages-compatible image variants and async analysis flow.
- Simple image file API
image-app/src/app/api/files/route.ts - Simple image list
image-app/src/components/ImageList/index.tsx - Upload API
image-app-with-queue-producer/src/app/api/files/route.ts - Upload orchestration
image-app-with-queue-producer/src/lib/upload_image_for_analysis.ts - R2 object storage · File APIs use R2 as the object-storage boundary.
- Analysis queue
- AI analysis consumer
image-analysis-consumer/src/index.ts - Workers AI
- Analysis records schema
image-app-with-queue-producer/migrations/0001_create-image-analysis-table.sql - Analysis polling UI · 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
chat-app-durable-objects/src/index.ts - Chat room state · Serializes per-chat state and realtime coordination.
chat-app-durable-objects/src/chat.ts - Chat client
chat-app-durable-objects/src/app.html
Weather Pages app
UI isolates its external provider behind an API route.
- Location weather page
weather-app/src/app/weather/[location]/page.tsx - Weather API
weather-app/src/app/api/weather/route.ts - Weather provider adapter
weather-app/src/lib/get_external_weather_data.ts - External weather service
Connections 17
- 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