2019-11-27 07:59:57 +01:00
|
|
|
package wasmhttp_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
wasmhttp "github.com/nlepage/go-wasm-http-server"
|
|
|
|
)
|
|
|
|
|
2019-11-27 08:05:32 +01:00
|
|
|
// Example_json demostrates a simple hello JSON service.
|
|
|
|
func Example_json() {
|
2019-11-27 07:59:57 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
defer wasmhttp.Serve(nil)()
|
|
|
|
|
|
|
|
// Wait for webpage event or use empty select{}
|
|
|
|
}
|