8000 feat: add gzip for local webserver · symfony-cli/symfony-cli@d65bb39 · GitHub
[go: up one dir, main page]

Skip to content

Commit d65bb39

Browse files
committed
feat: add gzip for local webserver
1 parent 364ecc3 commit d65bb39

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ require (
3131

3232
require (
3333
github.com/Microsoft/go-winio v0.6.0 // indirect
34+
github.com/NYTimes/gziphandler v1.1.1
3435
github.com/distribution/distribution/v3 v3.0.0-20220907155224-78b9c98c5c31 // indirect
3536
github.com/docker/distribution v2.8.1+incompatible // indirect
3637
github.com/docker/go-connections v0.4.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
4242
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
4343
github.com/Microsoft/go-winio v0.6.0 h1:slsWYD/zyx7lCXoZVlvQrj0hPTM1HI4+v1sIda2yDvg=
4444
github.com/Microsoft/go-winio v0.6.0/go.mod h1:cTAf44im0RAYeL23bpB+fzCyDH2MJiz2BO69KH/soAE=
45+
github.com/NYTimes/gziphandler v1.1.1 h1:ZUDjpQae29j0ryrS0u/B8HZfJBtBQHjqw2rQ2cqUQ3I=
46+
github.com/NYTimes/gziphandler v1.1.1/go.mod h1:n/CVRwUEOgIxrgPvAQhUUr9oeUtvrhMomdKFjzJNB0c=
4547
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
4648
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
4749
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=

local/http/http.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"strconv"
3131
"strings"
3232

33+
"github.com/NYTimes/gziphandler"
3334
"github.com/pkg/errors"
3435
"github.com/rs/zerolog"
3536
"github.com/soheilhy/cmux"
@@ -58,6 +59,17 @@ type Server struct {
5859
serverPort string
5960
}
6061

62+
var gzipContentTypes = []string{
63+
"text/html",
64+
"text/plain",
65+
"text/csv",
66+
"text/javascript",
67+
"text/css",
68+
"application/json",
69+
"application/javascript",
70+
"application/vnd.api+json",
71+
}
72+
6173
// Start starts the server
6274
func (s *Server) Start(errChan chan error) (int, error) {
6375
ln, port, err := process.CreateListener(s.Port, s.PreferredPort)
@@ -66,8 +78,14 @@ func (s *Server) Start(errChan chan error) (int, error) {
6678
}
6779
s.serverPort = strconv.Itoa(port)
6880

81+
gzipWrapper, err := gziphandler.GzipHandlerWithOpts(gziphandler.ContentTypes(gzipContentTypes))
82+
83+
if err != nil {
84+
return port, err
85+
}
86+
6987
s.httpserver = &http.Server{
70-
Handler: http.HandlerFunc(s.ProxyHandler),
88+
Handler: gzipWrapper(http.HandlerFunc(s.ProxyHandler)),
7189
}
7290
if s.PKCS12 == "" {
7391
go func() {
@@ -83,7 +101,7 @@ func (s *Server) Start(errChan chan error) (int, error) {
83101
}
84102

85103
s.httpsserver = &http.Server{
86-
Handler: http.HandlerFunc(s.ProxyHandler),
104+
Handler: gzipWrapper(http.HandlerFunc(s.ProxyHandler)),
87105
TLSConfig: &tls.Config{
88106
PreferServerCipherSuites: true,
89107
CurvePreferences: []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256},

0 commit comments

Comments
 (0)
0