Configure gzip static response
Signed-off-by: Bryan Frimin <bryan@getprobo.com>
This commit is contained in:
@@ -15,9 +15,11 @@
|
|||||||
package web
|
package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"compress/gzip"
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -132,6 +134,23 @@ func (s *Server) ServeSPA(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.Write(s.indexContent)
|
w.Write(s.indexContent)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type gzipResponseWriter struct {
|
||||||
|
io.Writer
|
||||||
|
http.ResponseWriter
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w gzipResponseWriter) Write(b []byte) (int, error) {
|
||||||
|
return w.Writer.Write(b)
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
|
||||||
|
w.Header().Set("Content-Encoding", "gzip")
|
||||||
|
gz := gzip.NewWriter(w)
|
||||||
|
defer gz.Close()
|
||||||
|
s.ServeSPA(gzipResponseWriter{Writer: gz, ResponseWriter: w}, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
s.ServeSPA(w, r)
|
s.ServeSPA(w, r)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user