Add brotli compression support

This commit is contained in:
Jan-Lukas Else 2022-01-14 23:52:26 +01:00
parent 88157e52e5
commit 049fee72bd
3 changed files with 10 additions and 1 deletions

1
go.mod
View File

@ -9,6 +9,7 @@ require (
git.jlel.se/jlelse/template-strings v0.0.0-20210617205924-cfa3bd35ae40
github.com/PuerkitoBio/goquery v1.8.0
github.com/alecthomas/chroma v0.10.0
github.com/andybalholm/brotli v1.0.4
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de
github.com/c2h5oh/datasize v0.0.0-20200825124411-48ed595a09d2
github.com/carlmjohnson/requests v0.22.1

2
go.sum
View File

@ -55,6 +55,8 @@ github.com/alecthomas/chroma v0.10.0 h1:7XDcGkCQopCNKjZHfYrNLraA+M7e0fMiJ/Mfikbf
github.com/alecthomas/chroma v0.10.0/go.mod h1:jtJATyUxlIORhUOFNA9NZDWGAQ8wpxQQqNSB4rjA/1s=
github.com/alexbrainman/sspi v0.0.0-20210105120005-909beea2cc74 h1:Kk6a4nehpJ3UuJRqlA3JxYxBZEqCeOmATOvrbT4p9RA=
github.com/alexbrainman/sspi v0.0.0-20210105120005-909beea2cc74/go.mod h1:cEWa1LVoE5KvSD9ONXsZrj0z6KqySlCCNKHlLzbqAt4=
github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY=
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/andybalholm/cascadia v1.0.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=

View File

@ -5,12 +5,14 @@ import (
"database/sql"
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"strconv"
"time"
"github.com/andybalholm/brotli"
"github.com/dchest/captcha"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
@ -40,7 +42,11 @@ func (a *goBlog) startServer() (err error) {
if a.cfg.Server.Logging {
h = h.Append(a.logMiddleware)
}
h = h.Append(middleware.Recoverer, middleware.Compress(flate.DefaultCompression), middleware.Heartbeat("/ping"), headAsGetHandler)
compressor := middleware.NewCompressor(flate.DefaultCompression)
compressor.SetEncoder("br", func(w io.Writer, _ int) io.Writer {
return brotli.NewWriter(w)
})
h = h.Append(middleware.Recoverer, compressor.Handler, middleware.Heartbeat("/ping"), headAsGetHandler)
if a.httpsConfigured(false) {
h = h.Append(a.securityHeaders)
}