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 ### Commands
``` ```
make dev # run dev server
make build # build Go Wasm binary make build # build Go Wasm binary
make dev # run dev server
make publish # publish worker 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/syumai/workers v0.0.0
require github.com/go-chi/chi/v5 v5.0.8 // indirect
replace github.com/syumai/workers => ../../ 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" "fmt"
"net/http" "net/http"
"github.com/go-chi/chi/v5"
"github.com/syumai/workers" "github.com/syumai/workers"
) )
func main() { func main() {
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { r := chi.NewRouter()
name := req.URL.Query().Get("name") r.Route("/api", func(r chi.Router) {
if name == "" { r.Get("/hello", func(w http.ResponseWriter, req *http.Request) {
name = "Pages Functions" name := req.URL.Query().Get("name")
} if name == "" {
fmt.Fprintf(w, "Hello, %s!", 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)
} }

View File

@ -5,10 +5,16 @@
</head> </head>
<body> <body>
<p> <p>
<a href="/hello">/hello</a> <a href="/api/hello">/api/hello</a>
</p> </p>
<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> </p>
</body> </body>
</html> </html>

View File

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