Created
May 19, 2020 11:44
-
-
Save deezone/37741bba0aac9c946415821097c3d6d6 to your computer and use it in GitHub Desktop.
WASM - Hello World - webServer.go
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
// 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