use chi router in pages functions example

This commit is contained in:
syumai 2023-04-30 13:52:21 +09:00
parent 26d2a5a0fa
commit 2501fe849e
8 changed files with 36 additions and 19 deletions

View File

@ -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
```

View File

@ -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;

View File

@ -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;

View File

@ -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 => ../../

View File

@ -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=

View File

@ -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) {
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)
})
workers.Serve(handler)
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(r)
}

View File

@ -5,10 +5,16 @@
</head>
<body>
<p>
<a href="/hello">/hello</a>
<a href="/api/hello">/api/hello</a>
</p>
<p>
<a href="/hello?name=syumai">/syumai</a>
<a href="/api/hello?name=syumai">/api/hello?name=syumai</a>
</p>
<p>
<a href="/api/hello2">/api/hello2</a>
</p>
<p>
<a href="/api/hello3">/api/hello3</a>
</p>
</body>
</html>

View File

@ -1,5 +1,2 @@
name = "pages-functions"
compatibility_date = "2023-04-30"
[build]
command = "make build"