From 36cf5eee3e1181af2f4dd76bfe9b518c1ac1ed1a Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Fri, 14 Feb 2020 08:24:35 -0700 Subject: [PATCH] gzip --- .gitignore | 0 main.go | 15 ++++++++++++++- public/_index.html | 1 + 3 files changed, 15 insertions(+), 1 deletion(-) mode change 100644 => 100755 .gitignore mode change 100644 => 100755 main.go create mode 100755 public/_index.html diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/main.go b/main.go old mode 100644 new mode 100755 index 5323f01..e4c332f --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ package main import ( "local/args" + "local/gziphttp" "log" "net/http" "strings" @@ -26,9 +27,21 @@ func main() { d := fs.Get("d").GetString() p := strings.TrimPrefix(fs.Get("p").GetString(), ":") - http.Handle("/", http.FileServer(http.Dir(d))) + http.Handle("/", http.HandlerFunc(handler(d))) log.Printf("Serving %s on HTTP port: %s\n", d, p) log.Fatal(http.ListenAndServe(":"+p, nil)) } + +func handler(d string) func(http.ResponseWriter, *http.Request) { + h := http.FileServer(http.Dir(d)) + return func(w http.ResponseWriter, r *http.Request) { + if gziphttp.Can(r) { + gz := gziphttp.New(w) + defer gz.Close() + w = gz + } + h.ServeHTTP(w, r) + } +} diff --git a/public/_index.html b/public/_index.html new file mode 100755 index 0000000..45b983b --- /dev/null +++ b/public/_index.html @@ -0,0 +1 @@ +hi