Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

serve compressed assets #140

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
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