aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorJP Appel <jeanpierre.appel01@gmail.com>2024-10-09 11:36:46 -0400
committerJP Appel <jeanpierre.appel01@gmail.com>2024-10-09 11:36:46 -0400
commit11d06bae8f961aaa3667a64bda7aa30f37b1cad4 (patch)
tree82cbfcac5b3da1e1b6b9d7992c0e070adfd02511
parent81c0c8f70ebe2247ba2de8ee879be71ded6acee9 (diff)
Add gzip middleware stub
-rw-r--r--nonsense-time.go31
1 files changed, 31 insertions, 0 deletions
diff --git a/nonsense-time.go b/nonsense-time.go
index e57e048..1d4e218 100644
--- a/nonsense-time.go
+++ b/nonsense-time.go
@@ -1,6 +1,7 @@
package main
import (
+ "compress/gzip"
"context"
"flag"
"fmt"
@@ -23,6 +24,36 @@ func timeoutMiddleware(next http.Handler, duration time.Duration) http.Handler {
})
}
+type gzipResponseWriter struct {
+ gzw gzip.Writer
+ hrw http.ResponseWriter
+}
+
+func (gzrw gzipResponseWriter) Header() http.Header {
+ headers := gzrw.hrw.Header()
+ // TODO: add gzip header
+ return headers
+}
+
+func (gzrw gzipResponseWriter) Write(p []byte) (int, error) {
+ // TODO: write data using gzrw.gzw.Write
+ return gzrw.hrw.Write(p)
+}
+
+func (gzrw gzipResponseWriter) WriteHeader(statuscode int) {
+ gzrw.hrw.WriteHeader(statuscode)
+}
+
+// Middleware to conditionally gzip a response
+func gzipMiddleware(next http.Handler) http.Handler {
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ // TODO: check for support gzip header
+ gzrw := gzipResponseWriter{hrw: w}
+
+ next.ServeHTTP(gzrw, r)
+ })
+}
+
func main() {
port := flag.Int("p", 8080, "the port to listen on")