33 lines
657 B
Go
Raw Normal View History

2021-01-22 15:54:33 +01:00
package main
import (
"encoding/json"
"fmt"
"net/http"
2021-01-25 11:07:52 +01:00
"sync/atomic"
2021-01-22 15:54:33 +01:00
2024-10-14 22:24:46 +02:00
wasmhttp "github.com/nlepage/go-wasm-http-server/v2"
2021-01-22 15:54:33 +01:00
)
func main() {
2021-01-25 11:07:52 +01:00
var counter int32
2021-01-22 15:54:33 +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)
}
2021-01-24 21:18:18 +01:00
res.Header().Add("Content-Type", "application/json")
2021-01-22 15:54:33 +01:00
if err := json.NewEncoder(res).Encode(map[string]string{
2021-01-25 11:07:52 +01:00
"message": fmt.Sprintf("Hello %s! (request %d)", params["name"], atomic.AddInt32(&counter, 1)),
2021-01-22 15:54:33 +01:00
}); err != nil {
panic(err)
}
})
wasmhttp.Serve(nil)
select {}
}