This commit is contained in:
Nicolas Lepage 2019-11-27 07:59:57 +01:00
parent 43b5ac9464
commit 6a36fe571c
No known key found for this signature in database
GPG Key ID: B0879E35E66D8F6F
2 changed files with 30 additions and 1 deletions

29
example_json_test.go Normal file
View File

@ -0,0 +1,29 @@
package wasmhttp_test
import (
"encoding/json"
"fmt"
"net/http"
wasmhttp "github.com/nlepage/go-wasm-http-server"
)
// Example_JSON demostrates a simple hello JSON service.
func Example_JSON() {
http.HandleFunc("/hello", func(res http.ResponseWriter, req *http.Request) {
params := make(map[string]string)
if err := json.NewDecoder(req.Body).Decode(&params); 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{}
}

View File

@ -8,7 +8,7 @@ import (
"github.com/nlepage/go-wasm-http-server/internal/whutil"
)
// Serve serves HTTP requests using handler or http.DefaultServeMux if handler is nil
// Serve serves HTTP requests using handler or http.DefaultServeMux if handler is nil.
func Serve(handler http.Handler) func() {
h := handler
if h == nil {