cloudflare-workers/README.md

107 lines
3.3 KiB
Markdown
Raw Permalink Normal View History

# 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)
2022-05-18 00:04:37 +09:00
* `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.
2022-05-18 01:01:50 +09:00
* Caution: This is an experimental project.
2022-05-18 00:04:37 +09:00
## Features
* [x] serve http.Handler
2022-07-31 21:17:34 +09:00
* [ ] R2
2022-05-29 10:02:40 +09:00
- [x] Head
- [x] Get
2022-06-18 23:55:27 +09:00
- [x] Put
- [x] Delete
2022-05-29 10:02:40 +09:00
- [x] List
2022-05-29 10:13:27 +09:00
- [ ] Options for R2 methods
2022-07-31 21:17:34 +09:00
* [ ] KV
- [x] Get
- [x] List
- [x] Put
- [x] Delete
- [ ] Options for KV methods
2023-04-26 14:19:09 +09:00
* [x] Cache API
2022-07-31 21:17:34 +09:00
* [ ] Durable Objects
2023-02-21 18:46:22 +01:00
- [x] Calling stubs
* [x] D1 (alpha)
2022-08-02 23:50:18 +09:00
* [x] Environment variables
2023-04-20 11:53:07 +09:00
* [x] FetchEvent
2023-04-30 11:01:41 +09:00
* [x] Cron Triggers
2024-01-03 23:33:34 +09:00
* [x] TCP Sockets
2024-11-10 00:52:46 +09:00
* [ ] Queues
- [x] Producer
- [ ] Consumer
2022-05-18 00:04:37 +09:00
## 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) { ... }
2022-05-18 00:07:25 +09:00
workers.Serve(handler)
2022-05-18 00:04:37 +09:00
}
```
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) { ... })
2022-05-18 00:21:08 +09:00
workers.Serve(nil) // if nil is given, http.DefaultServeMux is used.
2022-05-18 00:04:37 +09:00
}
```
2023-04-30 12:42:20 +09:00
For concrete examples, see `_examples` directory.
2022-05-18 00:16:28 +09:00
Currently, all examples use tinygo instead of Go due to binary size issues.
2022-05-18 00:04:37 +09:00
2023-04-30 12:06:40 +09:00
## Quick Start
2023-04-30 12:15:01 +09:00
First, please install the following tools:
2023-04-30 12:06:40 +09:00
2023-04-30 12:15:01 +09:00
* Node.js (and npm)
2023-04-30 12:06:40 +09:00
* [wrangler](https://developers.cloudflare.com/workers/wrangler/)
2023-04-30 12:15:01 +09:00
- You can install it by running `npm install -g wrangler`.
2023-10-11 23:35:21 +09:00
* tinygo 0.29.0 or later
* [gonew](https://pkg.go.dev/golang.org/x/tools/cmd/gonew)
2023-10-11 23:32:15 +09:00
- You can install it by running `go install golang.org/x/tools/cmd/gonew@latest`
2023-04-30 12:06:40 +09:00
2023-04-30 12:15:01 +09:00
After installation, please run the following commands.
2023-04-30 12:06:40 +09:00
```console
2023-10-11 23:32:15 +09:00
gonew github.com/syumai/workers/_templates/cloudflare/worker-tinygo your.module/my-app # e.g. github.com/syumai/my-app
2023-04-30 12:18:47 +09:00
cd my-app
go mod tidy
make dev # start running dev server
curl http://localhost:8787/hello # outputs "Hello!"
2023-04-30 12:06:40 +09:00
```
2023-04-30 12:15:01 +09:00
If you want a more detailed description, please refer to the README.md file in the generated directory.
2022-05-18 01:05:16 +09:00
2022-05-30 23:26:47 +09:00
## FAQ
2022-05-30 23:28:42 +09:00
### How do I deploy a worker implemented in this package?
2022-05-30 23:26:47 +09:00
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).
2023-04-30 12:06:40 +09:00
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.
2022-05-30 23:26:47 +09:00
2023-04-30 12:06:40 +09:00
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).