From 63173970bec10231ad31bb6a0c1709a355170090 Mon Sep 17 00:00:00 2001 From: Allison Durham Date: Mon, 29 May 2023 18:48:09 -0500 Subject: [PATCH 1/2] added pages template --- _templates/cloudflare/pages-tinygo/Makefile | 12 ++++ _templates/cloudflare/pages-tinygo/README.md | 61 +++++++++++++++++++ .../pages-tinygo/functions/api/[[routes]].mjs | 6 ++ _templates/cloudflare/pages-tinygo/go.mod | 9 +++ _templates/cloudflare/pages-tinygo/go.sum | 2 + _templates/cloudflare/pages-tinygo/main.go | 29 +++++++++ .../cloudflare/pages-tinygo/pages/index.html | 20 ++++++ .../cloudflare/pages-tinygo/wrangler.toml | 6 ++ 8 files changed, 145 insertions(+) create mode 100644 _templates/cloudflare/pages-tinygo/Makefile create mode 100644 _templates/cloudflare/pages-tinygo/README.md create mode 100644 _templates/cloudflare/pages-tinygo/functions/api/[[routes]].mjs create mode 100644 _templates/cloudflare/pages-tinygo/go.mod create mode 100644 _templates/cloudflare/pages-tinygo/go.sum create mode 100644 _templates/cloudflare/pages-tinygo/main.go create mode 100644 _templates/cloudflare/pages-tinygo/pages/index.html create mode 100644 _templates/cloudflare/pages-tinygo/wrangler.toml diff --git a/_templates/cloudflare/pages-tinygo/Makefile b/_templates/cloudflare/pages-tinygo/Makefile new file mode 100644 index 0000000..e1edf12 --- /dev/null +++ b/_templates/cloudflare/pages-tinygo/Makefile @@ -0,0 +1,12 @@ +.PHONY: dev +dev: + wrangler pages dev ./pages + +.PHONY: build +build: + go run github.com/syumai/workers/cmd/workers-assets-gen@latest + tinygo build -o ./build/app.wasm -target wasm ./... + +.PHONY: deploy +deploy: + wrangler pages deploy ./pages diff --git a/_templates/cloudflare/pages-tinygo/README.md b/_templates/cloudflare/pages-tinygo/README.md new file mode 100644 index 0000000..73de321 --- /dev/null +++ b/_templates/cloudflare/pages-tinygo/README.md @@ -0,0 +1,61 @@ +# pages-tinygo + +- A template for starting a Cloudflare Pages Functions project with tinygo. +- This template uses the [`workers`](https://github.com/syumai/workers) package to run. + +## Usage + +- `main.go` includes a [chi](https://github.com/go-chi/chi) HTTP router implementation with three different routes. Feel free to edit this code and implement your own HTTP router. + +## Requirements + +- Node.js +- [wrangler](https://developers.cloudflare.com/workers/wrangler/) + - just run `npm install -g wrangler` +- tinygo + +## Getting Started + +```console +wrangler generate my-app syumai/workers/_templates/cloudflare/pages-tinygo +cd my-app +go mod init +go mod tidy +make dev # start running dev server +curl http://localhost:8787/api/hello # outputs "Hello, Pages Functions!" +``` + +## Development + +### Commands + +``` +make dev # run dev server +make build # build Go Wasm binary +make deploy # deploy worker +``` + +### Testing dev server + +- You can send HTTP requests using tools like curl. + +``` +$ curl http://localhost:8787/api/hello +Hello, Pages Functions! +``` + +``` +$ curl http://localhost:8787/api/hello?name=Example +Hello, Example! +``` + +``` +$ curl http://localhost:8787/api/hello2 +Hello, Hello world! +``` + +``` +$ curl http://localhost:8787/api/hello3 +Hello, Hello, Hello world! +``` + diff --git a/_templates/cloudflare/pages-tinygo/functions/api/[[routes]].mjs b/_templates/cloudflare/pages-tinygo/functions/api/[[routes]].mjs new file mode 100644 index 0000000..d647561 --- /dev/null +++ b/_templates/cloudflare/pages-tinygo/functions/api/[[routes]].mjs @@ -0,0 +1,6 @@ +import mod from "../../build/app.wasm"; +import * as imports from "../../build/shim.mjs" + +imports.init(mod); + +export const onRequest = imports.onRequest; diff --git a/_templates/cloudflare/pages-tinygo/go.mod b/_templates/cloudflare/pages-tinygo/go.mod new file mode 100644 index 0000000..35458cc --- /dev/null +++ b/_templates/cloudflare/pages-tinygo/go.mod @@ -0,0 +1,9 @@ +module github.com/syumai/workers/_examples/pages-functions + +go 1.18 + +require github.com/syumai/workers v0.0.0 + +require github.com/go-chi/chi/v5 v5.0.8 // indirect + +replace github.com/syumai/workers => ../../ diff --git a/_templates/cloudflare/pages-tinygo/go.sum b/_templates/cloudflare/pages-tinygo/go.sum new file mode 100644 index 0000000..2ad8d91 --- /dev/null +++ b/_templates/cloudflare/pages-tinygo/go.sum @@ -0,0 +1,2 @@ +github.com/go-chi/chi/v5 v5.0.8 h1:lD+NLqFcAi1ovnVZpsnObHGW4xb4J8lNmoYVfECH1Y0= +github.com/go-chi/chi/v5 v5.0.8/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= diff --git a/_templates/cloudflare/pages-tinygo/main.go b/_templates/cloudflare/pages-tinygo/main.go new file mode 100644 index 0000000..de6c6d1 --- /dev/null +++ b/_templates/cloudflare/pages-tinygo/main.go @@ -0,0 +1,29 @@ +package main + +import ( + "fmt" + "net/http" + + "github.com/go-chi/chi/v5" + "github.com/syumai/workers" +) + +func main() { + r := chi.NewRouter() + r.Route("/api", func(r chi.Router) { + r.Get("/hello", func(w http.ResponseWriter, req *http.Request) { + name := req.URL.Query().Get("name") + if name == "" { + name = "Pages Functions" + } + fmt.Fprintf(w, "Hello, %s!", name) + }) + r.Get("/hello2", func(w http.ResponseWriter, req *http.Request) { + fmt.Fprintf(w, "Hello, Hello world!") + }) + r.Get("/hello3", func(w http.ResponseWriter, req *http.Request) { + fmt.Fprintf(w, "Hello, Hello, Hello world!") + }) + }) + workers.Serve(r) +} diff --git a/_templates/cloudflare/pages-tinygo/pages/index.html b/_templates/cloudflare/pages-tinygo/pages/index.html new file mode 100644 index 0000000..dba0c9e --- /dev/null +++ b/_templates/cloudflare/pages-tinygo/pages/index.html @@ -0,0 +1,20 @@ + + + + + + +

+ /api/hello +

+

+ /api/hello?name=Example +

+

+ /api/hello2 +

+

+ /api/hello3 +

+ + diff --git a/_templates/cloudflare/pages-tinygo/wrangler.toml b/_templates/cloudflare/pages-tinygo/wrangler.toml new file mode 100644 index 0000000..0a5f9b6 --- /dev/null +++ b/_templates/cloudflare/pages-tinygo/wrangler.toml @@ -0,0 +1,6 @@ +name = "pages-functions-tinygo" +compatibility_date = "2023-04-30" + +[build] +command = "make build" + From c5858041ea0d528e072c4f0074a76017699dd0c2 Mon Sep 17 00:00:00 2001 From: Allison Durham Date: Tue, 30 May 2023 08:30:51 -0500 Subject: [PATCH 2/2] Update _templates/cloudflare/pages-tinygo/pages/index.html Co-authored-by: syumai --- _templates/cloudflare/pages-tinygo/pages/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_templates/cloudflare/pages-tinygo/pages/index.html b/_templates/cloudflare/pages-tinygo/pages/index.html index dba0c9e..104f8b7 100644 --- a/_templates/cloudflare/pages-tinygo/pages/index.html +++ b/_templates/cloudflare/pages-tinygo/pages/index.html @@ -8,7 +8,7 @@ /api/hello

- /api/hello?name=Example + /api/hello?name=Example

/api/hello2