Skip to content

Commit b16aba5

Browse files
dunglasfrancislavoiemholt
authored
fileserver: Enable compression for command by default (#5855)
* feat: enable compression for file-server * refactor * const * Update help text * Update modules/caddyhttp/fileserver/command.go --------- Co-authored-by: Francis Lavoie <[email protected]> Co-authored-by: Matt Holt <[email protected]>
1 parent 362f33d commit b16aba5

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

modules/caddyhttp/fileserver/command.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ will be changed to the HTTPS port and the server will use HTTPS. If using
5252
a public domain, ensure A/AAAA records are properly configured before
5353
using this option.
5454
55+
By default, Zstandard and Gzip compression are enabled. Use --no-compress
56+
to disable compression.
57+
5558
If --browse is enabled, requests for folders without an index file will
5659
respond with a file listing.`,
5760
CobraFunc: func(cmd *cobra.Command) {
@@ -62,6 +65,7 @@ respond with a file listing.`,
6265
cmd.Flags().BoolP("templates", "t", false, "Enable template rendering")
6366
cmd.Flags().BoolP("access-log", "a", false, "Enable the access log")
6467
cmd.Flags().BoolP("debug", "v", false, "Enable verbose debug logs")
68+
cmd.Flags().BoolP("no-compress", "", false, "Disable Zstandard and Gzip compression")
6569
cmd.Flags().StringSliceP("precompressed", "p", []string{}, "Specify precompression file extensions. Compression preference implied from flag order.")
6670
cmd.RunE = caddycmd.WrapCommandFuncForCobra(cmdFileServer)
6771
cmd.AddCommand(&cobra.Command{
@@ -87,13 +91,34 @@ func cmdFileServer(fs caddycmd.Flags) (int, error) {
8791
templates := fs.Bool("templates")
8892
accessLog := fs.Bool("access-log")
8993
debug := fs.Bool("debug")
94+
compress := !fs.Bool("no-compress")
9095
precompressed, err := fs.GetStringSlice("precompressed")
9196
if err != nil {
9297
return caddy.ExitCodeFailedStartup, fmt.Errorf("invalid precompressed flag: %v", err)
9398
}
9499

95100
var handlers []json.RawMessage
96101

102+
if compress {
103+
zstd, err := caddy.GetModule("http.encoders.zstd")
104+
if err != nil {
105+
return caddy.ExitCodeFailedStartup, err
106+
}
107+
108+
gzip, err := caddy.GetModule("http.encoders.gzip")
109+
if err != nil {
110+
return caddy.ExitCodeFailedStartup, err
111+
}
112+
113+
handlers = append(handlers, caddyconfig.JSONModuleObject(encode.Encode{
114+
EncodingsRaw: caddy.ModuleMap{
115+
"zstd": caddyconfig.JSON(zstd.New(), nil),
116+
"gzip": caddyconfig.JSON(gzip.New(), nil),
117+
},
118+
Prefer: []string{"zstd", "gzip"},
119+
}, "handler", "encode", nil))
120+
}
121+
97122
if templates {
98123
handler := caddytpl.Templates{FileRoot: root}
99124
handlers = append(handlers, caddyconfig.JSONModuleObject(handler, "handler", "templates", nil))

0 commit comments

Comments
 (0)