syumai / workers

Go package to run an HTTP server on Cloudflare Workers.
MIT License
589 stars 40 forks source link
cloudflare cloudflare-workers go golang

workers

Go Reference Discord Server

Features

Installation

go get github.com/syumai/workers

Usage

implement your http.Handler and give it to workers.Serve().

func main() {
    var handler http.HandlerFunc = func (w http.ResponseWriter, req *http.Request) { ... }
    workers.Serve(handler)
}

or just call http.Handle and http.HandleFunc, then invoke workers.Serve() with nil.

func main() {
    http.HandleFunc("/hello", func (w http.ResponseWriter, req *http.Request) { ... })
    workers.Serve(nil) // if nil is given, http.DefaultServeMux is used.
}

For concrete examples, see _examples directory. Currently, all examples use tinygo instead of Go due to binary size issues.

Quick Start

First, please install the following tools:

After installation, please run the following commands.

gonew github.com/syumai/workers/_templates/cloudflare/worker-tinygo your.module/my-app # e.g. github.com/syumai/my-app
cd my-app
go mod tidy
make dev # start running dev server
curl http://localhost:8787/hello # outputs "Hello!"

If you want a more detailed description, please refer to the README.md file in the generated directory.

FAQ

How do I deploy a worker implemented in this package?

To deploy a Worker, the following steps are required.

The worker-tinygo template contains all the required files, so I recommend using this template.

The worker-go template (using regular Go, not tinygo) is also available, but it requires a paid plan of Cloudflare Workers (due to the large binary size).

Where can I have discussions about contributions, or ask questions about how to use the library?

You can do both through GitHub Issues. If you want to have a more casual conversation, please use the Discord server.