Skip to content

Instantly share code, notes, and snippets.

@deezone
Created May 19, 2020 11:44
Show Gist options
  • Save deezone/37741bba0aac9c946415821097c3d6d6 to your computer and use it in GitHub Desktop.
Save deezone/37741bba0aac9c946415821097c3d6d6 to your computer and use it in GitHub Desktop.
WASM - Hello World - webServer.go
// webServer.go
package main
import (
"log"
"net/http"
"strings"
)
const dir = "./out"
// create a handler struct
type HttpHandler struct{}
// implement `ServeHTTP` method on `HttpHandler` struct
func (h HttpHandler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
fs := http.FileServer(http.Dir(dir))
log.Print("Serving " + dir + " on http://localhost:8080")
res.Header().Add("Cache-Control", "no-cache")
if strings.HasSuffix(req.URL.Path, ".wasm") {
res.Header().Set("content-type", "application/wasm")
}
fs.ServeHTTP(res, req)
}
func main() {
// create a new handler
handler := HttpHandler{}
// listen and serve
http.ListenAndServe(":8080", handler)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment