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.
Before you start
Section titled “Before you start”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.
Deploy
Section titled “Deploy”-
Get the code and log in to Cloudflare:
Terminal window git clone https://github.com/ifokeev/gymgym.gitcd gymgymexport COREPACK_ENABLE_DOWNLOAD_PROMPT=0corepack pnpm installnpx wrangler login -
Create the database and the bucket for photos:
Terminal window npx wrangler d1 create gymgymnpx wrangler r2 bucket create gymgym-mediaPut the
database_idthat the first command prints intoapps/web/wrangler.jsonc. If you give the bucket a different name, changebucket_namethere too. -
Set your domain. The file ships with the values of gymgym.club. Change these in
apps/web/wrangler.jsonc:Setting Set it to routesYour domain, with custom_domain: trueBETTER_AUTH_URLThe address where people open the app, such as https://gym.example.comPASSKEY_RP_IDYour registrable domain, such as example.com. Passkeys belong to it, so a later change invalidates all of themOn 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 setSITE_URL(the origin of the landing page) andCOOKIE_DOMAIN(the parent domain that shares the sign-in cookies). On one host, leave both out.The
varsblock 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. -
Add the secret. You need only one:
Terminal window cd apps/webopenssl rand -base64 32 # copy the outputnpx wrangler secret put BETTER_AUTH_SECRETIf Wrangler offers to create the Worker because it does not exist yet, say yes.
-
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 -
Check it. Open your address and choose “Continue as guest”. Run
curl https://<your host>/api/health. It answers{"ok":true}. Runcurl -i https://<your host>/mcp. It answers 401 with aWWW-Authenticateheader. 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.
Switch more on
Section titled “Switch more on”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.
What gets created
Section titled “What gets created”| 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.
Update
Section titled “Update”git pullcorepack pnpm installcorepack pnpm --filter @gymgym/web deployMigrations 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.
Automatic deploys
Section titled “Automatic deploys”.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.
Staging
Section titled “Staging”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.
People’s data
Section titled “People’s data”- 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.
The docs site
Section titled “The docs site”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.
If something goes wrong
Section titled “If something goes wrong”| 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 |