dashboard: use gzip for static files, resolve
package server
import (
+ "compress/gzip"
"fmt"
+ "io"
"net"
"net/http"
+ "strings"
"time"
"github.com/fatedier/frp/assets"
@@ -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)
}))
@@ -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)
+}
