Go package to run an HTTP server on Cloudflare Workers.

  • By syumai
  • Last update: Jan 2, 2023
  • Comments: 1

workers Go Reference

  • workers is a package to run an HTTP server written in Go on Cloudflare Workers.
  • This package can easily serve http.Handler on Cloudflare Workers.
  • Caution: This is an experimental project.

Features

  • serve http.Handler
  • environment variables (WIP)
  • KV (WIP)
  • R2 (WIP)

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.

A template repository is also available.

License

MIT

Author

syumai

Download

workers.zip

Comments(1)

  • 1

    Cannot build wasm binary

    using template

    $ make dev
    wrangler dev
     ⛅️ wrangler 2.1.15 (update available 2.2.1)
    ------------------------------------------------------
    Running custom build: make build
    make[1]: Entering directory '/home/arman/Go-Workspace/rapidapi-translate'
    mkdir -p dist
    tinygo build -o ./dist/app.wasm -target wasm ./...
    # encoding/xml
    /usr/lib/go-1.19/src/encoding/xml/typeinfo.go:318:14: typ.FieldByIndex undefined (type reflect.Type has no field or method FieldByIndex)
    /usr/lib/go-1.19/src/encoding/xml/typeinfo.go:319:14: typ.FieldByIndex undefined (type reflect.Type has no field or method FieldByIndex)
    make[1]: *** [Makefile:8: build] Error 1
    make[1]: Leaving directory '/home/arman/Go-Workspace/rapidapi-translate'
    
    ✘ [ERROR] Command failed with exit code 2: make build
    
    
    If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose
    make: *** [Makefile:3: dev] Error 1