Files
wasm-http-server/docs/api.go

36 lines
603 B
Go
Raw Normal View History

2019-11-27 11:20:52 +01:00
package main
import (
"encoding/json"
"fmt"
"net/http"
wasmhttp "github.com/nlepage/go-wasm-http-server"
)
2019-12-02 17:22:48 +01:00
var no = 1
2019-11-27 11:20:52 +01:00
func main() {
2020-05-28 23:51:54 +02:00
fmt.Println("Starting...")
2019-11-27 11:20:52 +01:00
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{
2019-12-02 17:22:48 +01:00
"message": fmt.Sprintf("Hello %s! (%d)", params["name"], no),
2019-11-27 11:20:52 +01:00
}); err != nil {
panic(err)
}
2019-12-02 17:22:48 +01:00
no++
2019-11-27 11:20:52 +01:00
})
wasmhttp.Serve(nil)
2020-05-28 23:51:54 +02:00
fmt.Println("Started")
2019-11-27 11:20:52 +01:00
select {}
}