Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pierreprinetti committed Aug 13, 2018
0 parents commit d669a1a
Show file tree
Hide file tree
Showing 6 changed files with 552 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Dockerfile
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
6 changes: 6 additions & 0 deletions Makefile
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
25 changes: 25 additions & 0 deletions play.go
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))
}
28 changes: 28 additions & 0 deletions serve/serve.go
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))
}
45 changes: 45 additions & 0 deletions serve/wasm_exec.html
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>
Loading

0 comments on commit d669a1a

Please sign in to comment.