workers

package module
v0.11.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 29, 2023 License: MIT Imports: 8 Imported by: 66

README

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
  • R2
    • Head
    • Get
    • Put
    • Delete
    • List
    • Options for R2 methods
  • KV
    • Get
    • List
    • Put
    • Delete
    • Options for KV methods
  • Cache API
  • Durable Objects
    • Calling stubs
  • D1 (alpha)
  • Environment variables
  • FetchEvent's lifecycle methods
    • waitUntil
    • respondWith
    • passThroughOnException

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.

FAQ

How do I deploy a worker implemented in this package?

To deploy a Worker, the following steps are required.

  • Create a worker project using wrangler.
  • Build a Wasm binary.
  • Upload a Wasm binary with a JavaScript code to load and instantiate Wasm (for entry point).

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

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

License

MIT

Author

syumai, akarasz

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Serve

func Serve(handler http.Handler)

Server serves http.Handler on Cloudflare Workers. if the given handler is nil, http.DefaultServeMux will be used.

Types

This section is empty.

Directories

Path Synopsis
_examples
browser module
cache module
env module
fetch module
fetch-event module
hello module
incoming module
kv-counter module
queues module
sockets module
_templates
d1
examples
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL