-
-
Notifications
You must be signed in to change notification settings - Fork 431
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stop using CDNs + Remove bootstrap.min.js because it wasn't necessary
+ Gzip http.FileServer
- Loading branch information
Showing
6 changed files
with
58 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package main | ||
|
||
import ( | ||
"compress/gzip" | ||
"io" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
var gzPool = sync.Pool{ | ||
New: func() interface{} { | ||
return gzip.NewWriter(ioutil.Discard) | ||
}, | ||
} | ||
|
||
type gzipResponseWriter struct { | ||
io.Writer | ||
http.ResponseWriter | ||
} | ||
|
||
func (w *gzipResponseWriter) WriteHeader(status int) { | ||
w.Header().Del("Content-Length") | ||
w.ResponseWriter.WriteHeader(status) | ||
} | ||
|
||
func (w *gzipResponseWriter) Write(b []byte) (int, error) { | ||
return w.Writer.Write(b) | ||
} | ||
|
||
func GzipHandler(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(writer http.ResponseWriter, r *http.Request) { | ||
// If the request doesn't specify that it supports gzip, then don't compress it | ||
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") { | ||
next.ServeHTTP(writer, r) | ||
return | ||
} | ||
writer.Header().Set("Content-Encoding", "gzip") | ||
gz := gzPool.Get().(*gzip.Writer) | ||
defer gzPool.Put(gz) | ||
gz.Reset(writer) | ||
defer gz.Close() | ||
next.ServeHTTP(&gzipResponseWriter{ResponseWriter: writer, Writer: gz}, r) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.