mirror of
https://github.com/syumai/workers.git
synced 2025-03-10 17:29:11 +00:00
use chi router in pages functions example
This commit is contained in:
parent
26d2a5a0fa
commit
2501fe849e
@ -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
|
||||
```
|
||||
|
6
_examples/pages-functions/functions/api/[[routes]].mjs
Normal file
6
_examples/pages-functions/functions/api/[[routes]].mjs
Normal 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;
|
@ -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;
|
@ -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 => ../../
|
||||
|
@ -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=
|
@ -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)
|
||||
}
|
||||
|
@ -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>
|
||||
|
@ -1,5 +1,2 @@
|
||||
name = "pages-functions"
|
||||
compatibility_date = "2023-04-30"
|
||||
|
||||
[build]
|
||||
command = "make build"
|
||||
|
Loading…
x
Reference in New Issue
Block a user