diff --git a/_examples/pages-functions/README.md b/_examples/pages-functions/README.md index 7bdd361..8715f90 100644 --- a/_examples/pages-functions/README.md +++ b/_examples/pages-functions/README.md @@ -18,7 +18,7 @@ This project requires these tools to be installed globally. ### Commands ``` -make dev # run dev server make build # build Go Wasm binary +make dev # run dev server make publish # publish worker ``` diff --git a/_examples/pages-functions/functions/api/[[routes]].mjs b/_examples/pages-functions/functions/api/[[routes]].mjs new file mode 100644 index 0000000..d647561 --- /dev/null +++ b/_examples/pages-functions/functions/api/[[routes]].mjs @@ -0,0 +1,6 @@ +import mod from "../../build/app.wasm"; +import * as imports from "../../build/shim.mjs" + +imports.init(mod); + +export const onRequest = imports.onRequest; diff --git a/_examples/pages-functions/functions/hello.js b/_examples/pages-functions/functions/hello.js deleted file mode 100644 index dea0a84..0000000 --- a/_examples/pages-functions/functions/hello.js +++ /dev/null @@ -1,6 +0,0 @@ -import mod from "../build/app.wasm"; -import * as imports from "../build/shim.mjs" - -imports.init(mod); - -export const onRequest = imports.onRequest; diff --git a/_examples/pages-functions/go.mod b/_examples/pages-functions/go.mod index a141603..35458cc 100644 --- a/_examples/pages-functions/go.mod +++ b/_examples/pages-functions/go.mod @@ -4,4 +4,6 @@ go 1.18 require github.com/syumai/workers v0.0.0 +require github.com/go-chi/chi/v5 v5.0.8 // indirect + replace github.com/syumai/workers => ../../ diff --git a/_examples/pages-functions/go.sum b/_examples/pages-functions/go.sum index e69de29..2ad8d91 100644 --- a/_examples/pages-functions/go.sum +++ b/_examples/pages-functions/go.sum @@ -0,0 +1,2 @@ +github.com/go-chi/chi/v5 v5.0.8 h1:lD+NLqFcAi1ovnVZpsnObHGW4xb4J8lNmoYVfECH1Y0= +github.com/go-chi/chi/v5 v5.0.8/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= diff --git a/_examples/pages-functions/main.go b/_examples/pages-functions/main.go index 5c53629..de6c6d1 100644 --- a/_examples/pages-functions/main.go +++ b/_examples/pages-functions/main.go @@ -4,16 +4,26 @@ import ( "fmt" "net/http" + "github.com/go-chi/chi/v5" "github.com/syumai/workers" ) func main() { - handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { - name := req.URL.Query().Get("name") - if name == "" { - name = "Pages Functions" - } - fmt.Fprintf(w, "Hello, %s!", name) + r := chi.NewRouter() + r.Route("/api", func(r chi.Router) { + r.Get("/hello", func(w http.ResponseWriter, req *http.Request) { + name := req.URL.Query().Get("name") + if name == "" { + name = "Pages Functions" + } + fmt.Fprintf(w, "Hello, %s!", name) + }) + r.Get("/hello2", func(w http.ResponseWriter, req *http.Request) { + fmt.Fprintf(w, "Hello, Hello world!") + }) + r.Get("/hello3", func(w http.ResponseWriter, req *http.Request) { + fmt.Fprintf(w, "Hello, Hello, Hello world!") + }) }) - workers.Serve(handler) + workers.Serve(r) } diff --git a/_examples/pages-functions/pages/index.html b/_examples/pages-functions/pages/index.html index 64e5d20..f877aff 100644 --- a/_examples/pages-functions/pages/index.html +++ b/_examples/pages-functions/pages/index.html @@ -5,10 +5,16 @@

- /hello + /api/hello

- /syumai + /api/hello?name=syumai +

+

+ /api/hello2 +

+

+ /api/hello3

diff --git a/_examples/pages-functions/wrangler.toml b/_examples/pages-functions/wrangler.toml index 2850979..29ea062 100644 --- a/_examples/pages-functions/wrangler.toml +++ b/_examples/pages-functions/wrangler.toml @@ -1,5 +1,2 @@ name = "pages-functions" compatibility_date = "2023-04-30" - -[build] -command = "make build"