Skip to content

Deploy to Cloudflare

gymgym is one Cloudflare Worker with a D1 database, an R2 bucket for photos and two Durable Objects. You deploy it from a clone of the repository with Wrangler.

You need:

  • A Cloudflare account. For anything beyond a trial, use the Workers Paid plan. The Free plan allows 10 ms of CPU for each request. The limits page of Cloudflare says that workloads with sign-in or server-side rendering typically use 10 to 20 ms. So some requests fail with error 1102.
  • A domain whose DNS is on that account. The Worker is attached to it as a custom domain.
  • Git and Node.js 24 or newer. pnpm comes with Corepack.
  1. Get the code and log in to Cloudflare:

    Terminal window
    git clone https://github.com/ifokeev/gymgym.git
    cd gymgym
    export COREPACK_ENABLE_DOWNLOAD_PROMPT=0
    corepack pnpm install
    npx wrangler login
  2. Create the database and the bucket for photos:

    Terminal window
    npx wrangler d1 create gymgym
    npx wrangler r2 bucket create gymgym-media

    Put the database_id that the first command prints into apps/web/wrangler.jsonc. If you give the bucket a different name, change bucket_name there too.

  3. Set your domain. The file ships with the values of gymgym.club. Change these in apps/web/wrangler.jsonc:

    Setting Set it to
    routes Your domain, with custom_domain: true
    BETTER_AUTH_URL The address where people open the app, such as https://gym.example.com
    PASSKEY_RP_ID Your registrable domain, such as example.com. Passkeys belong to it, so a later change invalidates all of them

    On one host, the landing page is / and the app is /app. That is all you need. gymgym.club puts the landing page on the bare domain and the app on a subdomain. To do the same, also set SITE_URL (the origin of the landing page) and COOKIE_DOMAIN (the parent domain that shares the sign-in cookies). On one host, leave both out.

    The vars block also holds the coach settings of gymgym.club: the starting credit, a daily limit and the prompt cache. Read The coach before you open the instance to other people.

  4. Add the secret. You need only one:

    Terminal window
    cd apps/web
    openssl rand -base64 32 # copy the output
    npx wrangler secret put BETTER_AUTH_SECRET

    If Wrangler offers to create the Worker because it does not exist yet, say yes.

  5. Deploy. This command builds the site, applies the D1 migrations to your database and then deploys the Worker:

    Terminal window
    corepack pnpm --filter @gymgym/web deploy
  6. Check it. Open your address and choose “Continue as guest”. Run curl https://<your host>/api/health. It answers {"ok":true}. Run curl -i https://<your host>/mcp. It answers 401 with a WWW-Authenticate header. This means that the MCP server is up.

Guest accounts work with nothing else set up. To let people keep their data across devices, turn on a sign-in provider.

Each of these is optional. You can add them at any time.

You want Set Read
Sign-in with Google or GitHub GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET, GITHUB_CLIENT_ID and GITHUB_CLIENT_SECRET (secrets) Providers
An admin dashboard ADMIN_EMAILS, your email address, in vars Admins and invites
Registration by invitation only INVITE_ONLY=1 Admins and invites
The AI coach OPENROUTER_API_KEY (secret) The coach
Notifications for the rest timer, timed sets and reminders VAPID_PUBLIC_KEY, VAPID_PRIVATE_JWK and VAPID_CONTACT (such as mailto:you@example.com), from npx @pushforge/builder vapid
Exercise images or video examples MEDIA_BASE_URL or YOUTUBE_API_KEY Exercise media
Fewer days of activity events, or none EVENTS_RETENTION_DAYS (a number from 1 to 365, or 0 for none), in vars Activity

To set a secret, run npx wrangler secret put NAME from apps/web. A plain setting goes in the vars block of wrangler.jsonc.

Piece What it does
Worker gymgym Serves the landing page, the app, the API and the MCP server
D1 database Holds everything that people save: workouts, routines, accounts and coach conversations
R2 bucket Holds the photos of custom exercises and the photos that people send to the coach. It stays private. The Worker serves a photo only to its owner. Without the MEDIA binding, photo uploads are off
Durable Object RestTimer The clock of each person’s notifications. It sends the “rest is over” and “set is over” notifications on time, even when the app is closed
Durable Object AgentTurn Makes the answers of the coach in the background, one object for each conversation, so an answer goes on when the page is closed
Cron trigger, every minute Sends reminders and coach check-ins, and cleans up old logs

Wrangler creates the two Durable Objects from the migrations list in wrangler.jsonc. Do not change the class names or the migrations that are already there.

Terminal window
git pull
corepack pnpm install
corepack pnpm --filter @gymgym/web deploy

Migrations are applied in order and only once, so it is safe to run the command again. If you need it, D1 Time Travel can restore the database to an earlier moment.

.github/workflows/deploy.yml deploys main after CI passes. In your fork, add the repository secrets CLOUDFLARE_API_TOKEN and CLOUDFLARE_ACCOUNT_ID. Without the token, the workflow skips the deploy. The workflow does not run migrations. Before you merge a change that adds one, apply it yourself with npx wrangler d1 migrations apply DB --remote.

wrangler.jsonc defines a staging environment. Create its own database and bucket (gymgym-staging and gymgym-media-staging in the file). Put the database id into the file, add the secrets with --env staging, and deploy with npx wrangler deploy --env staging.

  • Everything lives in your D1 database and your R2 bucket, and gymgym deletes it with the account. People can download their data as one JSON file (Import and export).
  • The browser shrinks and re-encodes a photo that someone adds to an exercise. Before the Worker stores it, it removes the EXIF, XMP and IPTC data. So gymgym keeps no location or camera details.
  • The coach sends messages, and the account data that it reads to answer them, to OpenRouter and to the provider of the model. The coach has the details.

corepack pnpm --filter @gymgym/docs deploy publishes this site as static assets. First change the route in apps/docs/wrangler.jsonc to your own domain.

You see Cause
Error 1102, or “Worker exceeded resource limits” The CPU limit of the Free plan. Move to Workers Paid
Passkeys are refused PASSKEY_RP_ID is not your domain, or a parent of the address that people use (Passkeys)
No Google or GitHub button A provider shows only when both of its secrets are set
No Coach tab OPENROUTER_API_KEY is not set