8000 Merge pull request #198 from shyim/main · symfony-cli/symfony-cli@071eeb8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 071eeb8

Browse files
authored
Merge pull request #198 from shyim/main
feat: add gzip for local webserver
2 parents b54f021 + 0d5967b commit 071eeb8

File tree

6 files changed

+44
-2
lines changed

6 files changed

+44
-2
lines changed

commands/local_server_start.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ var localServerStartCmd = &console.Command{
6767
&console.BoolFlag{Name: "no-humanize", Usage: "Do not format JSON logs"},
6868
&console.StringFlag{Name: "p12", Usage: "Name of the file containing the TLS certificate to use in p12 format"},
6969
&console.BoolFlag{Name: "no-tls", Usage: "Use HTTP instead of HTTPS"},
70+
&console.BoolFlag{Name: "use-gzip", Usage: "Use GZIP"},
7071
},
7172
Action: func(c *console.Context) error {
7273
ui := terminal.SymfonyStyle(terminal.Stdout, terminal.Stdin)

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: 33 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"
@@ -51,13 +52,29 @@ type Server struct {
5152
AllowHTTP bool
5253
Logger zerolog.Logger
5354
Appversion string
55+
UseGzip bool
5456

5557
httpserver *http.Server
5658
httpsserver *http.Server
5759

5860
serverPort string
5961
}
6062

63+
var gzipContentTypes = []string{
64+
"text/html",
65+
"text/plain",
66+
"text/csv",
67+
"text/javascript",
68+
"text/css",
69+
"text/xml",
70+
"application/json",
71+
"application/javascript",
72+
"application/vnd.api+json",
73+
"application/atom+xml",
74+
"application/rss+xml",
75+
"image/svg+xml",
76+
}
77+
6178
// Start starts the server
6279
func (s *Server) Start(errChan chan error) (int, error) {
6380
ln, port, err := process.CreateListener(s.Port, s.PreferredPort)
@@ -66,8 +83,22 @@ func (s *Server) Start(errChan chan error) (int, error) {
6683
}
6784
s.serverPort = strconv.Itoa(port)
6885

86+
var proxyHandler http.Handler
87+
88+
proxyHandler = http.HandlerFunc(s.ProxyHandler)
89+
90+
if s.UseGzip {
91+
gzipWrapper, err := gziphandler.GzipHandlerWithOpts(gziphandler.ContentTypes(gzipContentTypes))
92+
93+
if err != nil {
94+
return port, errors.WithStack(err)
95+
}
96+
97+
proxyHandler = gzipWrapper(proxyHandler)
98+
}
99+
69100
s.httpserver = &http.Server{
70-
Handler: http.HandlerFunc(s.ProxyHandler),
101+
Handler: proxyHandler,
71102
}
72103
if s.PKCS12 == "" {
73104
go func() {
@@ -83,7 +114,7 @@ func (s *Server) Start(errChan chan error) (int, error) {
83114
}
84115

85116
s.httpsserver = &http.Server{
86-
Handler: http.HandlerFunc(s.ProxyHandler),
117+
Handler: proxyHandler,
87118
TLSConfig: &tls.Config{
88119
PreferServerCipherSuites: true,
89120
CurvePreferences: []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256},

local/project/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type Config struct {
4444
AllowHTTP bool `yaml:"allow_http"`
4545
NoTLS bool `yaml:"no_tls"`
4646
Daemon bool `yaml:"daemon"`
47+
UseGzip bool `yaml:"use_gzip"`
4748
}
4849

4950
type FileConfig struct {
@@ -103,6 +104,11 @@ func NewConfigFromContext(c *console.Context, projectDir string) (*Config, *File
103104
if c.IsSet("daemon") {
104105
config.Daemon = c.Bool("daemon")
105106
}
107+
108+
if c.IsSet("use-gzip") {
109+
config.UseGzip = c.Bool("use-gzip")
110+
}
111+
106112
return config, fileConfig, nil
107113
}
108114

local/project/project.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func New(c *Config) (*Project, error) {
6060
Logger: c.Logger,
6161
PKCS12: c.PKCS12,
6262
AllowHTTP: c.AllowHTTP,
63+
UseGzip: c.UseGzip,
6364
Appversion: c.AppVersion,
6465
},
6566
}

0 commit comments

Comments
 (0)
0