From aa3ec594b365fc80d2014f8168c203be676ad965 Mon Sep 17 00:00:00 2001 From: Johan Brandhorst Date: Sat, 29 Jun 2019 16:46:45 +0100 Subject: [PATCH] Add GZIP compression flag --- Makefile | 3 --- README.md | 16 ++++++++++++---- go.mod | 3 ++- go.sum | 7 +++++++ main.go | 32 +++++++++++++++++++++++--------- 5 files changed, 44 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index eff1946..6bb3e20 100644 --- a/Makefile +++ b/Makefile @@ -74,9 +74,6 @@ test: clean clean: rm -f ./html/* -serve: - go run main.go - install-test: go get github.com/agnivade/wasmbrowsertest mv $$GOPATH/bin/wasmbrowsertest $$GOPATH/bin/go_js_wasm_exec diff --git a/README.md b/README.md index 10e5177..472a479 100644 --- a/README.md +++ b/README.md @@ -22,14 +22,22 @@ rm -f ./html/* GOOS=js GOARCH=wasm go build -o ./html/test.wasm ./hello/main.go cp $(go env GOROOT)/misc/wasm/wasm_exec.html ./html/index.html cp $(go env GOROOT)/misc/wasm/wasm_exec.js ./html/wasm_exec.js -$ make serve -go run main.go +$ go run main.go 2019/02/24 14:11:51 Serving on http://localhost:8080 ``` Navigate to http://localhost:8080 to load the page. Some examples require opening the browser console to be seen. +## Gzip compression + +Use the `-gzip` flag to enable on-the-fly gzip compression of the files: + +```bash +$ go run main.go -gzip +2019/02/24 14:11:51 Serving on http://localhost:8080 +``` + ## TinyGo targets To compile something with the TinyGo WebAssembly compiler, simply choose the @@ -45,8 +53,7 @@ docker run --rm -v $$(pwd):/go/src/github.com/johanbrandhorst/wasm-experiments t " cp $$(go env GOROOT)/misc/wasm/wasm_exec.html ./html/index.html sed -i -e 's;;\n\t
;' ./html/index.html -$ make serve -go run main.go +$ go run main.go 2019/02/24 14:33:58 Serving on http://localhost:8080 ``` @@ -57,6 +64,7 @@ TinyGo: - `hello` - `channels` - `js` +- `tinygo-canvas` ## Experiments diff --git a/go.mod b/go.mod index dcb2bd4..35c9531 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,9 @@ module github.com/johanbrandhorst/wasm-experiments go 1.12 require ( + github.com/NYTimes/gziphandler v1.1.1 github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 github.com/hajimehoshi/ebiten v1.9.3 - github.com/vugu/vugu v0.0.0-20190518235128-5a84f26390d1 // indirect + github.com/vugu/vugu v0.0.0-20190518235128-5a84f26390d1 golang.org/x/image v0.0.0-20190618124811-92942e4437e2 ) diff --git a/go.sum b/go.sum index ad77035..050914e 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,10 @@ +github.com/NYTimes/gziphandler v1.1.1 h1:ZUDjpQae29j0ryrS0u/B8HZfJBtBQHjqw2rQ2cqUQ3I= +github.com/NYTimes/gziphandler v1.1.1/go.mod h1:n/CVRwUEOgIxrgPvAQhUUr9oeUtvrhMomdKFjzJNB0c= +github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-gl/glfw v0.0.0-20181213070059-819e8ce5125f h1:7MsFMbSn8Lcw0blK4+NEOf8DuHoOBDhJsHz04yh13pM= github.com/go-gl/glfw v0.0.0-20181213070059-819e8ce5125f/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= @@ -33,9 +37,12 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72 h1:qLC7fQah7D6K1B0ujays3HV9gkFtllcxhzImRR7ArPQ= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/vugu/vugu v0.0.0-20190518235128-5a84f26390d1 h1:Iowi+7qqrJr/6RmZFIM8g6n6e8TENUNiOxex1xG/NHA= github.com/vugu/vugu v0.0.0-20190518235128-5a84f26390d1/go.mod h1:JXaMmpEMpofCzJj8TmSWeLDxAajs4a4wrac5VYwj4A8= diff --git a/main.go b/main.go index bb4e2ae..b1a1c46 100644 --- a/main.go +++ b/main.go @@ -1,21 +1,35 @@ package main import ( + "flag" "log" "net/http" + "strings" + + "github.com/NYTimes/gziphandler" ) -func main() { - fs := http.FileServer(http.Dir("./html")) - log.Print("Serving on http://localhost:8080") - err := http.ListenAndServe(":8080", http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) { - if req.URL.Path == "/test.wasm" { - resp.Header().Set("content-type", "application/wasm") - } +var gz = flag.Bool("gzip", false, "enable automatic gzip compression") - fs.ServeHTTP(resp, req) - })) +func main() { + flag.Parse() + h := wasmContentTypeSetter(http.FileServer(http.Dir("./html"))) + if *gz { + h = gziphandler.GzipHandler(h) + } + + log.Print("Serving on http://localhost:8080") + err := http.ListenAndServe(":8080", h) if err != http.ErrServerClosed { log.Fatal(err) } } + +func wasmContentTypeSetter(h http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if strings.HasSuffix(r.URL.Path, ".wasm") { + w.Header().Set("content-type", "application/wasm") + } + h.ServeHTTP(w, r) + }) +}