# tangsy/edge-next-starter architecture

> Architecture diagram of the GitHub repository tangsy/edge-next-starter, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tangsy/edge-next-starter
- Repository: https://github.com/tangsy/edge-next-starter

## Overview

#### Purpose and Runtime
This is a production-oriented full-stack starter for Next.js App Router workloads deployed as a Cloudflare Worker through vinext/Vite. It provides edge-compatible application patterns for authentication, D1 persistence, KV caching/rate limiting, R2 uploads, Stripe billing, GDPR, and observability.

Worker bootstrap and deployment bindings are centered on `worker/index.ts`, `wrangler.toml`, and `wrangler.prod.toml`.

#### Application Entry Points
The UI is locale-scoped App Router content, rooted by `app/layout.tsx` and `app/[locale]/layout.tsx`; locale routing/request configuration lives in `i18n/routing.ts` and `i18n/request.ts`. Primary product screens include dashboard, profile, billing, pricing, and upload under `app/[locale]/`.

HTTP endpoints enter through route handlers in `app/api/`, including health, registration, auth, users/posts, uploads, GDPR, monitoring, and Stripe. The API boundary is designed for Edge Runtime execution rather than a separate backend service.

#### Authentication and User Lifecycle
better-auth is integrated behind auth libraries and API routes: `lib/auth/index.ts`, `lib/auth/session.ts`, and `app/api/auth/[...all]/route.ts`. Registration has a dedicated endpoint at `app/api/register/route.ts`, while server-side UI auth actions are in `app/actions/auth.ts`.

User data moves from handlers/services to repositories and D1. User-facing account pages are `app/[locale]/login/page.tsx`, `app/[locale]/register/page.tsx`, and `app/[locale]/profile/page.tsx`. Google OAuth and credential authentication are intended external identity paths.

#### API, Domain, and Persistence Boundary
Route handlers should use the common API layer for middleware, database access, rate limiting, and response conventions: `lib/api/middleware.ts`, `lib/api/database.ts`, and `lib/api/response.ts`. Domain orchestration is separated into services such as `services/auth.service.ts` and `services/upload.service.ts`.

Repositories isolate database operations from HTTP/domain code: `repositories/user.repository.ts`, `repositories/post.repository.ts`, and `repositories/subscription.repository.ts`. Prisma/D1 access is established by `lib/db/client.ts`; durable schema evolution is represented in `prisma/schema.prisma` and ordered SQL migrations under `migrations/`.

#### Cloudflare Data Services and Upload Flow
D1 is the relational source of truth. KV supports cache and likely request limiting through `lib/cache/client.ts`, `lib/rate-limiter/index.ts`, and `app/api/rate-limit/route.ts`. R2 object operations are isolated in `lib/r2/client.ts`.

Uploads flow from UI/API to an upload service and R2: `app/[locale]/upload/page.tsx`, `app/api/upload/route.ts`, and `services/upload.service.ts`. Signed URL support in `lib/signed-url/index.ts` provides a boundary for controlled object access.

#### Billing, Webhooks, and Compliance
Stripe client/configuration and webhook business logic live in `lib/stripe/client.ts`, `lib/stripe/config.ts`, and `lib/stripe/webhook-handlers.ts`. Checkout, portal, subscription state changes, and inbound webhook verification are exposed under `app/api/stripe/`; billing UI is under `app/[locale]/billing/`.

Billing persistence is modeled through customer, payment, invoice, subscription, and webhook-event repositories. GDPR export/delete endpoints (`app/api/gdpr/export/route.ts`, `app/api/gdpr/delete/route.ts`) provide explicit privacy-management control paths.

#### Cross-Cutting Controls and Operations
Environment validation is centralized in `lib/config/env.ts`. Security and traffic controls span CORS/CSRF, API middleware, and KV-backed limits: `lib/security/cors.ts`, `lib/security/csrf.ts`, and `lib/api/rate-limit.ts`. Audit logging, structured logging, analytics, and alerting are separate adapters in `lib/audit/logger.ts`, `lib/analytics/index.ts`, and `lib/monitoring/alerting.ts`.

GitHub Actions run CI and promote branches to Cloudflare environments: `.github/workflows/ci.yml`, `.github/workflows/deploy-test.yml`, and `.github/workflows/deploy-prod.yml`.

Main entry points: [`app/api/health/route.ts`](https://github.com/tangsy/edge-next-starter/blob/main/app/api/health/route.ts) (Route handlers), [`repositories/user.repository.ts`](https://github.com/tangsy/edge-next-starter/blob/main/repositories/user.repository.ts) (Repositories), [`worker/index.ts`](https://github.com/tangsy/edge-next-starter/blob/main/worker/index.ts) (Worker bootstrap), [`services/auth.service.ts`](https://github.com/tangsy/edge-next-starter/blob/main/services/auth.service.ts) (Domain services)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ui["App Router UI"]
  node_ui["Locale App Router<br/>Next.js UI<br/>[layout.tsx]"]
  node_i18n["Locale routing<br/>i18n config<br/>[routing.ts]"]
  node_auth_ui["Account screens &amp; actions<br/>UI auth<br/>[auth.ts]"]
end

subgraph group_api["Edge API"]
  node_worker{{"Worker bootstrap<br/>Cloudflare Worker<br/>[index.ts]"}}
  node_api_routes["Route handlers<br/>Edge API routes<br/>[route.ts]"]
  node_api_layer["API middleware layer<br/>API foundation<br/>[middleware.ts]"]
  node_auth_routes["Auth endpoints<br/>auth routes<br/>[route.ts]"]
  node_stripe_routes["Stripe endpoints<br/>billing routes<br/>[route.ts]"]
  node_gdpr_routes["GDPR endpoints<br/>privacy routes<br/>[route.ts]"]
end

subgraph group_domain["Domain &amp; Platform"]
  node_auth["Auth &amp; sessions<br/>better-auth integration<br/>[index.ts]"]
  node_services["Domain services<br/>service layer<br/>[auth.service.ts]"]
  node_repositories["Repositories<br/>persistence layer<br/>[user.repository.ts]"]
  node_db_client["Prisma D1 client<br/>database adapter<br/>[client.ts]"]
  node_cache_limits["Cache &amp; rate limits<br/>KV controls<br/>[index.ts]"]
  node_r2_access["R2 object access<br/>storage adapter<br/>[client.ts]"]
  node_stripe["Stripe integration<br/>billing adapter"]
  node_observability["Audit &amp; monitoring<br/>operations adapters<br/>[logger.ts]"]
end

subgraph group_data["Cloudflare Data"]
  node_d1[("D1 database<br/>relational store<br/>[schema.prisma]")]
  node_kv[("KV<br/>cache store<br/>[client.ts]")]
  node_r2[("R2<br/>object storage")]
end

subgraph group_external["External Services"]
  node_identity["Google OAuth<br/>identity provider"]
  node_stripe_platform["Stripe<br/>payment platform"]
end

subgraph group_delivery["Delivery"]
  node_ci_cd["CI/CD workflows<br/>GitHub Actions<br/>[ci.yml]"]
  node_deploy{{"Cloudflare deployment<br/>deployment config<br/>[wrangler.prod.toml]"}}
end

node_ci_cd -->|"deploys"| node_deploy
node_deploy -->|"configures"| node_worker
node_worker -->|"serves"| node_ui
node_worker -->|"routes requests"| node_api_routes
node_i18n -->|"localizes"| node_ui
node_auth_ui -->|"authenticates via"| node_auth
node_ui -->|"calls"| node_api_routes
node_api_routes -->|"uses"| node_api_layer
node_api_routes -->|"invokes"| node_services
node_auth_routes -->|"uses"| node_auth
node_stripe_routes -->|"uses"| node_stripe
node_gdpr_routes -->|"exports or deletes"| node_repositories
node_api_layer -->|"enforces"| node_cache_limits
node_services -->|"persists through"| node_repositories
node_services -->|"uploads through"| node_r2_access
node_repositories -->|"uses"| node_db_client
node_db_client -->|"reads and writes"| node_d1
node_cache_limits -->|"uses"| node_kv
node_r2_access -->|"stores objects"| node_r2
node_auth -->|"OAuth"| node_identity
node_stripe -->|"checkout and webhooks"| node_stripe_platform
node_stripe -->|"records billing state"| node_repositories
node_api_routes -.->|"emits telemetry"| node_observability

click node_worker "https://github.com/tangsy/edge-next-starter/blob/main/worker/index.ts"
click node_ui "https://github.com/tangsy/edge-next-starter/blob/main/app/%5Blocale%5D/layout.tsx"
click node_i18n "https://github.com/tangsy/edge-next-starter/blob/main/i18n/routing.ts"
click node_auth_ui "https://github.com/tangsy/edge-next-starter/blob/main/app/actions/auth.ts"
click node_api_routes "https://github.com/tangsy/edge-next-starter/blob/main/app/api/health/route.ts"
click node_api_layer "https://github.com/tangsy/edge-next-starter/blob/main/lib/api/middleware.ts"
click node_auth_routes "https://github.com/tangsy/edge-next-starter/blob/main/app/api/auth/%5B...all%5D/route.ts"
click node_stripe_routes "https://github.com/tangsy/edge-next-starter/blob/main/app/api/stripe/webhook/route.ts"
click node_gdpr_routes "https://github.com/tangsy/edge-next-starter/blob/main/app/api/gdpr/export/route.ts"
click node_auth "https://github.com/tangsy/edge-next-starter/blob/main/lib/auth/index.ts"
click node_services "https://github.com/tangsy/edge-next-starter/blob/main/services/auth.service.ts"
click node_repositories "https://github.com/tangsy/edge-next-starter/blob/main/repositories/user.repository.ts"
click node_db_client "https://github.com/tangsy/edge-next-starter/blob/main/lib/db/client.ts"
click node_cache_limits "https://github.com/tangsy/edge-next-starter/blob/main/lib/rate-limiter/index.ts"
click node_r2_access "https://github.com/tangsy/edge-next-starter/blob/main/lib/r2/client.ts"
click node_stripe "https://github.com/tangsy/edge-next-starter/blob/main/lib/stripe/webhook-handlers.ts"
click node_observability "https://github.com/tangsy/edge-next-starter/blob/main/lib/audit/logger.ts"
click node_d1 "https://github.com/tangsy/edge-next-starter/blob/main/prisma/schema.prisma"
click node_kv "https://github.com/tangsy/edge-next-starter/blob/main/lib/cache/client.ts"
click node_ci_cd "https://github.com/tangsy/edge-next-starter/blob/main/.github/workflows/ci.yml"
click node_deploy "https://github.com/tangsy/edge-next-starter/blob/main/wrangler.prod.toml"

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_ui,node_i18n,node_auth_ui toneBlue
class node_worker,node_api_routes,node_api_layer,node_auth_routes,node_stripe_routes,node_gdpr_routes toneAmber
class node_auth,node_services,node_repositories,node_db_client,node_cache_limits,node_r2_access,node_stripe,node_observability toneMint
class node_d1,node_kv,node_r2 toneRose
class node_identity,node_stripe_platform toneIndigo
class node_ci_cd,node_deploy toneTeal
```

## Components

### App Router UI

Locale-scoped product interface.

- [Locale App Router](https://github.com/tangsy/edge-next-starter/blob/main/app/%5Blocale%5D/layout.tsx): Next.js UI · Layouts and product pages including dashboard, profile, billing, and upload. · `app/[locale]/layout.tsx`
- [Locale routing](https://github.com/tangsy/edge-next-starter/blob/main/i18n/routing.ts): i18n config · `i18n/routing.ts`
- [Account screens & actions](https://github.com/tangsy/edge-next-starter/blob/main/app/actions/auth.ts): UI auth · Login, registration, profile, and server-side auth actions. · `app/actions/auth.ts`

### Edge API

Route handlers execute in the Worker runtime.

- [Worker bootstrap](https://github.com/tangsy/edge-next-starter/blob/main/worker/index.ts): Cloudflare Worker · Deploys the vinext/Vite Next.js application with bindings. · `worker/index.ts`
- [Route handlers](https://github.com/tangsy/edge-next-starter/blob/main/app/api/health/route.ts): Edge API routes · Health, users, posts, upload, GDPR, monitoring, and product APIs. · `app/api/health/route.ts`
- [API middleware layer](https://github.com/tangsy/edge-next-starter/blob/main/lib/api/middleware.ts): API foundation · Applies middleware, database access, rate limits, and response conventions. · `lib/api/middleware.ts`
- [Auth endpoints](https://github.com/tangsy/edge-next-starter/blob/main/app/api/auth/%5B...all%5D/route.ts): auth routes · better-auth catch-all and dedicated registration endpoint. · `app/api/auth/[...all]/route.ts`
- [Stripe endpoints](https://github.com/tangsy/edge-next-starter/blob/main/app/api/stripe/webhook/route.ts): billing routes · Checkout, portal, subscription, and webhook endpoints. · `app/api/stripe/webhook/route.ts`
- [GDPR endpoints](https://github.com/tangsy/edge-next-starter/blob/main/app/api/gdpr/export/route.ts): privacy routes · Exports or deletes personal data. · `app/api/gdpr/export/route.ts`

### Domain & Platform

Shared application capabilities and orchestration.

- [Auth & sessions](https://github.com/tangsy/edge-next-starter/blob/main/lib/auth/index.ts): better-auth integration · Authentication and session boundary. · `lib/auth/index.ts`
- [Domain services](https://github.com/tangsy/edge-next-starter/blob/main/services/auth.service.ts): service layer · Orchestrates authentication and upload workflows. · `services/auth.service.ts`
- [Repositories](https://github.com/tangsy/edge-next-starter/blob/main/repositories/user.repository.ts): persistence layer · Isolates user, post, subscription, and billing database operations. · `repositories/user.repository.ts`
- [Prisma D1 client](https://github.com/tangsy/edge-next-starter/blob/main/lib/db/client.ts): database adapter · `lib/db/client.ts`
- [Cache & rate limits](https://github.com/tangsy/edge-next-starter/blob/main/lib/rate-limiter/index.ts): KV controls · KV-backed caching and request limiting. · `lib/rate-limiter/index.ts`
- [R2 object access](https://github.com/tangsy/edge-next-starter/blob/main/lib/r2/client.ts): storage adapter · Object operations and controlled signed access. · `lib/r2/client.ts`
- [Stripe integration](https://github.com/tangsy/edge-next-starter/blob/main/lib/stripe/webhook-handlers.ts): billing adapter · Client configuration and verified webhook business logic. · `lib/stripe/webhook-handlers.ts`
- [Audit & monitoring](https://github.com/tangsy/edge-next-starter/blob/main/lib/audit/logger.ts): operations adapters · Audit logs, analytics, alerting, and monitoring. · `lib/audit/logger.ts`

### Cloudflare Data

Bound Worker storage bindings.

- [D1 database](https://github.com/tangsy/edge-next-starter/blob/main/prisma/schema.prisma): relational store · Relational source of truth, defined by Prisma schema and migrations. · `prisma/schema.prisma`
- [KV](https://github.com/tangsy/edge-next-starter/blob/main/lib/cache/client.ts): cache store · `lib/cache/client.ts`
- **R2**: object storage

### External Services

- **Google OAuth**: identity provider
- **Stripe**: payment platform

### Delivery

- [CI/CD workflows](https://github.com/tangsy/edge-next-starter/blob/main/.github/workflows/ci.yml): GitHub Actions · Validates and promotes deployments to Cloudflare environments. · `.github/workflows/ci.yml`
- [Cloudflare deployment](https://github.com/tangsy/edge-next-starter/blob/main/wrangler.prod.toml): deployment config · Worker bindings and production deployment configuration. · `wrangler.prod.toml`

## Connections

- CI/CD workflows → Cloudflare deployment: deploys
- Cloudflare deployment → Worker bootstrap: configures
- Worker bootstrap → Locale App Router: serves
- Worker bootstrap → Route handlers: routes requests
- Locale routing → Locale App Router: localizes
- Account screens & actions → Auth & sessions: authenticates via
- Locale App Router → Route handlers: calls
- Route handlers → API middleware layer: uses
- Route handlers → Domain services: invokes
- Auth endpoints → Auth & sessions: uses
- Stripe endpoints → Stripe integration: uses
- GDPR endpoints → Repositories: exports or deletes
- API middleware layer → Cache & rate limits: enforces
- Domain services → Repositories: persists through
- Domain services → R2 object access: uploads through
- Repositories → Prisma D1 client: uses
- Prisma D1 client → D1 database: reads and writes
- Cache & rate limits → KV: uses
- R2 object access → R2: stores objects
- Auth & sessions → Google OAuth: OAuth
- Stripe integration → Stripe: checkout and webhooks
- Stripe integration → Repositories: records billing state
- Route handlers → Audit & monitoring: emits telemetry
