mirror of
https://github.com/syumai/workers.git
synced 2025-03-10 09:27:08 +00:00
add multiple-handlers example
This commit is contained in:
parent
68bb0c7e08
commit
fc07f1d66e
3
_examples/multiple-handlers/.gitignore
vendored
Normal file
3
_examples/multiple-handlers/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
build
|
||||
node_modules
|
||||
.wrangler
|
12
_examples/multiple-handlers/Makefile
Normal file
12
_examples/multiple-handlers/Makefile
Normal file
@ -0,0 +1,12 @@
|
||||
.PHONY: dev
|
||||
dev:
|
||||
wrangler dev --test-scheduled
|
||||
|
||||
.PHONY: build
|
||||
build:
|
||||
go run ../../cmd/workers-assets-gen
|
||||
tinygo build -o ./build/app.wasm -target wasm -no-debug ./...
|
||||
|
||||
.PHONY: deploy
|
||||
deploy:
|
||||
wrangler deploy
|
29
_examples/multiple-handlers/README.md
Normal file
29
_examples/multiple-handlers/README.md
Normal file
@ -0,0 +1,29 @@
|
||||
# multiple-handlers
|
||||
|
||||
* This example shows how to use multiple handlers in a single worker.
|
||||
|
||||
## Development
|
||||
|
||||
### Requirements
|
||||
|
||||
This project requires these tools to be installed globally.
|
||||
|
||||
* wrangler
|
||||
* tinygo
|
||||
|
||||
### Commands
|
||||
|
||||
```
|
||||
make dev # run dev server
|
||||
make build # build Go Wasm binary
|
||||
make deploy # deploy worker
|
||||
```
|
||||
|
||||
#### Testing cron schedule
|
||||
|
||||
* With curl command below, you can test the cron schedule.
|
||||
- see: https://developers.cloudflare.com/workers/runtime-apis/handlers/scheduled/#background
|
||||
|
||||
```
|
||||
curl "http://localhost:8787/__scheduled?cron=*+*+*+*+*"
|
||||
```
|
7
_examples/multiple-handlers/go.mod
Normal file
7
_examples/multiple-handlers/go.mod
Normal file
@ -0,0 +1,7 @@
|
||||
module github.com/syumai/workers/_examples/multiple-handlers
|
||||
|
||||
go 1.21.3
|
||||
|
||||
require github.com/syumai/workers v0.0.0
|
||||
|
||||
replace github.com/syumai/workers => ../../
|
0
_examples/multiple-handlers/go.sum
Normal file
0
_examples/multiple-handlers/go.sum
Normal file
38
_examples/multiple-handlers/main.go
Normal file
38
_examples/multiple-handlers/main.go
Normal file
@ -0,0 +1,38 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/syumai/workers"
|
||||
"github.com/syumai/workers/cloudflare/cron"
|
||||
)
|
||||
|
||||
func main() {
|
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||
fmt.Fprint(w, "Hello, world!")
|
||||
})
|
||||
|
||||
task := func(ctx context.Context) error {
|
||||
e, err := cron.NewEvent(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(e.ScheduledTime.Unix())
|
||||
return nil
|
||||
}
|
||||
|
||||
// set up the worker
|
||||
workers.ServeNonBlock(handler)
|
||||
cron.ScheduleTaskNonBlock(task)
|
||||
|
||||
// send a ready signal to the runtime
|
||||
workers.Ready()
|
||||
|
||||
// block until the handler or task is done
|
||||
select {
|
||||
case <-workers.Done():
|
||||
case <-cron.Done():
|
||||
}
|
||||
}
|
10
_examples/multiple-handlers/wrangler.toml
Normal file
10
_examples/multiple-handlers/wrangler.toml
Normal file
@ -0,0 +1,10 @@
|
||||
name = "multiple-handlers"
|
||||
main = "./build/worker.mjs"
|
||||
compatibility_date = "2023-02-24"
|
||||
workers_dev = false
|
||||
|
||||
[triggers]
|
||||
crons = ["* * * * *"]
|
||||
|
||||
[build]
|
||||
command = "make build"
|
Loading…
x
Reference in New Issue
Block a user