workers

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 29, 2022 License: MIT Imports: 11 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 - Partially supported
    • Head
    • Get
    • Put (load all bytes to memory)
    • Put (stream)
    • Delete
    • List
    • Options for R2 methods
  • environment variables (WIP)
  • KV (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

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

type R2Bucket added in v0.2.0

type R2Bucket interface {
	Head(key string) (*R2Object, error)
	Get(key string) (*R2Object, error)
	Put(key string, value io.ReadCloser, opts *R2PutOptions) (*R2Object, error)
	Delete(key string) error
	List() (*R2Objects, error)
}

R2Bucket represents interface of Cloudflare Worker's R2 Bucket instance. - https://developers.cloudflare.com/r2/runtime-apis/#bucket-method-definitions - https://github.com/cloudflare/workers-types/blob/3012f263fb1239825e5f0061b267c8650d01b717/index.d.ts#L1006

func NewR2Bucket added in v0.2.0

func NewR2Bucket(varName string) (R2Bucket, error)

NewR2Bucket returns R2Bucket for given variable name. * variable name must be defined in wrangler.toml.

* if the given variable name doesn't exist on global object, returns error.

type R2HTTPMetadata added in v0.2.0

type R2HTTPMetadata struct {
	ContentType        string
	ContentLanguage    string
	ContentDisposition string
	ContentEncoding    string
	CacheControl       string
	CacheExpiry        time.Time
}

R2HTTPMetadata represents metadata of R2Object. * https://github.com/cloudflare/workers-types/blob/3012f263fb1239825e5f0061b267c8650d01b717/index.d.ts#L1053

type R2Object added in v0.2.0

type R2Object struct {
	Key            string
	Version        string
	Size           int
	ETag           string
	HTTPETag       string
	Uploaded       time.Time
	HTTPMetadata   R2HTTPMetadata
	CustomMetadata map[string]string
	// Body is a body of R2Object.
	// This value becomes nil when `Head` method of R2Bucket is called.
	Body io.Reader
	// contains filtered or unexported fields
}

R2Object represents Cloudflare R2 object. * https://github.com/cloudflare/workers-types/blob/3012f263fb1239825e5f0061b267c8650d01b717/index.d.ts#L1094

func (*R2Object) BodyUsed added in v0.2.0

func (o *R2Object) BodyUsed() (bool, error)

type R2Objects added in v0.2.0

type R2Objects struct {
	Objects   []*R2Object
	Truncated bool
	// Cursor indicates next cursor of R2Objects.
	// * This becomes empty string if cursor doesn't exist.
	Cursor            string
	DelimitedPrefixes []string
}

R2Objects represents Cloudflare R2 objects. * https://github.com/cloudflare/workers-types/blob/3012f263fb1239825e5f0061b267c8650d01b717/index.d.ts#L1121

type R2PutOptions added in v0.2.0

type R2PutOptions struct {
	HTTPMetadata   R2HTTPMetadata
	CustomMetadata map[string]string
	MD5            string
}

R2PutOptions represents Cloudflare R2 put options. * https://github.com/cloudflare/workers-types/blob/3012f263fb1239825e5f0061b267c8650d01b717/index.d.ts#L1128

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
examples

Jump to

Keyboard shortcuts

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