syumai/workers

repository·main·Indexed 22 days ago

https://github.com/syumai/workers

A Go package that enables running Go HTTP servers on Cloudflare Workers by bridging standard Go http.Handler interfaces to the Cloudflare Workers runtime via WebAssembly (Wasm). It includes examples for Basic Authentication proxies, KV storage, Durable Objects, Cron triggers, Cloudflare Queues, and Pages Functions.

Tokens
10.9K
Snippets
61
Records
83
Agent score
75%

What's inside syumai/workers

  1. Extend Worker lifecycle with WaitUntil and PassThroughOnException

    main

    Cloudflare Workers typically return an HTTP Response and exit immediately. You can extend this lifecycle using two key mechanisms:

    1. WaitUntil: Allows you to execute asynchronous tasks that run in the background without blocking the initial HTTP response. The Workers runtime will keep the worker alive until these tasks are completed.
    2. PassThroughOnException: Prevents the worker from returning a runtime error response if an unhandled exception occurs. Instead, the request is forwarded to the origin server as if the worker had not intercepted it.

    Recommended Pattern for Resilient Logging: If you are implementing a feature like a log stream that modifies headers (e.g., masking API tokens) but must not crash the service if an error occurs, declare PassThroughOnException first and use WaitUntil for the logging task.

  2. Understanding Queue Consumption patterns

    main

    When implementing real-world Cloudflare Queues consumers in Go, be aware of the distinction between blocking and non-blocking consumption.

    Note that wrangler does not support running multiple workers interacting with the same local queue simultaneously. For production scenarios, choose between:

    • queues.Consume: For blocking consumption.
    • queues.ConsumeNonBlock: For non-blocking consumption.
  3. Set up development for r2-image-server

    main

    To develop or run the r2-image-server, ensure you have the following tools installed globally:

    • wrangler (Cloudflare CLI)
    • Go version 1.24.0 or later

    For deeper integration details, refer to the Cloudflare R2 runtime APIs documentation and the syumai/workers package documentation.

  4. Manage your cron-job-template-go project with Make commands

    main

    The template includes a Makefile to simplify common development tasks:

    • make dev: Starts the local development server.
    • make build: Compiles the Go code into a WebAssembly (Wasm) binary.
    • make deploy: Deploys the worker to Cloudflare.

    To change the name of your worker, edit the name property in the wrangler.toml file.

    make dev
    make build
    make deploy
  5. Initialize and run a TinyGo Worker project

    main

    After scaffolding the project, follow these steps to initialize the Go modules and start the local development server:

    1. Navigate to your project directory.
    2. Initialize Go modules using go mod init.
    3. Tidy dependencies with go mod tidy.
    4. Start the development server with npm start.
    5. Test the server using curl.
    cd my-app
    go mod init
    go mod tidy
    npm start
    curl http://localhost:8787/hello
  6. Develop and deploy your Go Worker

    main

    The following commands are available for managing your Go Worker project lifecycle:

    • npm start: Runs the development server using Wrangler (enables Cloudflare-specific features).
    • go run .: Runs the dev server without Wrangler (Cloudflare-related features will be unavailable).
    • npm run build: Builds the Go WebAssembly (Wasm) binary.
    • npm run deploy: Deploys the worker to Cloudflare.
    npm start      # run dev server
    # or
    go run .       # run dev server without Wrangler
    npm run build  # build Go Wasm binary
    npm run deploy # deploy worker