add compression
This commit is contained in:
parent
fe53085bba
commit
c7b59a5c9e
6
go.mod
6
go.mod
|
@ -1,8 +1,10 @@
|
||||||
module git.teamworkapps.com/shortcut/httphere
|
module git.teamworkapps.com/shortcut/httphere
|
||||||
|
|
||||||
go 1.17
|
go 1.24.1
|
||||||
|
|
||||||
require (
|
require (
|
||||||
git.teamworkapps.com/shortcut/enhancedfileserver v0.0.0-20230421162957-948ff8787756
|
git.teamworkapps.com/shortcut/enhancedfileserver v0.0.0-20230421162957-948ff8787756
|
||||||
github.com/gorilla/mux v1.8.0
|
github.com/gorilla/mux v1.8.1
|
||||||
)
|
)
|
||||||
|
|
||||||
|
require github.com/andybalholm/brotli v1.2.0 // indirect
|
||||||
|
|
8
go.sum
8
go.sum
|
@ -1,6 +1,6 @@
|
||||||
git.teamworkapps.com/shortcut/enhancedfileserver v0.0.0-20181107163431-49cae39a0eb7 h1:wl0faTtewN9Y0nTZpRY2mcaIhVkaR1l8k920U0rDIk0=
|
|
||||||
git.teamworkapps.com/shortcut/enhancedfileserver v0.0.0-20181107163431-49cae39a0eb7/go.mod h1:4t5y+BFcMRTe6GkAX72YXcjrKbGkOVM7CkJMRHNZYiQ=
|
|
||||||
git.teamworkapps.com/shortcut/enhancedfileserver v0.0.0-20230421162957-948ff8787756 h1:ViHTT1GI1JlsLrUsivl4tvg6DySn35d1C3bzXeLa8uY=
|
git.teamworkapps.com/shortcut/enhancedfileserver v0.0.0-20230421162957-948ff8787756 h1:ViHTT1GI1JlsLrUsivl4tvg6DySn35d1C3bzXeLa8uY=
|
||||||
git.teamworkapps.com/shortcut/enhancedfileserver v0.0.0-20230421162957-948ff8787756/go.mod h1:Px7gD0CL+z9WBsvFPtCuDpVEJNStJXrbFRD3q7aJwYk=
|
git.teamworkapps.com/shortcut/enhancedfileserver v0.0.0-20230421162957-948ff8787756/go.mod h1:Px7gD0CL+z9WBsvFPtCuDpVEJNStJXrbFRD3q7aJwYk=
|
||||||
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
|
github.com/andybalholm/brotli v1.2.0 h1:ukwgCxwYrmACq68yiUqwIWnGY0cTPox/M94sVwToPjQ=
|
||||||
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
|
github.com/andybalholm/brotli v1.2.0/go.mod h1:rzTDkvFWvIrjDXZHkuS16NPggd91W3kUSvPlQ1pLaKY=
|
||||||
|
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
|
||||||
|
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
|
||||||
|
|
62
main.go
62
main.go
|
@ -1,12 +1,16 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"compress/gzip"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"git.teamworkapps.com/shortcut/enhancedfileserver"
|
"git.teamworkapps.com/shortcut/enhancedfileserver"
|
||||||
|
"github.com/andybalholm/brotli"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -51,6 +55,9 @@ func main() {
|
||||||
r.Use(noCache)
|
r.Use(noCache)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Always enable compression (both brotli and gzip)
|
||||||
|
r.Use(compression)
|
||||||
|
|
||||||
if c.noindex {
|
if c.noindex {
|
||||||
r.PathPrefix("/").Handler(http.StripPrefix("/", enhancedfileserver.FileServerNoIndex(http.Dir("./"))))
|
r.PathPrefix("/").Handler(http.StripPrefix("/", enhancedfileserver.FileServerNoIndex(http.Dir("./"))))
|
||||||
} else {
|
} else {
|
||||||
|
@ -92,3 +99,58 @@ func queryLog(next http.Handler) http.Handler {
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// compressionWriter wraps http.ResponseWriter to provide compression
|
||||||
|
type compressionWriter struct {
|
||||||
|
http.ResponseWriter
|
||||||
|
writer io.Writer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cw *compressionWriter) Write(b []byte) (int, error) {
|
||||||
|
return cw.writer.Write(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
// compression middleware that supports both brotli and gzip
|
||||||
|
func compression(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// Get the Accept-Encoding header
|
||||||
|
acceptEncoding := r.Header.Get("Accept-Encoding")
|
||||||
|
|
||||||
|
// Check if client supports brotli (preferred)
|
||||||
|
if strings.Contains(acceptEncoding, "br") {
|
||||||
|
w.Header().Set("Content-Encoding", "br")
|
||||||
|
w.Header().Set("Vary", "Accept-Encoding")
|
||||||
|
|
||||||
|
bw := brotli.NewWriter(w)
|
||||||
|
defer bw.Close()
|
||||||
|
|
||||||
|
cw := &compressionWriter{
|
||||||
|
ResponseWriter: w,
|
||||||
|
writer: bw,
|
||||||
|
}
|
||||||
|
|
||||||
|
next.ServeHTTP(cw, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if client supports gzip
|
||||||
|
if strings.Contains(acceptEncoding, "gzip") {
|
||||||
|
w.Header().Set("Content-Encoding", "gzip")
|
||||||
|
w.Header().Set("Vary", "Accept-Encoding")
|
||||||
|
|
||||||
|
gw := gzip.NewWriter(w)
|
||||||
|
defer gw.Close()
|
||||||
|
|
||||||
|
cw := &compressionWriter{
|
||||||
|
ResponseWriter: w,
|
||||||
|
writer: gw,
|
||||||
|
}
|
||||||
|
|
||||||
|
next.ServeHTTP(cw, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// No compression supported, serve normally
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue