Deploy Cloudflare Workers easily without sacrifying developer experience.
- TypeScript.
- GitHub Actions CI/CD.
- Logflare integration (free logs for development).
- Easy way to manage environment variables for development/staging/production, all within the repo.
- Development environment based on Node.js that emulates the web worker environment.
- Create an account in Cloudflare and a token to manage workers.
- Clone this repo.
- Run
yarn
to install dependencies or use your preferred package manager. - Modify
.env.development
(create an account in Logflare if you want). - Create
.env.production
with equivalent variables and rungpg --symmetric --cipher-algo AES256 .env.production
to generate an encrypted version (save the passphrase for next step). - Add the following GitHub secrets:
PRODUCTION_PASSPHRASE
,CF_ACCOUNT_ID
,CF_API_TOKEN
.
src
: All the source codeapi
: Export Route objects meant to be consumed by workers. Each Route specifies an HTTP method, an URL path and a function as a handler.services
: Packages to communicate with third party services via HTTP protocol.utils
: Utility functions.
workers
: Each of the workers deployed to Cloudflare.setup.ts
: Common code to setup a router in CF workers, handle requests and responding.[worker-name]
index.ts
: Import Route object fromapi
and setup worker.wrangler.toml
: Describes how the worker is built and where is deployed to.package.json
: Dummy package.json required by Wrangler. Do not install dependencies here.
webpack.config.js
: Webpack configuration for production builds only.dev
: Lifts a Node server for development. Notice that the development runtime (Node) is slightly different from production runtim (WebWorker) so watch out for incompatibilities. Some of these differences are polyfilled in this directory.tsconfig.json
: Describes TypeScript properties.
Run yarn dev
to start a local server. Each route will be prefixed with the worker name. E.g. https://my-example.my-org.workers.dev/path
will be http://localhost:1337/my-example/path
.
Try adding new workers to the workers
directory or simply adding new routes to one of them.
- Why not using the default CFW way of adding environment variables?
This is by adding variables to the UI or to wrangler.toml
. I find that neither of those solutions are very flexible since, either you take the variables out of git itself, or just write them in plain text in a file. The proposed way keeps them in source control and encrypts the production variables.
- Why not using Serverless Framework instead of Wrangler?
Serverless Framework is great in general but its CF integration is no actively maintained. Specifically, it doens't support deploying to the free workers.dev
domain that CF provides.
- Add unit and integration tests.
- Add staging environment.