mirror of
https://github.com/nlepage/go-wasm-http-server.git
synced 2026-01-12 01:59:14 +00:00
🚚 Move hello to its own dir
This commit is contained in:
28
docs/hello/api.go
Normal file
28
docs/hello/api.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
wasmhttp "github.com/nlepage/go-wasm-http-server"
|
||||
)
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/hello", func(res http.ResponseWriter, req *http.Request) {
|
||||
params := make(map[string]string)
|
||||
if err := json.NewDecoder(req.Body).Decode(¶ms); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if err := json.NewEncoder(res).Encode(map[string]string{
|
||||
"message": fmt.Sprintf("Hello %s!", params["name"]),
|
||||
}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
})
|
||||
|
||||
wasmhttp.Serve(nil)
|
||||
|
||||
select {}
|
||||
}
|
||||
BIN
docs/hello/api.wasm
Executable file
BIN
docs/hello/api.wasm
Executable file
Binary file not shown.
25
docs/hello/index.html
Normal file
25
docs/hello/index.html
Normal file
@@ -0,0 +1,25 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>go-wasm-http-server demo</title>
|
||||
<script>
|
||||
navigator.serviceWorker.register('sw.js')
|
||||
|
||||
async function hello() {
|
||||
res = await fetch('api/hello', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ name: document.querySelector("#name").value })
|
||||
})
|
||||
const { message } = await res.json()
|
||||
alert(message)
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<label for="name">Name: </label><input id="name" value="World">
|
||||
<button onclick="hello()">Hello</button>
|
||||
</body>
|
||||
</html>
|
||||
11
docs/hello/sw.js
Normal file
11
docs/hello/sw.js
Normal file
@@ -0,0 +1,11 @@
|
||||
importScripts('https://cdn.jsdelivr.net/gh/nlepage/go-wasm-http-server@078ff3547ebe2abfbee1fd5af9ca5ad64be480c0/sw.js')
|
||||
|
||||
addEventListener('install', (event) => {
|
||||
event.waitUntil(skipWaiting())
|
||||
})
|
||||
|
||||
addEventListener('activate', event => {
|
||||
event.waitUntil(clients.claim())
|
||||
})
|
||||
|
||||
registerWasmHTTPListener('api.wasm', { base: 'api' })
|
||||
Reference in New Issue
Block a user