Skip to content

Cloudflare Workers Adapter

The Cloudflare adapter runs Mocktioneer on Cloudflare Workers, providing global edge deployment through Cloudflare's network.

Overview

PropertyValue
Cratemocktioneer-adapter-cloudflare
Targetwasm32-unknown-unknown
PlatformCloudflare Workers
Use CaseProduction edge deployment

Prerequisites

  1. Wrangler CLI

    bash
    npm install -g wrangler
    # Or use npx wrangler
  2. WASM target

    bash
    rustup target add wasm32-unknown-unknown
  3. worker-build (used by the default wrangler.toml build command)

    bash
    cargo install worker-build
  4. Cloudflare account with Workers enabled

  5. Authenticate Wrangler

    bash
    wrangler login

Local Development

Run locally using Wrangler's local mode:

bash
# Using EdgeZero CLI
edgezero-cli serve --adapter cloudflare

# Or directly
wrangler dev --config crates/mocktioneer-adapter-cloudflare/wrangler.toml

This starts a local server that emulates the Workers environment.

Building

bash
# Using EdgeZero CLI
edgezero-cli build --adapter cloudflare

# Or directly
cargo build --release --target wasm32-unknown-unknown -p mocktioneer-adapter-cloudflare

The build produces a WASM binary at:

target/wasm32-unknown-unknown/release/mocktioneer_adapter_cloudflare.wasm

Deployment

First-Time Setup

  1. Edit crates/mocktioneer-adapter-cloudflare/wrangler.toml:

    toml
    name = "mocktioneer-adapter-cloudflare"
    main = "build/worker/shim.mjs"
    compatibility_date = "2023-05-01"
    
    [build]
    command = "worker-build --release"
  2. Deploy:

    bash
    wrangler publish --config crates/mocktioneer-adapter-cloudflare/wrangler.toml

Subsequent Deployments

bash
# Using EdgeZero CLI
edgezero-cli deploy --adapter cloudflare

# Or directly
wrangler publish --config crates/mocktioneer-adapter-cloudflare/wrangler.toml

Configuration

Build Settings

toml
[adapters.cloudflare.build]
target = "wasm32-unknown-unknown"
profile = "release"
features = ["cloudflare"]

Logging

toml
[adapters.cloudflare.logging]
level = "info"
echo_stdout = true

Cloudflare Workers logs are visible in:

  • wrangler tail for real-time logs
  • Workers Analytics in the dashboard

wrangler.toml

The crates/mocktioneer-adapter-cloudflare/wrangler.toml contains Workers-specific configuration:

toml
name = "mocktioneer-adapter-cloudflare"
main = "build/worker/shim.mjs"
compatibility_date = "2023-05-01"

[vars]
# Environment variables
ENVIRONMENT = "production"

[[kv_namespaces]]
# KV store bindings if needed
# binding = "MY_KV"
# id = "xxx"

Custom Domains

Using workers.dev

By default, your Worker is available at:

https://mocktioneer.<your-subdomain>.workers.dev

Custom Domain

  1. Add domain to Cloudflare (if not already)
  2. In Workers dashboard, go to your Worker
  3. Click "Triggers" > "Custom Domains"
  4. Add your domain

Or via wrangler.toml:

toml
routes = [
  { pattern = "mocktioneer.example.com/*", zone_name = "example.com" }
]

Environment Variables

Set environment variables in wrangler.toml:

toml
[vars]
LOG_LEVEL = "debug"

Or as secrets:

bash
wrangler secret put API_KEY

Monitoring

Real-Time Logs

bash
wrangler tail --config crates/mocktioneer-adapter-cloudflare/wrangler.toml

Dashboard Analytics

In the Cloudflare dashboard under Workers:

  • Request count
  • CPU time
  • Error rate
  • Geographic distribution

Troubleshooting

Build Errors

bash
# Ensure target is installed
rustup target add wasm32-unknown-unknown

# Clean and rebuild
cargo clean
cargo build --release --target wasm32-unknown-unknown -p mocktioneer-adapter-cloudflare

Local Development Issues

bash
# Check wrangler version
wrangler --version

# Run with verbose output
wrangler dev --config crates/mocktioneer-adapter-cloudflare/wrangler.toml --local

Deployment Failures

bash
# Check authentication
wrangler whoami

# Validate configuration
wrangler publish --dry-run --config crates/mocktioneer-adapter-cloudflare/wrangler.toml

Performance Considerations

Cloudflare Workers has these limits:

LimitFreePaid
CPU time10ms50ms
Memory128 MB128 MB
Script size1 MB10 MB
Subrequests501000

Mocktioneer typically uses:

  • < 5ms CPU time per request
  • < 10 MB memory
  • No subrequests

Workers KV Integration

If you need persistent storage, add KV bindings:

toml
[[kv_namespaces]]
binding = "MOCKTIONEER_KV"
id = "your-kv-namespace-id"

Create the namespace:

bash
wrangler kv:namespace create "MOCKTIONEER_KV"

Next Steps

Built with EdgeZero