-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
58 lines (52 loc) · 1.22 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
package main
import (
"fmt"
"log"
"net/http"
"github.com/go-chi/chi"
)
func main() {
r := chi.NewRouter()
r.Get("/", index)
r.Mount("/js", http.StripPrefix("/js/", http.FileServer(http.Dir("js"))))
r.Mount("/wasm", http.StripPrefix("/wasm/", http.FileServer(http.Dir("build"))))
s := http.Server{
Addr: "localhost:4000",
Handler: r,
}
if err := s.ListenAndServe(); err != nil {
log.Fatal(err)
}
}
func index(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
fmt.Fprintf(w, `<DOCTYPE html>
<html>
<head>
<title>Tests</title>
<script src="/js/wasm_exec.js"></script>
<script type="text/javascript">
function fetchAndInstantiate(url, importObject) {
return fetch(url).then(response =>
response.arrayBuffer()
).then(bytes =>
WebAssembly.instantiate(bytes, importObject)
).then(results =>
results.instance
);
}
var go = new Go();
var mod = fetchAndInstantiate("/wasm/test.wasm", go.importObject);
window.onload = function() {
mod.then(function(instance) {
go.run(instance);
});
};
</script>
</head>
<body>
<h1>Tests</h1>
</body>
</html>
`)
}