package main import ( "compress/gzip" "io" "log" "net/http" "strings" "chaosmith-site/blog" ) type gzipResponseWriter struct { io.Writer http.ResponseWriter } func (w gzipResponseWriter) Write(b []byte) (int, error) { return w.Writer.Write(b) } func gzipHandler(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") { next.ServeHTTP(w, r) return } w.Header().Set("Content-Encoding", "gzip") w.Header().Del("Content-Length") gz := gzip.NewWriter(w) defer gz.Close() next.ServeHTTP(gzipResponseWriter{Writer: gz, ResponseWriter: w}, r) }) } func main() { mux := http.NewServeMux() mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "index.html") }) mux.HandleFunc("/ABSTRACTS.png", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "ABSTRACTS.png") }) mux.Handle("/src/", http.StripPrefix("/src/", http.FileServer(http.Dir("src")))) mux.Handle("/content/images/", http.StripPrefix("/content/images/", http.FileServer(http.Dir("content/images")))) if err := blog.RegisterRoutes(mux, "content/posts"); err != nil { log.Fatalf("blog: %v", err) } log.Println("listening on :8000") http.ListenAndServe(":8000", gzipHandler(mux)) }