opt: gzip + no cdn shi + feeds

This commit is contained in:
2026-06-26 20:57:14 +03:00
parent 78b8bea6f7
commit 98d85ecbf5
10 changed files with 3563 additions and 15 deletions

28
main.go
View File

@@ -1,12 +1,38 @@
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) {
@@ -23,5 +49,5 @@ func main() {
}
log.Println("listening on :8000")
http.ListenAndServe(":8000", mux)
http.ListenAndServe(":8000", gzipHandler(mux))
}