cloudflare-workers/README.md

71 lines
1.8 KiB
Markdown
Raw Normal View History

2022-05-18 00:55:19 +09:00
# workers [![Go Reference](https://pkg.go.dev/badge/github.com/syumai/workers.svg)](https://pkg.go.dev/github.com/syumai/workers)
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-05-29 10:02:40 +09:00
* [ ] R2 - Partially supported
- [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-05-29 10:17:47 +09:00
* [ ] environment variables (WIP)
* [ ] KV (WIP)
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
}
```
2022-05-18 00:16:28 +09:00
For concrete examples, see `examples` directory.
Currently, all examples use tinygo instead of Go due to binary size issues.
2022-05-18 00:04:37 +09:00
2022-05-18 01:05:16 +09:00
A template repository is also available.
* https://github.com/syumai/worker-template-tinygo
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).
The [worker-template-tinygo](https://github.com/syumai/worker-template-tinygo) repository contains all the required files, so I recommend using this template.
2022-05-18 00:04:37 +09:00
## License
MIT
## Author
syumai