Template for building Cloudflare Workers with Hono, using a hexagonal architecture (ports and adapters).
- Runtime: Cloudflare Workers
- Framework: Hono 4.x
- Language: TypeScript
- Tooling: Wrangler 4.x
src/
core/
domain/ # Domain types and custom errors
ports/ # Interface contracts
services/ # Business logic
adapters/
primary/http/ # HTTP routes, middleware, request models
secondary/ # External implementations (e.g. logger)
assets/ # Static assets (for example JWT public keys)
utils/ # Shared utility helpers- Node.js 20+
- npm
- Cloudflare account and
wranglerauthentication for deployment
Install dependencies:
npm installRun locally:
npm run devThe Worker runs on Wrangler's local development server (typically http://localhost:8787).
npm run dev: Start local development server (wrangler dev)npm run deploy: Deploy Worker (wrangler deploy --minify)npm run deploy-local: Deploy using local environment (wrangler deploy --minify -e=local)npm run cf-typegen: Regenerate Worker bindings types (wrangler types --env-interface CloudflareBindings)
Run npm run cf-typegen after editing bindings in wrangler.jsonc.
Main Worker configuration is in wrangler.jsonc.
Current bindings and variables:
- Variable
ALLOWED_ORIGINSfor CORS origin allowlist (comma-separated) - Service binding
ACCESS_MGMTfor access authorization RPC
Environments:
- Default environment
localenvironment override underenv.local
The app entrypoint is src/adapters/primary/http/index.ts.
Registered global middleware order:
- Hono logger
- Request ID middleware
- Secure headers
- CORS middleware
Centralized error handling is configured with app.onError(handleError).
OpenAPI spec is available at openapi.yaml.
Current endpoint:
GET /message: Returns plain textHello Hono!
Error response format is standardized as:
{ "error": "ErrorName" }Defined error names:
InternalErrorBadRequestErrorUnauthorizedErrorNotFoundErrorConflictErrorForbiddenError
- Keep business logic in
src/core/services - Keep adapters focused on integration concerns only
- Use constructor-based dependency injection
- Use one class per file
When adding a new feature:
- Define domain models/errors in
src/core/domain - Define or update interfaces in
src/core/ports - Implement business use cases in
src/core/services - Add HTTP route/schema/middleware wiring in
src/adapters/primary/http - Add or update adapter implementations in
src/adapters/secondary - Update
openapi.yamlto reflect API changes
Deploy to Cloudflare:
npm run deployDeploy with local environment config:
npm run deploy-localThis repository includes custom Copilot agents under .github/agents/ to standardize planning and review workflows.
Available agents:
Feature Planning Agent(.github/agents/plan-feature.agent.md)- Use for feature scoping and technical planning before implementation.
- Produces a structured feature spec covering domain, ports, services, adapters, tests, and
openapi.yamlupdates.
Code Review Agent(.github/agents/review-code.agent.md)- Use for structured code reviews of diffs and PRs.
- Focuses on correctness, architecture boundaries, error handling, security, tests, and API contract consistency.
Security Review Agent(.github/agents/security-review.agent.md)- Use for threat modeling and vulnerability analysis.
- Focuses on OWASP Top 10 checks, authn/authz coverage, injection risks, data exposure, and Cloudflare-specific concerns.
Suggested workflow:
- Start with
Feature Planning Agentfor a new capability. - Implement the feature.
- Run
Code Review Agenton the resulting changes. - Run
Security Review Agentbefore merge/deploy.
Notes:
- Agent instructions are project-specific and aligned with
AGENTS.mdconventions. - Keep agent files in
.github/agents/so they stay versioned with the codebase. - Keep updating the agents' instructions periodically, to match with latest structure of project
- Cloudflare Workers docs: https://developers.cloudflare.com/workers/
- Wrangler config docs: https://developers.cloudflare.com/workers/wrangler/configuration/
- Hono docs: https://hono.dev/