Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
a0acac5
rewrite basic-auth-proxy example using go instead of tinygo
nekojanai Apr 30, 2025
b77ed97
fix disallow request body for GET and HEAD
nekojanai Apr 30, 2025
120df11
rewrite cache example using go instead of tinygo
nekojanai May 1, 2025
acdb1d3
fix disallow request body for GET and HEAD in cache example
nekojanai May 1, 2025
aadaf96
rewrite cron example using go instead of tinygo
nekojanai May 1, 2025
42334ed
rewrite d1-blog-server example using go instead of tinygo
nekojanai May 1, 2025
c56dc70
rewrite durable-object-counter example using go instead of tinygo
nekojanai May 1, 2025
2d4669c
rewrite env example using go instead of tinygo
nekojanai May 1, 2025
2b2d084
rewrite fetch example using go instead of tinygo
nekojanai May 1, 2025
ee60009
rewrite fetch-event example using go instead of tinygo
nekojanai May 1, 2025
6260f56
rewrite incoming example using go instead of tinygo
nekojanai May 1, 2025
e484d7c
rewrite kw-counter example using go instead of tinygo
nekojanai May 1, 2025
7bf7593
rewrite multiple-handlers example using go instead of tinygo
nekojanai May 1, 2025
2173af0
rewrite pages-functions example using go instead of tinygo
nekojanai May 1, 2025
22cf5dc
rewrite queues example using go instead of tinygo
nekojanai May 2, 2025
488eb3d
rewrite r2-image-server example using go instead of tinygo
nekojanai May 2, 2025
1a434a8
rewrite r2-image-viewer example using go instead of tinygo
nekojanai May 2, 2025
2571165
rewrite service-bindings example using go instead of tinygo
nekojanai May 2, 2025
10917d2
rewrite simple-json-server example using go instead of tinygo
nekojanai May 2, 2025
43fbf21
rewrite stream-large-file example using go instead of tinygo
nekojanai May 2, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions _examples/basic-auth-proxy/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ dev:

.PHONY: build
build:
go run ../../cmd/workers-assets-gen
tinygo build -o ./build/app.wasm -target wasm -no-debug ./...
go run ../../cmd/workers-assets-gen -mode=go
GOOS=js GOARCH=wasm go build -o ./build/app.wasm .

.PHONY: deploy
deploy:
Expand Down
4 changes: 2 additions & 2 deletions _examples/basic-auth-proxy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
This project requires these tools to be installed globally.

* wrangler
* tinygo
* Go 1.24.0 or later

### Commands

```
make dev # run dev server
make build # build Go Wasm binary
make deploy # deploy worker
make deploy # deploy worker
```
15 changes: 14 additions & 1 deletion _examples/basic-auth-proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const (
userPassword = "password"
)

func canHaveBody(method string) bool {
return method != "GET" && method != "HEAD" && method != ""
}

func authenticate(req *http.Request) bool {
username, password, ok := req.BasicAuth()
return ok && username == userName && password == userPassword
Expand All @@ -33,7 +37,16 @@ func handleRequest(w http.ResponseWriter, req *http.Request) {
u := *req.URL
u.Scheme = "https"
u.Host = "syum.ai"
r, err := fetch.NewRequest(req.Context(), req.Method, u.String(), req.Body)

// disallow setting body for GET and HEAD
// see https://github.com/whatwg/fetch/issues/551
var r *fetch.Request
var err error
if canHaveBody(req.Method) {
r, err = fetch.NewRequest(req.Context(), req.Method, u.String(), req.Body)
} else {
r, err = fetch.NewRequest(req.Context(), req.Method, u.String(), nil)
}
if err != nil {
handleError(w, http.StatusInternalServerError, "Internal Error")
log.Printf("failed to initialize proxy request: %v\n", err)
Expand Down
5 changes: 2 additions & 3 deletions _examples/cache/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ dev:

.PHONY: build
build:
go run ../../cmd/workers-assets-gen
#tinygo build -o ./build/app.wasm -target wasm -no-debug ./...
tinygo build -o ./build/app.wasm -target wasm -no-debug ./...
go run ../../cmd/workers-assets-gen -mode=go
GOOS=js GOARCH=wasm go build -o ./build/app.wasm .

.PHONY: deploy
deploy:
Expand Down
4 changes: 2 additions & 2 deletions _examples/cache/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ The Cache API allows fine grained control of reading and writing from the Cloudf
This project requires these tools to be installed globally.

* wrangler
* tinygo
* Go 1.24.0 or later

#### Commands

```
make dev # run dev server
make build # build Go Wasm binary
make deploy # deploy worker
make deploy # deploy worker
```
8 changes: 8 additions & 0 deletions _examples/cache/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (
"github.com/syumai/workers/cloudflare/cache"
)

func canHaveBody(method string) bool {
return method != "GET" && method != "HEAD" && method != ""
}

type responseWriter struct {
http.ResponseWriter
StatusCode int
Expand Down Expand Up @@ -40,6 +44,10 @@ func handler(w http.ResponseWriter, req *http.Request) {
rw := responseWriter{ResponseWriter: w}
c := cache.New()

if !canHaveBody(req.Method) {
req.Body = nil
}

// Find cache
res, _ := c.Match(req, nil)
if res != nil {
Expand Down
4 changes: 2 additions & 2 deletions _examples/cron/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ dev:

.PHONY: build
build:
go run ../../cmd/workers-assets-gen
tinygo build -o ./build/app.wasm -target wasm -no-debug ./...
go run ../../cmd/workers-assets-gen -mode=go
GOOS=js GOARCH=wasm go build -o ./build/app.wasm .

.PHONY: deploy
deploy:
Expand Down
4 changes: 2 additions & 2 deletions _examples/cron/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
This project requires these tools to be installed globally.

* wrangler
* tinygo
* Go 1.24.0 or later

### Commands

```
make dev # run dev server
make build # build Go Wasm binary
make deploy # deploy worker
make deploy # deploy worker
```

#### Testing cron schedule
Expand Down
10 changes: 7 additions & 3 deletions _examples/d1-blog-server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ dev:

.PHONY: build
build:
go run ../../cmd/workers-assets-gen
tinygo build -o ./build/app.wasm -target wasm -no-debug ./main.go
go run ../../cmd/workers-assets-gen -mode=go
GOOS=js GOARCH=wasm go build -o ./build/app.wasm .

.PHONY: deploy
deploy:
wrangler deploy

.PHONY: create-db
create-db:
wrangler d1 create d1-blog-server

.PHONY: init-db
init-db:
wrangler d1 execute d1-blog-server --file=./schema.sql
wrangler d1 execute d1-blog-server --remote --file=./schema.sql

.PHONY: init-db-local
init-db-local:
Expand Down
10 changes: 6 additions & 4 deletions _examples/d1-blog-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ $ curl 'https://d1-blog-server.syumai.workers.dev/articles'
This project requires these tools to be installed globally.

* wrangler
* tinygo
* Go 1.24.0 or later

### Commands

Expand All @@ -70,8 +70,10 @@ make dev # run dev server
make build # build Go Wasm binary

# production
make init-db # initialize production DB (remove all rows)
make deploy # deploy worker
make create-db # create production DB
# copy binding to wrangler.toml
make init-db # initialize production DB (remove all rows)
make deploy # deploy worker
```

* Notice: This example uses raw SQL commands to initialize the DB for simplicity, but in general you should use `wrangler d1 migraions` for your application.
* Notice: This example uses raw SQL commands to initialize the DB for simplicity, but in general you should use `wrangler d1 migrations` for your application.
2 changes: 1 addition & 1 deletion _examples/d1-blog-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func main() {
db, err := sql.Open("d1", "BlogDB")
db, err := sql.Open("d1", "DB")
if err != nil {
log.Fatalf("error opening DB: %s", err.Error())
}
Expand Down
6 changes: 0 additions & 6 deletions _examples/d1-blog-server/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,3 @@ compatibility_date = "2024-04-15"

[build]
command = "make build"

[[ d1_databases ]]
binding = "BlogDB"
database_name = "d1-blog-server"
database_id = "c6d5e4c0-3134-42fa-834c-812b24fea3cf"
preview_database_id = "BlogDB"
4 changes: 2 additions & 2 deletions _examples/durable-object-counter/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ dev:

.PHONY: build
build:
go run ../../cmd/workers-assets-gen
tinygo build -o ./build/app.wasm -target wasm -no-debug ./...
go run ../../cmd/workers-assets-gen -mode=go
GOOS=js GOARCH=wasm go build -o ./build/app.wasm ./...

.PHONY: deploy
deploy:
Expand Down
4 changes: 2 additions & 2 deletions _examples/durable-object-counter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ After `make deploy` the trigger is `http://durable-object-counter.YOUR-DOMAIN.wo
This project requires these tools to be installed globally.

* wrangler
* tinygo
* Go 1.24.0 or later

### Commands

```
make dev # run dev server
make build # build Go Wasm binary
make deploy # deploy worker
make deploy # deploy worker
```

## Author
Expand Down
8 changes: 8 additions & 0 deletions _examples/durable-object-counter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ func main() {
workers.Serve(&MyHandler{})
}

func canHaveBody(method string) bool {
return method != "GET" && method != "HEAD" && method != ""
}

type MyHandler struct{}

func (_ *MyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
Expand All @@ -26,6 +30,10 @@ func (_ *MyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
panic(err)
}

if !canHaveBody(req.Method) {
req.Body = nil
}

res, err := obj.Fetch(req)
if err != nil {
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion _examples/durable-object-counter/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ bindings = [{name = "COUNTER", class_name = "Counter"}]

[[migrations]]
tag = "v1" # Should be unique for each entry
new_classes = ["Counter"]
new_sqlite_classes = ["Counter"]
4 changes: 2 additions & 2 deletions _examples/env/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ dev:

.PHONY: build
build:
go run ../../cmd/workers-assets-gen
tinygo build -o ./build/app.wasm -target wasm -no-debug ./...
go run ../../cmd/workers-assets-gen -mode=go
GOOS=js GOARCH=wasm go build -o ./build/app.wasm ./...

.PHONY: deploy
deploy:
Expand Down
4 changes: 2 additions & 2 deletions _examples/env/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
This project requires these tools to be installed globally.

* wrangler
* tinygo
* Go 1.24.0 or later

### Commands

```
make dev # run dev server
make build # build Go Wasm binary
make deploy # deploy worker
make deploy # deploy worker
```
4 changes: 2 additions & 2 deletions _examples/fetch-event/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ dev:

.PHONY: build
build:
go run ../../cmd/workers-assets-gen
tinygo build -o ./build/app.wasm -target wasm -no-debug ./...
go run ../../cmd/workers-assets-gen -mode=go
GOOS=js GOARCH=wasm go build -o ./build/app.wasm ./...

.PHONY: deploy
deploy:
Expand Down
4 changes: 2 additions & 2 deletions _examples/fetch-event/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ The workers is executed if the URL matches `sub.example.com/*`.
This project requires these tools to be installed globally.

* wrangler
* tinygo
* Go 1.24.0 or later

#### Commands

```
make dev # run dev server
make build # build Go Wasm binary
make deploy # deploy worker
make deploy # deploy worker
```
7 changes: 4 additions & 3 deletions _examples/fetch-event/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ name = "fetch-event"
main = "./build/worker.mjs"
compatibility_date = "2023-02-24"

routes = [
{ pattern = "example.com/*", zone_name = "example.com" }
]
# setup route and uncomment
# routes = [
# { pattern = "example.com/*", zone_name = "example.com" }
# ]

[build]
command = "make build"
4 changes: 2 additions & 2 deletions _examples/fetch/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ dev:

.PHONY: build
build:
go run ../../cmd/workers-assets-gen
tinygo build -o ./build/app.wasm -target wasm -no-debug ./...
go run ../../cmd/workers-assets-gen -mode=go
GOOS=js GOARCH=wasm go build -o ./build/app.wasm ./...

.PHONY: deploy
deploy:
Expand Down
5 changes: 2 additions & 3 deletions _examples/fetch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@
This project requires these tools to be installed globally.

* wrangler
* tinygo

* Go 1.24.0 or later
### Commands

```
make dev # run dev server
make build # build Go Wasm binary
make deploy # deploy worker
make deploy # deploy worker
```
4 changes: 2 additions & 2 deletions _examples/incoming/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ dev:

.PHONY: build
build:
go run ../../cmd/workers-assets-gen
tinygo build -o ./build/app.wasm -target wasm -no-debug ./...
go run ../../cmd/workers-assets-gen -mode=go
GOOS=js GOARCH=wasm go build -o ./build/app.wasm ./...

.PHONY: deploy
deploy:
Expand Down
8 changes: 6 additions & 2 deletions _examples/kv-counter/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ dev:

.PHONY: build
build:
go run ../../cmd/workers-assets-gen
tinygo build -o ./build/app.wasm -target wasm -no-debug ./...
go run ../../cmd/workers-assets-gen -mode=go
GOOS=js GOARCH=wasm go build -o ./build/app.wasm ./...

.PHONY: create-kv-namespace
create-kv-namespace:
wrangler kv namespace create COUNTER

.PHONY: deploy
deploy:
Expand Down
9 changes: 5 additions & 4 deletions _examples/kv-counter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
This project requires these tools to be installed globally.

* wrangler
* tinygo
* Go 1.24.0 or later

### Commands

```
make dev # run dev server
make build # build Go Wasm binary
make deploy # deploy worker
make dev # run dev server
make build # build Go Wasm binary
make create-kv-namespace # creates a kv namespace - add binding to wrangler.toml before deploy
make deploy # deploy worker
```
Loading
Loading