mirror of
https://github.com/syumai/workers.git
synced 2025-03-10 17:29:11 +00:00
Merge pull request #62 from AdjectiveAllison/pages-tinygo
added pages template
This commit is contained in:
commit
0c1ce96dae
12
_templates/cloudflare/pages-tinygo/Makefile
Normal file
12
_templates/cloudflare/pages-tinygo/Makefile
Normal file
@ -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
|
61
_templates/cloudflare/pages-tinygo/README.md
Normal file
61
_templates/cloudflare/pages-tinygo/README.md
Normal file
@ -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!
|
||||||
|
```
|
||||||
|
|
@ -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;
|
9
_templates/cloudflare/pages-tinygo/go.mod
Normal file
9
_templates/cloudflare/pages-tinygo/go.mod
Normal file
@ -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 => ../../
|
2
_templates/cloudflare/pages-tinygo/go.sum
Normal file
2
_templates/cloudflare/pages-tinygo/go.sum
Normal file
@ -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=
|
29
_templates/cloudflare/pages-tinygo/main.go
Normal file
29
_templates/cloudflare/pages-tinygo/main.go
Normal file
@ -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)
|
||||||
|
}
|
20
_templates/cloudflare/pages-tinygo/pages/index.html
Normal file
20
_templates/cloudflare/pages-tinygo/pages/index.html
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8" />
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<p>
|
||||||
|
<a href="/api/hello">/api/hello</a>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<a href="/api/hello?name=Example">/api/hello?name=Example</a>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<a href="/api/hello2">/api/hello2</a>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<a href="/api/hello3">/api/hello3</a>
|
||||||
|
</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
6
_templates/cloudflare/pages-tinygo/wrangler.toml
Normal file
6
_templates/cloudflare/pages-tinygo/wrangler.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
name = "pages-functions-tinygo"
|
||||||
|
compatibility_date = "2023-04-30"
|
||||||
|
|
||||||
|
[build]
|
||||||
|
command = "make build"
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user