Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/syumai/workers
Go package to run an HTTP server on Cloudflare Workers.
https://github.com/syumai/workers
cloudflare cloudflare-workers go golang
Last synced: 8 days ago
JSON representation
Go package to run an HTTP server on Cloudflare Workers.
- Host: GitHub
- URL: https://github.com/syumai/workers
- Owner: syumai
- License: mit
- Created: 2022-05-17T15:06:31.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-11T13:17:56.000Z (3 months ago)
- Last Synced: 2024-10-25T04:50:08.738Z (14 days ago)
- Topics: cloudflare, cloudflare-workers, go, golang
- Language: Go
- Homepage:
- Size: 325 KB
- Stars: 663
- Watchers: 10
- Forks: 40
- Open Issues: 19
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# workers
[![Go Reference](https://pkg.go.dev/badge/github.com/syumai/workers.svg)](https://pkg.go.dev/github.com/syumai/workers)
[![Discord Server](https://img.shields.io/discord/1095344956421447741?logo=discord&style=social)](https://discord.gg/tYhtatRqGs)* `workers` is a package to run an HTTP server written in Go on [Cloudflare Workers](https://workers.cloudflare.com/).
* This package can easily serve *http.Handler* on Cloudflare Workers.
* Caution: This is an experimental project.## Features
* [x] serve http.Handler
* [ ] R2
- [x] Head
- [x] Get
- [x] Put
- [x] Delete
- [x] List
- [ ] Options for R2 methods
* [ ] KV
- [x] Get
- [x] List
- [x] Put
- [x] Delete
- [ ] Options for KV methods
* [x] Cache API
* [ ] Durable Objects
- [x] Calling stubs
* [x] D1 (alpha)
* [x] Environment variables
* [x] FetchEvent
* [x] Cron Triggers
* [x] TCP Sockets## Installation
```
go get github.com/syumai/workers
```## Usage
implement your http.Handler and give it to `workers.Serve()`.
```go
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.
```go
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:
* Node.js (and npm)
* [wrangler](https://developers.cloudflare.com/workers/wrangler/)
- You can install it by running `npm install -g wrangler`.
* tinygo 0.29.0 or later
* [gonew](https://pkg.go.dev/golang.org/x/tools/cmd/gonew)
- You can install it by running `go install golang.org/x/tools/cmd/gonew@latest`After installation, please run the following commands.
```console
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.
* Create a worker project using [wrangler](https://developers.cloudflare.com/workers/wrangler/).
* Build a Wasm binary.
* Upload a Wasm binary with a JavaScript code to load and instantiate Wasm (for entry point).The [worker-tinygo template](https://github.com/syumai/workers/tree/main/_templates/cloudflare/worker-tinygo) contains all the required files, so I recommend using this template.
The [worker-go template](https://github.com/syumai/workers/tree/main/_templates/cloudflare/worker-go) (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](https://discord.gg/tYhtatRqGs).