#!/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 < ./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 < ./index.html EOF touch favicon.ico show_msg "3/4 - Create go server - to handle our application." cat < ./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 < ./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"