-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from aki-0421/fetch-event
Full support for FetchEvent
- Loading branch information
Showing
14 changed files
with
135 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
.PHONY: dev | ||
dev: | ||
wrangler dev | ||
|
||
.PHONY: build | ||
build: | ||
mkdir -p dist | ||
#tinygo build -o ./dist/app.wasm -target wasm ./... | ||
tinygo build -o ./dist/app.wasm -target wasm -no-debug ./... | ||
|
||
.PHONY: publish | ||
publish: | ||
wrangler publish |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# [FetchEvent](https://developers.cloudflare.com/workers/runtime-apis/fetch-event/) | ||
|
||
Normally, workers are designed to return some kind of HTTP Response and exit immediately upon receiving an HTTP request. `FetchEvent` can extend these life cycles. | ||
|
||
#### WaitUntil | ||
|
||
`WaitUntil` extends the lifetime of the "fetch" event. It accepts an asynchronous task which the Workers runtime will execute without blocking the response. The worker will not be terminated until those tasks are completed. | ||
|
||
#### PassThroughOnException | ||
|
||
`PassThroughOnException` prevents a runtime error response when the Worker script throws an unhandled exception. Instead, the request forwards to the origin server as if it had not gone through the worker. | ||
|
||
## Example | ||
|
||
### Usecase | ||
|
||
You have decided to implement a log stream API to capture all access logs. You must edit the Headers so that the user's API token is not logged. If an unknown error occurs during this process, the entire service will be down, which must be avoided. | ||
In such cases, declare PassThroughOnException first and use WaitUntil for logging. | ||
|
||
### Setup | ||
|
||
This example worker is triggered by [Routes](https://developers.cloudflare.com/workers/platform/triggers/routes/). To try this example, add your site to cloudflare and add some records(A and CNAME, etc.) so that you can actually access the website. | ||
If your domain is `sub.example.com`, edit `wrangler.toml` as following: | ||
|
||
```toml | ||
routes = [ | ||
{ pattern = "sub.example.com/*", zone_name = "example.com" } | ||
] | ||
``` | ||
|
||
The workers is executed if the URL matches `sub.example.com/*`. | ||
|
||
### 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 publish # publish worker | ||
``` |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httputil" | ||
"time" | ||
|
||
"github.com/syumai/workers" | ||
"github.com/syumai/workers/cloudflare" | ||
"github.com/syumai/workers/cloudflare/fetch" | ||
) | ||
|
||
func handler(w http.ResponseWriter, req *http.Request) { | ||
ctx := req.Context() | ||
|
||
cloudflare.PassThroughOnException(ctx) | ||
|
||
// logging after responding | ||
cloudflare.WaitUntil(ctx, func() { | ||
for i := 0; i < 5; i++ { | ||
time.Sleep(time.Second) | ||
} | ||
fmt.Println("5-second task completed") | ||
}) | ||
|
||
// panic if x-error header has provided | ||
if req.Header.Get("x-error") != "" { | ||
panic("error") | ||
} | ||
|
||
// responds with origin server | ||
fc := fetch.NewClient() | ||
proxy := httputil.ReverseProxy{ | ||
Transport: fc.HTTPClient().Transport, | ||
Director: func(r *http.Request) { | ||
r.URL = req.URL | ||
}, | ||
} | ||
|
||
proxy.ServeHTTP(w, req) | ||
} | ||
|
||
func main() { | ||
workers.Serve(http.HandlerFunc(handler)) | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
name = "fetch-event" | ||
main = "./worker.mjs" | ||
compatibility_date = "2023-02-24" | ||
|
||
routes = [ | ||
{ pattern = "example.com/*", zone_name = "example.com" } | ||
] | ||
|
||
[build] | ||
command = "make build" |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.