mirror of
https://github.com/syumai/workers.git
synced 2025-03-10 17:29:11 +00:00
Merge pull request #157 from syumai/use-c3-for-templates
use C3 for templates
This commit is contained in:
commit
e57ef6f75b
45
README.md
45
README.md
@ -65,25 +65,48 @@ For concrete examples, see `_examples` directory.
|
|||||||
|
|
||||||
## Quick Start
|
## Quick Start
|
||||||
|
|
||||||
First, please install the following tools:
|
### Requirements
|
||||||
|
|
||||||
* Node.js (and npm)
|
* 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
|
* 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
|
```console
|
||||||
gonew github.com/syumai/workers/_templates/cloudflare/worker-go your.module/my-app # e.g. github.com/syumai/my-app
|
npm create cloudflare@latest -- --template github.com/syumai/workers/_templates/cloudflare/worker-go
|
||||||
cd my-app
|
|
||||||
go mod tidy
|
|
||||||
make dev # start running dev server
|
|
||||||
curl http://localhost:8787/hello # outputs "Hello!"
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### 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.
|
If you want a more detailed description, please refer to the README.md file in the generated directory.
|
||||||
|
|
||||||
## FAQ
|
## FAQ
|
||||||
|
@ -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
|
|
@ -5,8 +5,7 @@
|
|||||||
|
|
||||||
## Notice
|
## Notice
|
||||||
|
|
||||||
- A free plan Cloudflare Workers only accepts ~1MB 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.
|
||||||
- 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
|
## Usage
|
||||||
|
|
||||||
@ -15,39 +14,36 @@
|
|||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
- Node.js
|
- Node.js
|
||||||
- [wrangler](https://developers.cloudflare.com/workers/wrangler/)
|
- Go 1.24.0 or later
|
||||||
- just run `npm install -g wrangler`
|
|
||||||
- Go 1.21.0 or later
|
|
||||||
|
|
||||||
## Getting Started
|
## 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
|
```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.
|
- Initialize a project.
|
||||||
- Second argument passed to `gonew` is a module path of your new app.
|
|
||||||
|
|
||||||
```console
|
```console
|
||||||
gonew github.com/syumai/workers/_templates/cloudflare/worker-go your.module/my-app # e.g. github.com/syumai/my-app
|
|
||||||
cd my-app
|
cd my-app
|
||||||
|
go mod init
|
||||||
go mod tidy
|
go mod tidy
|
||||||
make dev # start running dev server
|
npm start # start running dev server
|
||||||
curl http://localhost:8787/hello # outputs "Hello!"
|
curl http://localhost:8787/hello # outputs "Hello!"
|
||||||
```
|
```
|
||||||
|
|
||||||
- To change worker name, please edit `name` property in `wrangler.toml`.
|
|
||||||
|
|
||||||
## Development
|
## Development
|
||||||
|
|
||||||
### Commands
|
### Commands
|
||||||
|
|
||||||
```
|
```
|
||||||
make dev # run dev server
|
npm start # run dev server
|
||||||
make build # build Go Wasm binary
|
# or
|
||||||
make deploy # deploy worker
|
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
|
### Testing dev server
|
||||||
|
14
_templates/cloudflare/worker-go/package.json
Normal file
14
_templates/cloudflare/worker-go/package.json
Normal 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"
|
||||||
|
}
|
||||||
|
}
|
8
_templates/cloudflare/worker-go/wrangler.jsonc
Normal file
8
_templates/cloudflare/worker-go/wrangler.jsonc
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"name": "<TBD>",
|
||||||
|
"main": "./build/worker.mjs",
|
||||||
|
"compatibility_date": "<TBD>",
|
||||||
|
"build": {
|
||||||
|
"command": "npm run build"
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +0,0 @@
|
|||||||
name = "go-worker"
|
|
||||||
main = "./build/worker.mjs"
|
|
||||||
compatibility_date = "2024-04-15"
|
|
||||||
|
|
||||||
[build]
|
|
||||||
command = "make build"
|
|
@ -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
|
|
@ -10,39 +10,36 @@
|
|||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
- Node.js
|
- Node.js
|
||||||
- [wrangler](https://developers.cloudflare.com/workers/wrangler/)
|
- tinygo 0.35.0 or later
|
||||||
- just run `npm install -g wrangler`
|
|
||||||
- tinygo 0.29.0 or later
|
|
||||||
|
|
||||||
## Getting Started
|
## 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
|
```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.
|
- Initialize a project.
|
||||||
- Second argument passed to `gonew` is a module path of your new app.
|
|
||||||
|
|
||||||
```console
|
```console
|
||||||
gonew github.com/syumai/workers/_templates/cloudflare/worker-tinygo your.module/my-app # e.g. github.com/syumai/my-app
|
cd my-app # A directory of the project created by the above command
|
||||||
cd my-app
|
go mod init
|
||||||
go mod tidy
|
go mod tidy
|
||||||
make dev # start running dev server
|
npm start # start running dev server
|
||||||
curl http://localhost:8787/hello # outputs "Hello!"
|
curl http://localhost:8787/hello # outputs "Hello!"
|
||||||
```
|
```
|
||||||
|
|
||||||
- To change worker name, please edit `name` property in `wrangler.toml`.
|
|
||||||
|
|
||||||
## Development
|
## Development
|
||||||
|
|
||||||
### Commands
|
### Commands
|
||||||
|
|
||||||
```
|
```
|
||||||
make dev # run dev server
|
npm start # run dev server
|
||||||
make build # build Go Wasm binary
|
# or
|
||||||
make deploy # deploy worker
|
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
|
### Testing dev server
|
||||||
|
@ -1,3 +0,0 @@
|
|||||||
module github.com/syumai/workers/_templates/cloudflare/worker-tinygo
|
|
||||||
|
|
||||||
go 1.21.1
|
|
14
_templates/cloudflare/worker-tinygo/package.json
Normal file
14
_templates/cloudflare/worker-tinygo/package.json
Normal 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"
|
||||||
|
}
|
||||||
|
}
|
8
_templates/cloudflare/worker-tinygo/wrangler.jsonc
Normal file
8
_templates/cloudflare/worker-tinygo/wrangler.jsonc
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"name": "<TBD>",
|
||||||
|
"main": "./build/worker.mjs",
|
||||||
|
"compatibility_date": "<TBD>",
|
||||||
|
"build": {
|
||||||
|
"command": "npm run build"
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +0,0 @@
|
|||||||
name = "tinygo-worker"
|
|
||||||
main = "./build/worker.mjs"
|
|
||||||
compatibility_date = "2024-04-15"
|
|
||||||
|
|
||||||
[build]
|
|
||||||
command = "make build"
|
|
Loading…
x
Reference in New Issue
Block a user