aboutsummaryrefslogtreecommitdiffhomepage
path: root/modules/caddyhttp/fileserver
diff options
context:
space:
mode:
authorKévin Dunglas <[email protected]>2023-12-14 04:44:22 +0100
committerGitHub <[email protected]>2023-12-13 20:44:22 -0700
commitb16aba5c27ace2c6ada9c2525f44329bf6dfcd37 (patch)
treed63daf59d6966b9f94597383aa04a7fdf4bf17bd /modules/caddyhttp/fileserver
parent362f33daae13c31e6d7d5e73fb79aba5a1e9deef (diff)
downloadcaddy-b16aba5c27ace2c6ada9c2525f44329bf6dfcd37.tar.gz
caddy-b16aba5c27ace2c6ada9c2525f44329bf6dfcd37.zip
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]>
Diffstat (limited to 'modules/caddyhttp/fileserver')
-rw-r--r--modules/caddyhttp/fileserver/command.go25
1 files changed, 25 insertions, 0 deletions
diff --git a/modules/caddyhttp/fileserver/command.go b/modules/caddyhttp/fileserver/command.go
index 3550c20da..2bc816743 100644
--- a/modules/caddyhttp/fileserver/command.go
+++ b/modules/caddyhttp/fileserver/command.go
@@ -52,6 +52,9 @@ will be changed to the HTTPS port and the server will use HTTPS. If using
a public domain, ensure A/AAAA records are properly configured before
using this option.
+By default, Zstandard and Gzip compression are enabled. Use --no-compress
+to disable compression.
+
If --browse is enabled, requests for folders without an index file will
respond with a file listing.`,
CobraFunc: func(cmd *cobra.Command) {
@@ -62,6 +65,7 @@ respond with a file listing.`,
cmd.Flags().BoolP("templates", "t", false, "Enable template rendering")
cmd.Flags().BoolP("access-log", "a", false, "Enable the access log")
cmd.Flags().BoolP("debug", "v", false, "Enable verbose debug logs")
+ cmd.Flags().BoolP("no-compress", "", false, "Disable Zstandard and Gzip compression")
cmd.Flags().StringSliceP("precompressed", "p", []string{}, "Specify precompression file extensions. Compression preference implied from flag order.")
cmd.RunE = caddycmd.WrapCommandFuncForCobra(cmdFileServer)
cmd.AddCommand(&cobra.Command{
@@ -87,6 +91,7 @@ func cmdFileServer(fs caddycmd.Flags) (int, error) {
templates := fs.Bool("templates")
accessLog := fs.Bool("access-log")
debug := fs.Bool("debug")
+ compress := !fs.Bool("no-compress")
precompressed, err := fs.GetStringSlice("precompressed")
if err != nil {
return caddy.ExitCodeFailedStartup, fmt.Errorf("invalid precompressed flag: %v", err)
@@ -94,6 +99,26 @@ func cmdFileServer(fs caddycmd.Flags) (int, error) {
var handlers []json.RawMessage
+ if compress {
+ zstd, err := caddy.GetModule("http.encoders.zstd")
+ if err != nil {
+ return caddy.ExitCodeFailedStartup, err
+ }
+
+ gzip, err := caddy.GetModule("http.encoders.gzip")
+ if err != nil {
+ return caddy.ExitCodeFailedStartup, err
+ }
+
+ handlers = append(handlers, caddyconfig.JSONModuleObject(encode.Encode{
+ EncodingsRaw: caddy.ModuleMap{
+ "zstd": caddyconfig.JSON(zstd.New(), nil),
+ "gzip": caddyconfig.JSON(gzip.New(), nil),
+ },
+ Prefer: []string{"zstd", "gzip"},
+ }, "handler", "encode", nil))
+ }
+
if templates {
handler := caddytpl.Templates{FileRoot: root}
handlers = append(handlers, caddyconfig.JSONModuleObject(handler, "handler", "templates", nil))