Skip to content

Commit b0d43a5

Browse files
committed
proxy,scripts: serve compressed asset
Signed-off-by: Cristian Staretu <[email protected]>
1 parent 9bbf084 commit b0d43a5

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

proxy/proxy.go

+32-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ package proxy
33
import (
44
"crypto/tls"
55
"errors"
6+
"fmt"
67
"io/ioutil"
78
"net"
89
"net/http"
910
"net/url"
1011
"os"
12+
"strings"
1113
"sync"
1214
"time"
1315

@@ -283,10 +285,37 @@ type staticFileHandler struct {
283285

284286
func (sfh *staticFileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
285287
common.EnableHSTS(w)
288+
var (
289+
serveCompressed = false
290+
currentURL = ""
291+
err error
292+
)
293+
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
294+
sfh.fileServer.ServeHTTP(w, r)
295+
}
296+
isCSSFile := strings.HasSuffix(currentURL, ".css")
297+
isJSFile := strings.HasSuffix(currentURL, ".js")
298+
if isCSSFile || isJSFile {
299+
currentURL = r.URL.String()
300+
newURL := fmt.Sprintf("%v.gz", currentURL)
301+
r.URL, err = url.Parse(newURL)
302+
if err != nil {
303+
err := fmt.Errorf("failed to parse URL: %v", err)
304+
serverError(w, err)
305+
}
306+
serveCompressed = true
307+
}
308+
if isCSSFile {
309+
w.Header().Set("Content-Type", "text/css")
310+
}
286311

287-
// TODO: here is a good spot to look for asset requests (.js, .css, etc.)
288-
// and append .gz and serve a gzipped version instead by rewriting
289-
// the requested path.
312+
if isJSFile {
313+
w.Header().Set("Content-Type", "application/x-javascript")
314+
}
315+
316+
if serveCompressed {
317+
w.Header().Set("Content-Encoding", "gzip")
318+
}
290319

291320
sfh.fileServer.ServeHTTP(w, r)
292321
}

scripts/build.sh

+4-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ docker run \
5555
# copy out the binaries
5656
docker cp build_cntr:/go/src/github.com/contiv/auth_proxy/build/output/auth_proxy ./build/output/
5757
docker rm -fv build_cntr
58-
58+
find build/dependencies/contiv-ui/app/ -name '*.js' -not \
59+
-path "build/dependencies/contiv-ui/app/bower_components/semantic-ui/*" -exec gzip -9fk '{}' \;
60+
find build/dependencies/contiv-ui/app/ -name '*.css' -not \
61+
-path "build/dependencies/contiv-ui/app/bower_components/semantic-ui/*" -exec gzip -9fk '{}' \;
5962
docker build -t $IMAGE_NAME:$VERSION -f ./build/Dockerfile.release .
6063
echo "Created image: $IMAGE_NAME:$VERSION"
6164

0 commit comments

Comments
 (0)