From 996a557e84905e1be93e970d9b85d7a4c12ada7f Mon Sep 17 00:00:00 2001 From: unclejack Date: Wed, 26 Apr 2017 01:03:59 +0300 Subject: [PATCH 1/2] .dockerignore: skip unneeded assets Signed-off-by: Cristian Staretu --- .dockerignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.dockerignore b/.dockerignore index 139b990f..dea3632e 100644 --- a/.dockerignore +++ b/.dockerignore @@ -4,3 +4,5 @@ # actual site and compiled assets live. build/dependencies/contiv-ui !build/dependencies/contiv-ui/app +build/dependencies/contiv-ui/app/assets +build/dependencies/contiv-ui/app/bower_components/semantic-ui From 44e0f47ac5884ea2fd54a1af68294fad53483521 Mon Sep 17 00:00:00 2001 From: unclejack Date: Wed, 26 Apr 2017 01:05:07 +0300 Subject: [PATCH 2/2] proxy,scripts: serve compressed asset Signed-off-by: Cristian Staretu --- proxy/proxy.go | 35 ++++++++++++++++++++++++++++++++--- scripts/build.sh | 5 ++++- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/proxy/proxy.go b/proxy/proxy.go index cada757d..dffcccbf 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -3,11 +3,13 @@ package proxy import ( "crypto/tls" "errors" + "fmt" "io/ioutil" "net" "net/http" "net/url" "os" + "strings" "sync" "time" @@ -283,10 +285,37 @@ type staticFileHandler struct { func (sfh *staticFileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { common.EnableHSTS(w) + var ( + serveCompressed = false + currentURL = "" + err error + ) + if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") { + sfh.fileServer.ServeHTTP(w, r) + } + isCSSFile := strings.HasSuffix(currentURL, ".css") + isJSFile := strings.HasSuffix(currentURL, ".js") + if isCSSFile || isJSFile { + currentURL = r.URL.String() + newURL := fmt.Sprintf("%v.gz", currentURL) + r.URL, err = url.Parse(newURL) + if err != nil { + err = fmt.Errorf("failed to parse URL: %v", err) + serverError(w, err) + } + serveCompressed = true + } + if isCSSFile { + w.Header().Set("Content-Type", "text/css") + } - // TODO: here is a good spot to look for asset requests (.js, .css, etc.) - // and append .gz and serve a gzipped version instead by rewriting - // the requested path. + if isJSFile { + w.Header().Set("Content-Type", "application/x-javascript") + } + + if serveCompressed { + w.Header().Set("Content-Encoding", "gzip") + } sfh.fileServer.ServeHTTP(w, r) } diff --git a/scripts/build.sh b/scripts/build.sh index d3d535e2..344d0cca 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -55,7 +55,10 @@ docker run \ # copy out the binaries docker cp build_cntr:/go/src/github.com/contiv/auth_proxy/build/output/auth_proxy ./build/output/ docker rm -fv build_cntr - +find build/dependencies/contiv-ui/app/ -name '*.js' -not \ + -path "build/dependencies/contiv-ui/app/bower_components/semantic-ui/*" -exec gzip -9fk '{}' \; +find build/dependencies/contiv-ui/app/ -name '*.css' -not \ + -path "build/dependencies/contiv-ui/app/bower_components/semantic-ui/*" -exec gzip -9fk '{}' \; docker build -t $IMAGE_NAME:$VERSION -f ./build/Dockerfile.release . echo "Created image: $IMAGE_NAME:$VERSION"