-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d669a1a
Showing
6 changed files
with
552 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
FROM golang:1.11beta3-alpine | ||
|
||
# Copy the code from the host and compile it | ||
WORKDIR $GOPATH/src/play-wasm | ||
COPY . ./ | ||
RUN GOOS=js GOARCH=wasm go build -o ./serve/play.wasm ./play.go | ||
RUN GOOS=linux go build -o ./app ./serve/serve.go | ||
|
||
EXPOSE 8080 | ||
|
||
CMD ./app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
build: | ||
docker build -t play-wasm . | ||
|
||
run: build | ||
docker run --rm -p 8080:8080 play-wasm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package main | ||
|
||
import ( | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"syscall/js" | ||
) | ||
|
||
func main() { | ||
|
||
res, err := http.Get("https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS") | ||
if err != nil { | ||
log.Fatalf("error fetching: %v", err) | ||
} | ||
|
||
b, err := ioutil.ReadAll(res.Body) | ||
if err != nil { | ||
log.Fatalf("error parsing the response body: %v", err) | ||
} | ||
|
||
document := js.Global().Get("document") | ||
p := document.Call("getElementById", "target") | ||
p.Set("innerHTML", string(b)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"strings" | ||
"time" | ||
) | ||
|
||
func file(name string) http.Handler { | ||
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { | ||
log.Printf("%s %s %s", time.Now().Format(time.RFC3339), req.Method, req.RequestURI) | ||
if strings.HasSuffix(name, ".wasm") { | ||
rw.Header().Set("Content-Type", "application/wasm") | ||
} | ||
http.ServeFile(rw, req, name) | ||
}) | ||
} | ||
|
||
func main() { | ||
mux := http.NewServeMux() | ||
mux.Handle("/", file("./serve/wasm_exec.html")) | ||
mux.Handle("/wasm_exec.js", file("./serve/wasm_exec.js")) | ||
mux.Handle("/play.wasm", file("./serve/play.wasm")) | ||
|
||
log.Println("Serving...") | ||
log.Fatal(http.ListenAndServe(":8080", mux)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<!doctype html> | ||
<!-- | ||
Copyright 2018 The Go Authors. All rights reserved. | ||
Use of this source code is governed by a BSD-style | ||
license that can be found in the LICENSE file. | ||
--> | ||
<html> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
<title>Go wasm</title> | ||
</head> | ||
|
||
<body> | ||
<script src="wasm_exec.js"></script> | ||
<script> | ||
if (!WebAssembly.instantiateStreaming) { // polyfill | ||
WebAssembly.instantiateStreaming = async (resp, importObject) => { | ||
const source = await (await resp).arrayBuffer(); | ||
return await WebAssembly.instantiate(source, importObject); | ||
}; | ||
} | ||
|
||
const go = new Go(); | ||
let mod, inst; | ||
WebAssembly.instantiateStreaming(fetch("play.wasm"), go.importObject).then((result) => { | ||
mod = result.module; | ||
inst = result.instance; | ||
document.getElementById("runButton").disabled = false; | ||
}); | ||
|
||
async function run() { | ||
console.clear(); | ||
await go.run(inst); | ||
inst = await WebAssembly.instantiate(mod, go.importObject); // reset instance | ||
} | ||
</script> | ||
|
||
<button onClick="run();" id="runButton" disabled>Run</button> | ||
|
||
<p id="target"></p> | ||
|
||
</body> | ||
|
||
</html> |
Oops, something went wrong.