Skip to content

Commit

Permalink
dashboard: use gzip for static files, resolve fatedier#333
Browse files Browse the repository at this point in the history
  • Loading branch information
fatedier committed May 30, 2017
1 parent 7bc6c72 commit 44971c7
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion server/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
package server

import (
"compress/gzip"
"fmt"
"io"
"net"
"net/http"
"strings"
"time"

"github.com/fatedier/frp/assets"
Expand Down Expand Up @@ -45,7 +48,7 @@ func RunDashboardServer(addr string, port int64) (err error) {

// view
router.Handler("GET", "/favicon.ico", http.FileServer(assets.FileSystem))
router.Handler("GET", "/static/*filepath", basicAuthWraper(http.StripPrefix("/static/", http.FileServer(assets.FileSystem))))
router.Handler("GET", "/static/*filepath", MakeGzipHandler(basicAuthWraper(http.StripPrefix("/static/", http.FileServer(assets.FileSystem)))))
router.HandlerFunc("GET", "/", basicAuth(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/static/", http.StatusMovedPermanently)
}))
Expand Down Expand Up @@ -125,3 +128,34 @@ func httprouterBasicAuth(h httprouter.Handle) httprouter.Handle {
}
}
}

type GzipWraper struct {
h http.Handler
}

func (gw *GzipWraper) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
gw.h.ServeHTTP(w, r)
return
}
w.Header().Set("Content-Encoding", "gzip")
gz := gzip.NewWriter(w)
defer gz.Close()
gzr := gzipResponseWriter{Writer: gz, ResponseWriter: w}
gw.h.ServeHTTP(gzr, r)
}

func MakeGzipHandler(h http.Handler) http.Handler {
return &GzipWraper{
h: h,
}
}

type gzipResponseWriter struct {
io.Writer
http.ResponseWriter
}

func (w gzipResponseWriter) Write(b []byte) (int, error) {
return w.Writer.Write(b)
}

0 comments on commit 44971c7

Please sign in to comment.