-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
65 lines (56 loc) · 1.6 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"context"
_ "embed"
"flag"
"fmt"
"log"
"net/http"
"strconv"
"time"
"github.com/pinebit/go-boot/boot"
)
//go:embed testdata/add.wasm
var addWasm []byte
func main() {
portFlag := flag.Int("port", 8080, "specify server port")
flag.Parse()
wasmService := NewWasmService()
http.HandleFunc("/run", func(w http.ResponseWriter, r *http.Request) {
a := r.URL.Query().Get("a")
b := r.URL.Query().Get("b")
ai, err := strconv.Atoi(a)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("bad 'a' parameter"))
return
}
bi, err := strconv.Atoi(b)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("bad 'b' parameter"))
return
}
res, err := wasmService.Run(r.Context(), addWasm, uint64(ai), uint64(bi))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(fmt.Sprintf("result: %d", res)))
log.Printf("handled request to sum %d and %d, the result is %d", ai, bi, res)
})
httpServer := &http.Server{
Addr: fmt.Sprintf(":%d", *portFlag),
ReadTimeout: httpReadTimeoutMs * time.Millisecond,
ReadHeaderTimeout: httpReadTimeoutMs * time.Millisecond,
WriteTimeout: httpWriteTimeoutMs * time.Millisecond,
}
httpServerService := boot.NewHttpServer(httpServer)
appServices := boot.Sequentially(wasmService, httpServerService)
app := boot.NewApplicationForService(appServices, shutdownTimeoutSeconds*time.Second)
if err := app.Run(context.Background()); err != nil {
fmt.Println("server error:", err)
}
}