Skip to content

Commit

Permalink
proxy,scripts: serve compressed asset
Browse files Browse the repository at this point in the history
Signed-off-by: Cristian Staretu <[email protected]>
  • Loading branch information
unclejack committed Apr 25, 2017
1 parent 9bbf084 commit 201caf5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
35 changes: 32 additions & 3 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package proxy
import (
"crypto/tls"
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -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)
}
Expand Down
5 changes: 4 additions & 1 deletion scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

0 comments on commit 201caf5

Please sign in to comment.