created hws module

This commit is contained in:
2026-01-04 00:59:24 +11:00
parent ade3fa0454
commit 14eec74683
12 changed files with 479 additions and 0 deletions

31
hws/gzip.go Normal file
View File

@@ -0,0 +1,31 @@
package hws
import (
"compress/gzip"
"io"
"net/http"
"strings"
)
func addgzip(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")
gz := gzip.NewWriter(w)
defer gz.Close()
gzw := gzipResponseWriter{Writer: gz, ResponseWriter: w}
next.ServeHTTP(gzw, r)
})
}
type gzipResponseWriter struct {
io.Writer
http.ResponseWriter
}
func (w gzipResponseWriter) Write(b []byte) (int, error) {
return w.Writer.Write(b)
}