Last active
January 11, 2022 01:52
-
-
Save romanitalian/cd28becfb130506d5f65ea48d8780ba9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/bin/bash | |
# README | |
# -------------------- | |
# chmod u+x wasm_init.sh | |
# ./wasm_init.sh example.com | |
# -------------------- | |
PROJECT_NAME="$1" | |
if [ -z "$PROJECT_NAME" ] | |
then | |
echo "\$PROJECT_NAME is empty. Run this: $0 example.com" | |
exit 1 | |
fi | |
SERVER_PORT="8080" | |
show_msg() | |
{ | |
# printf "=%.0s" {1..30} | |
printf "$1\n" | |
# printf "=%.0s" {1..30} | |
# printf "\n" | |
} | |
show_msg "Create WebAssembly app ..." | |
rm -rf "$PROJECT_NAME" | |
mkdir "$PROJECT_NAME" && cd "$PROJECT_NAME" | |
go mod init "$PROJECT_NAME" | |
show_msg "1/4 - Copy 'wasm_exec.js' - this will be the js-adapter itself for our app - to communicate throw JS to 'wasm' app." | |
cp "$(go env GOROOT)/misc/wasm/wasm_exec.js" . | |
show_msg "2/4 - Create and compile 'wasm'." | |
mkdir app | |
cat <<EOF > ./app/main.go | |
package main | |
import ( | |
"fmt" | |
) | |
func main() { | |
fmt.Println("Hello User") | |
} | |
EOF | |
cd app && GOOS=js GOARCH=wasm go build -o ../main.wasm && cd .. | |
show_msg "3/4 - Create html templates" | |
cat <<EOF > ./index.html | |
<html> | |
<head> | |
<meta charset="utf-8"/> | |
<script src="wasm_exec.js"></script> | |
<script> | |
// polyfill | |
if (!WebAssembly.instantiateStreaming) { | |
WebAssembly.instantiateStreaming = async (resp, importObject) => { | |
const source = await (await resp).arrayBuffer(); | |
return await WebAssembly.instantiate(source, importObject); | |
}; | |
} | |
const go = new Go(); | |
WebAssembly | |
.instantiateStreaming(fetch("main.wasm"), go.importObject) | |
.then((result) => { | |
go.run(result.instance); | |
}); | |
</script> | |
</head> | |
<body></body> | |
</html> | |
EOF | |
touch favicon.ico | |
show_msg "3/4 - Create go server - to handle our application." | |
cat <<EOF > ./server.go | |
package main | |
import ( | |
"log" | |
"net/http" | |
) | |
const ( | |
AppPort = "$SERVER_PORT" | |
TemplatesDir = "." | |
) | |
func main() { | |
log.Println("Run server on :" + AppPort + " ...") | |
err := http.ListenAndServe(":"+AppPort, http.FileServer(http.Dir(TemplatesDir))) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
} | |
EOF | |
show_msg "4/4 - Create Makefile." | |
cat <<EOF > ./Makefile | |
LDFLAGS = | |
GO = \$(shell which go) | |
GORUN = \$(GO) run \$(LDFLAGS) | |
APP_PORT=$SERVER_PORT | |
help : Makefile | |
@echo "+--------------------+" | |
@echo "| AVAILABLE COMMANDS |" | |
@echo "+--------------------+\n" | |
@cat $< | grep "##" | sed -n 's/^## /make /p' | column -t -s ':' && echo "" | |
## run: only start application server. | |
run: | |
\$(GORUN) server.go | |
## run-now: start application server and open it in web-browser. | |
run-now: | |
make run& | |
open "http://127.0.0.1:\$(APP_PORT)" | |
EOF | |
echo "" | |
# Just show list of available commands. | |
make | |
show_msg "RUN IT: \ncd $PROJECT_NAME && make run-now" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TypeError: Failed to execute 'compile' on 'WebAssembly': Incorrect response MIME type. Expected 'application/wasm'