diff --git a/docs/hello-state/api.go b/docs/hello-state/api.go new file mode 100644 index 0000000..c1e201d --- /dev/null +++ b/docs/hello-state/api.go @@ -0,0 +1,32 @@ +package main + +import ( + "encoding/json" + "fmt" + "net/http" + + wasmhttp "github.com/nlepage/go-wasm-http-server" +) + +func main() { + var no = 1 + + 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! (request n°%d)", params["name"], no), + }); err != nil { + panic(err) + } + + no++ + }) + + wasmhttp.Serve(nil) + + select {} +} diff --git a/docs/hello-state/api.wasm b/docs/hello-state/api.wasm new file mode 100755 index 0000000..edced6d Binary files /dev/null and b/docs/hello-state/api.wasm differ diff --git a/docs/hello-state/index.html b/docs/hello-state/index.html new file mode 100644 index 0000000..62fa87f --- /dev/null +++ b/docs/hello-state/index.html @@ -0,0 +1,25 @@ + + + + go-wasm-http-server hello with state demo + + + + + + + diff --git a/docs/hello-state/sw.js b/docs/hello-state/sw.js new file mode 100644 index 0000000..38d585e --- /dev/null +++ b/docs/hello-state/sw.js @@ -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' }) diff --git a/docs/index.html b/docs/index.html index 16d29cd..6167295 100644 --- a/docs/index.html +++ b/docs/index.html @@ -6,6 +6,7 @@