use C3 for templates

This commit is contained in:
syumai 2025-02-23 23:54:29 +09:00
parent b6a96f14c7
commit 04efeaacf5
12 changed files with 102 additions and 81 deletions

View File

@ -65,25 +65,48 @@ For concrete examples, see `_examples` directory.
## Quick Start
First, please install the following tools:
### Requirements
* Node.js (and npm)
* [wrangler](https://developers.cloudflare.com/workers/wrangler/)
- You can install it by running `npm install -g wrangler`.
* Go 1.24.0 or later
* [gonew](https://pkg.go.dev/golang.org/x/tools/cmd/gonew)
- You can install it by running `go install golang.org/x/tools/cmd/gonew@latest`
After installation, please run the following commands.
### Create a new Worker project
Run the following command:
```console
gonew github.com/syumai/workers/_templates/cloudflare/worker-go your.module/my-app # e.g. github.com/syumai/my-app
cd my-app
go mod tidy
make dev # start running dev server
curl http://localhost:8787/hello # outputs "Hello!"
npm create cloudflare@latest -- --template github.com/syumai/workers/_templates/cloudflare/worker-go
```
### Initialize the project
1. Navigate to your new project directory:
```console
cd my-app
```
2. Initialize Go modules:
```console
go mod init
go mod tidy
```
3. Start the development server:
```console
npm start
```
4. Verify the worker is running:
```console
curl http://localhost:8787/hello
```
You will see **"Hello!"** as the response.
If you want a more detailed description, please refer to the README.md file in the generated directory.
## FAQ

View File

@ -1,12 +0,0 @@
.PHONY: dev
dev:
wrangler dev
.PHONY: build
build:
go run github.com/syumai/workers/cmd/workers-assets-gen -mode=go
GOOS=js GOARCH=wasm go build -o ./build/app.wasm .
.PHONY: deploy
deploy:
wrangler deploy

View File

@ -5,8 +5,7 @@
## 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).
- Go (not TinyGo) with many dependencies may exceed the size limit of the Worker (3MB for free plan, 10MB for paid plan). In that case, you can use the [TinyGo template](https://github.com/syumai/workers/tree/main/_templates/cloudflare/worker-tinygo) instead.
## Usage
@ -15,39 +14,36 @@
## Requirements
- Node.js
- [wrangler](https://developers.cloudflare.com/workers/wrangler/)
- just run `npm install -g wrangler`
- Go 1.21.0 or later
- Go 1.24.0 or later
## Getting Started
* If not already installed, please install the [gonew](https://pkg.go.dev/golang.org/x/tools/cmd/gonew) command.
- Create a new worker project using this template.
```console
go install golang.org/x/tools/cmd/gonew@latest
npm create cloudflare@latest -- --template github.com/syumai/workers/_templates/cloudflare/worker-go
```
* Create a new project using this template.
- Second argument passed to `gonew` is a module path of your new app.
- Initialize a project.
```console
gonew github.com/syumai/workers/_templates/cloudflare/worker-go your.module/my-app # e.g. github.com/syumai/my-app
cd my-app
go mod init
go mod tidy
make dev # start running dev server
npm start # start running dev server
curl http://localhost:8787/hello # outputs "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 deploy # deploy worker
npm start # run dev server
# or
go run . # run dev server without Wrangler (Cloudflare-related features are not available)
npm run build # build Go Wasm binary
npm run deploy # deploy worker
```
### Testing dev server

View File

@ -0,0 +1,14 @@
{
"name": "<TBD>",
"version": "0.0.0",
"private": true,
"scripts": {
"build": "go run github.com/syumai/workers/cmd/workers-assets-gen -mode=go && GOOS=js GOARCH=wasm go build -o ./build/app.wasm .",
"deploy": "wrangler deploy",
"dev": "wrangler dev",
"start": "wrangler dev"
},
"devDependencies": {
"wrangler": "^3.109.2"
}
}

View File

@ -0,0 +1,8 @@
{
"name": "<TBD>",
"main": "./build/worker.mjs",
"compatibility_date": "<TBD>",
"build": {
"command": "npm run build"
}
}

View File

@ -1,6 +0,0 @@
name = "go-worker"
main = "./build/worker.mjs"
compatibility_date = "2024-04-15"
[build]
command = "make build"

View File

@ -1,12 +0,0 @@
.PHONY: dev
dev:
wrangler dev
.PHONY: build
build:
go run github.com/syumai/workers/cmd/workers-assets-gen
tinygo build -o ./build/app.wasm -target wasm -no-debug ./...
.PHONY: deploy
deploy:
wrangler deploy

View File

@ -10,39 +10,36 @@
## Requirements
- Node.js
- [wrangler](https://developers.cloudflare.com/workers/wrangler/)
- just run `npm install -g wrangler`
- tinygo 0.29.0 or later
- tinygo 0.35.0 or later
## Getting Started
* If not already installed, please install the [gonew](https://pkg.go.dev/golang.org/x/tools/cmd/gonew) command.
- Create a new worker project using this template.
```console
go install golang.org/x/tools/cmd/gonew@latest
npm create cloudflare@latest -- --template github.com/syumai/workers/_templates/cloudflare/worker-tinygo
```
* Create a new project using this template.
- Second argument passed to `gonew` is a module path of your new app.
- Initialize a project.
```console
gonew github.com/syumai/workers/_templates/cloudflare/worker-tinygo your.module/my-app # e.g. github.com/syumai/my-app
cd my-app
cd my-app # A directory of the project created by the above command
go mod init
go mod tidy
make dev # start running dev server
npm start # start running dev server
curl http://localhost:8787/hello # outputs "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 deploy # deploy worker
npm start # run dev server
# or
go run . # run dev server without Wrangler (Cloudflare-related features are not available)
npm run build # build Go Wasm binary
npm run deploy # deploy worker
```
### Testing dev server

View File

@ -1,3 +0,0 @@
module github.com/syumai/workers/_templates/cloudflare/worker-tinygo
go 1.21.1

View File

@ -0,0 +1,14 @@
{
"name": "<TBD>",
"version": "0.0.0",
"private": true,
"scripts": {
"build": "go run github.com/syumai/workers/cmd/workers-assets-gen && tinygo build -o ./build/app.wasm -target wasm -no-debug ./...",
"deploy": "wrangler deploy",
"dev": "wrangler dev",
"start": "wrangler dev"
},
"devDependencies": {
"wrangler": "^3.109.2"
}
}

View File

@ -0,0 +1,8 @@
{
"name": "<TBD>",
"main": "./build/worker.mjs",
"compatibility_date": "<TBD>",
"build": {
"command": "npm run build"
}
}

View File

@ -1,6 +0,0 @@
name = "tinygo-worker"
main = "./build/worker.mjs"
compatibility_date = "2024-04-15"
[build]
command = "make build"