optiminimize
This commit is contained in:
49
main.go
49
main.go
@@ -45,6 +45,53 @@ func cacheHandler(next http.Handler) http.Handler {
|
||||
})
|
||||
}
|
||||
|
||||
type mimeWriter struct {
|
||||
http.ResponseWriter
|
||||
override string
|
||||
wrote bool
|
||||
}
|
||||
|
||||
func (m *mimeWriter) WriteHeader(code int) {
|
||||
if !m.wrote {
|
||||
m.wrote = true
|
||||
m.Header().Set("Content-Type", m.override)
|
||||
}
|
||||
m.ResponseWriter.WriteHeader(code)
|
||||
}
|
||||
|
||||
func (m *mimeWriter) Write(b []byte) (int, error) {
|
||||
if !m.wrote {
|
||||
m.WriteHeader(200)
|
||||
}
|
||||
return m.ResponseWriter.Write(b)
|
||||
}
|
||||
|
||||
func mimeHandler(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
ext := strings.ToLower(r.URL.Path[strings.LastIndex(r.URL.Path, ".")+1:])
|
||||
var ct string
|
||||
switch ext {
|
||||
case "woff2":
|
||||
ct = "font/woff2"
|
||||
case "woff":
|
||||
ct = "font/woff"
|
||||
case "ttf":
|
||||
ct = "font/ttf"
|
||||
case "js":
|
||||
ct = "text/javascript"
|
||||
case "css":
|
||||
ct = "text/css"
|
||||
case "webp":
|
||||
ct = "image/webp"
|
||||
}
|
||||
if ct != "" {
|
||||
next.ServeHTTP(&mimeWriter{ResponseWriter: w, override: ct}, r)
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func main() {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/ABSTRACTS.png", func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -64,5 +111,5 @@ func main() {
|
||||
mux.HandleFunc("/", blog.ServeLanding)
|
||||
|
||||
log.Println("listening on :8000")
|
||||
http.ListenAndServe(":8000", cacheHandler(gzipHandler(mux)))
|
||||
http.ListenAndServe(":8000", mimeHandler(cacheHandler(gzipHandler(mux))))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user