GithubHelp home page GithubHelp logo

isabella232 / cloudflare-starter-kit Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kriasoft/cloudflare-starter-kit

0.0 0.0 0.0 4.63 MB

Template (boilerplate) repository for scaffolding Cloudflare Workers projects

Home Page: https://workers.cloudflare.com/

License: MIT License

Shell 0.38% JavaScript 38.78% TypeScript 58.94% HTML 1.90%

cloudflare-starter-kit's Introduction

Cloudflare Workers Starter Kit

Project template for scaffolding Cloudflare Workers projects.

Features

  • Supports multiple CF Workers within the same (mono)repo; using ES modules syntax
  • Pre-configured with TypeScript, Babel, Rollup, ESLint, Jest, Prettier, Wrangler CLI, Miniflare
  • Pre-configured with local, test (staging/QA), and prod (production) environments
  • Pre-configured with local testing and debugging; loading environment variables from *.env files
  • Web Crypto API usage example for integrating with 3rd party services (Google Cloud, etc.)
  • Code snippets and other VSCode settings; CI/CD workflows with GitHub Actions

This project was bootstrapped with Cloudflare Starter Kit. Be sure to join our Discord channel for assistance.

Directory Structure

├──.github/workflows — CI/CD workflows powered by GitHub Actions
├──.vscodeVSCode settings including code snippets, recommended extensions etc.
├──env — Settings for local (dev), test (staging/QA), and prod (production) environments
├──api — Cloudflare Worker script for handling API requests
├──app — Web application front-end powered by Vite and React.js
├──siteCloudflare Workers script for serving static websites (reverse proxy)
├──scripts — Automation scripts, such as yarn deploy
├──package.json — The list of NPM dependencies and Yarn workspaces
├──rollup.config.mjsRollup configuration for compiling and bundling CF Workers
└──tsconfig.base.jsonTypeScript configuration shared across packages/workspaces

Tech Stack

Cloudflare Workers, Miniflare, Wrangler CLI, Vite, TypeScript, Babel, ESLint, Prettier, Jest, Yarn, Rollup.

Requirements

Getting Started

Generate a new repository from this template, clone, install dependencies, open it in VSCode and start hacking:

$ git clone https://github.com/kriasoft/cloudflare-starter-kit.git
$ cd ./cloudflare-starter-kit
$ yarn install
$ yarn start
$ yarn test

Find the worker scripts inside of the ./site and ./api folders.

IMPORTANT: Ensure that VSCode is using the workspace version of TypeScript.

Scripts

How to Create a CF Worker

Find below the minimal boilerplate for creating a new CF Worker script using TypeScript with ESM syntax:

example/index.ts — CF Worker script

export default {
  async fetch(req, env, ctx) {
    return new Response(`Hello world!`, { status: 200 });
  },
} as Required<Pick<ExportedHandler<Env>, "fetch">>;

example/index.test.ts — unit test powered by Miniflare

import worker from "./index.js";

test("GET /", async () => {
  const env = getMiniflareBindings();
  const req = new Request(`https://${env.APP_HOSTNAME}/`);
  const res = await worker.fetch(req, env, {});
  const body = await res.text();

  expect(res.status).toEqual(200);
  expect(body).toEqual(`Hello world!`);
});

example/wrangler.toml — deployment configuration

name = "example"
main = "index.js"
compatibility_date = "2022-04-18"
account_id = "$CLOUDFLARE_ACCOUNT_ID"
route = "$APP_HOSTNAME/*"

[vars]
APP_ENV = "$APP_ENV"
APP_HOSTNAME = "$APP_HOSTNAME"

[[rules]]
type = "ESModule"
globs = ["**/*.js"]

Plus package.json, tsconfig.json, and global.d.ts files configuring TypeScript for the workspace.

Note that $APP_HOSTNAME and $CLOUDFLARE_ACCOUNT_ID placeholders in the example above will be automatically replaced with values from *.env files for the target environment during local testing or deployment.

For more sophisticated examples visit Cloudflare Workers Examples directory.

How to Deploy

The deployments are handled automatically by GitHub Actions (see .github/workflows) whenever a new commit lands onto one of these branches:

Alternatively, you can deploy the app manually by ensuring the all the required environment variables found in the *.env files are up-to-date (e.g. CLOUDFLARE_API_TOKEN), then running yarn deploy [--env #0], specifying the target deployment area via --env flag, e.g. --env=test (default) or --env=prod.

You can also deploy packages (workspaces) individually, for example:

$ yarn api:deploy --env=prod
$ yarn site:deploy --env=prod

How to View Logs

$ yarn api:cf tail [--env #0]
$ yarn site:cf tail [--env #0]

How to Update

  • yarn set version stable — Bump Yarn to the latest version
  • yarn upgrade-interactive — Update Node.js modules (dependencies)
  • yarn dlx @yarnpkg/sdks vscode — Update TypeScript, ESLint, and Prettier settings in VSCode

Backers 💰

              

Related Projects

How to Contribute

Anyone and everyone is welcome to contribute. Start by checking out the list of open issues marked help wanted. However, if you decide to get involved, please take a moment to review the guidelines.

License

Copyright © 2020-present Kriasoft. This source code is licensed under the MIT license found in the LICENSE file.


Made with ♥ by Konstantin Tarkus (@koistya, blog) and contributors.

cloudflare-starter-kit's People

Contributors

koistya avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.