From 8978b3b2b09cd945255b0b2abf10baacb90ed1be Mon Sep 17 00:00:00 2001 From: syumai Date: Sun, 30 Apr 2023 11:52:54 +0900 Subject: [PATCH 1/4] add worker-tinygo template --- .../cloudflare/worker-tinygo/.gitignore | 1 + _templates/cloudflare/worker-tinygo/Makefile | 12 +++++ _templates/cloudflare/worker-tinygo/README.md | 53 +++++++++++++++++++ _templates/cloudflare/worker-tinygo/main.go | 19 +++++++ .../cloudflare/worker-tinygo/wrangler.toml | 9 ++++ 5 files changed, 94 insertions(+) create mode 100644 _templates/cloudflare/worker-tinygo/.gitignore create mode 100644 _templates/cloudflare/worker-tinygo/Makefile create mode 100644 _templates/cloudflare/worker-tinygo/README.md create mode 100644 _templates/cloudflare/worker-tinygo/main.go create mode 100644 _templates/cloudflare/worker-tinygo/wrangler.toml diff --git a/_templates/cloudflare/worker-tinygo/.gitignore b/_templates/cloudflare/worker-tinygo/.gitignore new file mode 100644 index 0000000..c795b05 --- /dev/null +++ b/_templates/cloudflare/worker-tinygo/.gitignore @@ -0,0 +1 @@ +build \ No newline at end of file diff --git a/_templates/cloudflare/worker-tinygo/Makefile b/_templates/cloudflare/worker-tinygo/Makefile new file mode 100644 index 0000000..f185118 --- /dev/null +++ b/_templates/cloudflare/worker-tinygo/Makefile @@ -0,0 +1,12 @@ +.PHONY: dev +dev: + wrangler dev + +.PHONY: build +build: + go run github.com/syumai/workers/cmd/workers-assets-gen@latest + tinygo build -o ./build/app.wasm -target wasm ./... + +.PHONY: publish +publish: + wrangler publish diff --git a/_templates/cloudflare/worker-tinygo/README.md b/_templates/cloudflare/worker-tinygo/README.md new file mode 100644 index 0000000..7299824 --- /dev/null +++ b/_templates/cloudflare/worker-tinygo/README.md @@ -0,0 +1,53 @@ +# worker-template-tinygo + +- A template for starting a Cloudflare Worker project with tinygo. +- This template uses [`workers`](https://github.com/syumai/workers) package to run an HTTP server. + +## Usage + +- `main.go` includes simple HTTP server implementation. Feel free to edit this code and implement your own HTTP server. + +## Requirements + +- Node.js +- [wrangler](https://developers.cloudflare.com/workers/wrangler/) + - just run `npm install -g wrangler` +- tinygo + +## Getting Started + +``` +$ wrangler generate my-app syumai/workers/_templates/cloudflare/worker-tinygo +$ cd my-app +$ go mod init +$ go mod tidy +$ make dev # start running dev server +$ curl http://localhost:8787/hello +Hello! +``` + +- To change worker name, please edit `name` property in `wrangler.toml`. + +## Development + +### Commands + +``` +make dev # run dev server +make build # build Go Wasm binary +make publish # publish worker +``` + +### Testing dev server + +- Just send HTTP request using some tools like curl. + +``` +$ curl http://localhost:8787/hello +Hello! +``` + +``` +$ curl -X POST -d "test message" http://localhost:8787/echo +test message +``` diff --git a/_templates/cloudflare/worker-tinygo/main.go b/_templates/cloudflare/worker-tinygo/main.go new file mode 100644 index 0000000..936aa9c --- /dev/null +++ b/_templates/cloudflare/worker-tinygo/main.go @@ -0,0 +1,19 @@ +package main + +import ( + "io" + "net/http" + + "github.com/syumai/workers" +) + +func main() { + http.HandleFunc("/hello", func(w http.ResponseWriter, req *http.Request) { + msg := "Hello!" + w.Write([]byte(msg)) + }) + http.HandleFunc("/echo", func(w http.ResponseWriter, req *http.Request) { + io.Copy(w, req.Body) + }) + workers.Serve(nil) // use http.DefaultServeMux +} diff --git a/_templates/cloudflare/worker-tinygo/wrangler.toml b/_templates/cloudflare/worker-tinygo/wrangler.toml new file mode 100644 index 0000000..e5a3bdc --- /dev/null +++ b/_templates/cloudflare/worker-tinygo/wrangler.toml @@ -0,0 +1,9 @@ +name = "tinygo-worker" +main = "./build/worker.mjs" +compatibility_date = "2022-05-13" +compatibility_flags = [ + "streams_enable_constructors" +] + +[build] +command = "make build" From 058165ca1c90cb07bc294c17ab5fb08ed111f5e1 Mon Sep 17 00:00:00 2001 From: syumai Date: Sun, 30 Apr 2023 11:54:32 +0900 Subject: [PATCH 2/4] add worker-go template --- _templates/cloudflare/worker-go/.gitignore | 1 + _templates/cloudflare/worker-go/Makefile | 12 ++++ _templates/cloudflare/worker-go/README.md | 58 +++++++++++++++++++ _templates/cloudflare/worker-go/main.go | 24 ++++++++ _templates/cloudflare/worker-go/wrangler.toml | 9 +++ 5 files changed, 104 insertions(+) create mode 100644 _templates/cloudflare/worker-go/.gitignore create mode 100644 _templates/cloudflare/worker-go/Makefile create mode 100644 _templates/cloudflare/worker-go/README.md create mode 100644 _templates/cloudflare/worker-go/main.go create mode 100644 _templates/cloudflare/worker-go/wrangler.toml diff --git a/_templates/cloudflare/worker-go/.gitignore b/_templates/cloudflare/worker-go/.gitignore new file mode 100644 index 0000000..c795b05 --- /dev/null +++ b/_templates/cloudflare/worker-go/.gitignore @@ -0,0 +1 @@ +build \ No newline at end of file diff --git a/_templates/cloudflare/worker-go/Makefile b/_templates/cloudflare/worker-go/Makefile new file mode 100644 index 0000000..fe89215 --- /dev/null +++ b/_templates/cloudflare/worker-go/Makefile @@ -0,0 +1,12 @@ +.PHONY: dev +dev: + wrangler dev + +.PHONY: build +build: + go run github.com/syumai/workers/cmd/workers-assets-gen@latest -mode=go + GOOS=js GOARCH=wasm go build -o ./build/app.wasm . + +.PHONY: publish +publish: + wrangler publish diff --git a/_templates/cloudflare/worker-go/README.md b/_templates/cloudflare/worker-go/README.md new file mode 100644 index 0000000..2a03f91 --- /dev/null +++ b/_templates/cloudflare/worker-go/README.md @@ -0,0 +1,58 @@ +# worker-template-go + +- A template for starting a Cloudflare Worker project with Go. +- This template uses [`workers`](https://github.com/syumai/workers) package to run an HTTP server. + +## Notice + +- A free plan Cloudflare Workers only accepts ~1MB sized workers. + - Go Wasm binaries easily exceeds this limit, so **you'll need to use a paid plan of Cloudflare Workers** (which accepts ~5MB sized workers). + +## Usage + +- `main.go` includes simple HTTP server implementation. Feel free to edit this code and implement your own HTTP server. + +## Requirements + +- Node.js +- [wrangler](https://developers.cloudflare.com/workers/wrangler/) + - just run `npm install -g wrangler` +- Go + +## Getting Started + +``` +$ wrangler generate my-app syumai/workers/_templates/cloudflare/worker-go +$ cd my-app +$ go mod init +$ go mod tidy +$ make dev # start running dev server +$ curl http://localhost:8787/hello +Hello! +``` + +- To change worker name, please edit `name` property in `wrangler.toml`. + +## Development + +### Commands + +``` +make dev # run dev server +make build # build Go Wasm binary +make publish # publish worker +``` + +### Testing dev server + +- Just send HTTP request using some tools like curl. + +``` +$ curl http://localhost:8787/hello +Hello! +``` + +``` +$ curl -X POST -d "test message" http://localhost:8787/echo +test message +``` diff --git a/_templates/cloudflare/worker-go/main.go b/_templates/cloudflare/worker-go/main.go new file mode 100644 index 0000000..2e2326c --- /dev/null +++ b/_templates/cloudflare/worker-go/main.go @@ -0,0 +1,24 @@ +package main + +import ( + "bytes" + "io" + "net/http" + + "github.com/syumai/workers" +) + +func main() { + http.HandleFunc("/hello", func(w http.ResponseWriter, req *http.Request) { + msg := "Hello!" + w.Write([]byte(msg)) + }) + http.HandleFunc("/echo", func(w http.ResponseWriter, req *http.Request) { + b, err := io.ReadAll(req.Body) + if err != nil { + panic(err) + } + io.Copy(w, bytes.NewReader(b)) + }) + workers.Serve(nil) // use http.DefaultServeMux +} diff --git a/_templates/cloudflare/worker-go/wrangler.toml b/_templates/cloudflare/worker-go/wrangler.toml new file mode 100644 index 0000000..7e68af0 --- /dev/null +++ b/_templates/cloudflare/worker-go/wrangler.toml @@ -0,0 +1,9 @@ +name = "go-worker" +main = "./build/worker.mjs" +compatibility_date = "2022-11-19" +compatibility_flags = [ + "streams_enable_constructors" +] + +[build] +command = "make build" From 305ba9fe68f6e3b07ada56c8126605e994f23326 Mon Sep 17 00:00:00 2001 From: syumai Date: Sun, 30 Apr 2023 12:06:40 +0900 Subject: [PATCH 3/4] update README.md --- README.md | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 967ee37..ec5e43c 100644 --- a/README.md +++ b/README.md @@ -60,8 +60,28 @@ func main() { 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. -* https://github.com/syumai/worker-template-tinygo +## Quick Start + +At first, please install the tools below. + +* Node.js (& npm) +* [wrangler](https://developers.cloudflare.com/workers/wrangler/) + - just run `npm install -g wrangler` +* tinygo + +Next, please execute commands below. + +```console +$ wrangler generate my-app syumai/workers/_templates/cloudflare/worker-tinygo +$ cd my-app +$ go mod init +$ go mod tidy +$ make dev # start running dev server +$ curl http://localhost:8787/hello +Hello! +``` + +If you want to read more detail description, please refer to the README.md in the generated directory. ## FAQ @@ -73,9 +93,9 @@ To deploy a Worker, the following steps are required. * 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. +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-template-go](https://github.com/syumai/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). +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? From 35230c5774c4813cf7183d05864ef0c19e1e7eaa Mon Sep 17 00:00:00 2001 From: syumai Date: Sun, 30 Apr 2023 12:15:01 +0900 Subject: [PATCH 4/4] update README.md descriptions --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index ec5e43c..7625dc1 100644 --- a/README.md +++ b/README.md @@ -62,14 +62,14 @@ Currently, all examples use tinygo instead of Go due to binary size issues. ## Quick Start -At first, please install the tools below. +First, please install the following tools: -* Node.js (& npm) +* Node.js (and npm) * [wrangler](https://developers.cloudflare.com/workers/wrangler/) - - just run `npm install -g wrangler` + - You can install it by running `npm install -g wrangler`. * tinygo -Next, please execute commands below. +After installation, please run the following commands. ```console $ wrangler generate my-app syumai/workers/_templates/cloudflare/worker-tinygo @@ -81,7 +81,7 @@ $ curl http://localhost:8787/hello Hello! ``` -If you want to read more detail description, please refer to the README.md in the generated directory. +If you want a more detailed description, please refer to the README.md file in the generated directory. ## FAQ