-
Notifications
You must be signed in to change notification settings - Fork 0
/
wasm_test_server.go
47 lines (41 loc) · 1.2 KB
/
wasm_test_server.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
// This code launches a simple HTTP server to test the pre-built WASM bundle
// (This code was inspired by https://github.com/bobcob7/wasm-basic-triangle)
package main
import (
"io/ioutil"
"log"
"net/http"
"runtime"
)
func main() {
// HTML file
html, err := ioutil.ReadFile("./wasm_test.html")
if err != nil {
log.Fatalf("Could not read wasm_test.html file: %s\n", err)
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write(html)
})
// texture images
http.Handle("/assets/", http.FileServer(http.Dir(".")))
// 'wasm_exec.js'
exjs, err := ioutil.ReadFile(runtime.GOROOT() + "/misc/wasm/wasm_exec.js")
if err != nil {
log.Fatalf("Could not read wasm_exec.js file: %s\n", err)
}
http.HandleFunc("/wasm_exec.js", func(w http.ResponseWriter, r *http.Request) {
w.Write(exjs)
})
// 'wasm_test.wasm'
wasm, err := ioutil.ReadFile("wasm_test.wasm")
if err != nil {
log.Fatalf("Could not read wasm file: %s\n", err)
}
http.HandleFunc("/wasm_test.wasm", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/wasm")
w.WriteHeader(http.StatusOK)
w.Write(wasm)
})
// start the server
log.Fatal(http.ListenAndServe(":8080", nil))
}