mirror of
https://github.com/syumai/workers.git
synced 2025-03-10 17:29:11 +00:00
add worker-go template
This commit is contained in:
parent
8978b3b2b0
commit
058165ca1c
1
_templates/cloudflare/worker-go/.gitignore
vendored
Normal file
1
_templates/cloudflare/worker-go/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
build
|
12
_templates/cloudflare/worker-go/Makefile
Normal file
12
_templates/cloudflare/worker-go/Makefile
Normal file
@ -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
|
58
_templates/cloudflare/worker-go/README.md
Normal file
58
_templates/cloudflare/worker-go/README.md
Normal file
@ -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
|
||||
```
|
24
_templates/cloudflare/worker-go/main.go
Normal file
24
_templates/cloudflare/worker-go/main.go
Normal file
@ -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
|
||||
}
|
9
_templates/cloudflare/worker-go/wrangler.toml
Normal file
9
_templates/cloudflare/worker-go/wrangler.toml
Normal file
@ -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"
|
Loading…
x
Reference in New Issue
Block a user