From cf3b486615a03d11201e3905065b7098e277911d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Wed, 4 Oct 2023 22:57:06 +0200 Subject: [PATCH 1/5] feat: enable compression for file-server --- modules/caddyhttp/fileserver/command.go | 27 +++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/modules/caddyhttp/fileserver/command.go b/modules/caddyhttp/fileserver/command.go index 895c4f06d08..34171751319 100644 --- a/modules/caddyhttp/fileserver/command.go +++ b/modules/caddyhttp/fileserver/command.go @@ -31,6 +31,7 @@ import ( "github.com/caddyserver/caddy/v2" "github.com/caddyserver/caddy/v2/caddyconfig" "github.com/caddyserver/caddy/v2/modules/caddyhttp" + "github.com/caddyserver/caddy/v2/modules/caddyhttp/encode" caddytpl "github.com/caddyserver/caddy/v2/modules/caddyhttp/templates" ) @@ -60,6 +61,7 @@ respond with a file listing.`, cmd.Flags().BoolP("templates", "t", false, "Enable template rendering") cmd.Flags().BoolP("access-log", "", 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.RunE = caddycmd.WrapCommandFuncForCobra(cmdFileServer) cmd.AddCommand(&cobra.Command{ Use: "export-template", @@ -84,9 +86,34 @@ 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") var handlers []json.RawMessage + if compress { + encodings := make(caddy.ModuleMap, 2) + prefer := make([]string, 0, 2) + + gzip, err := caddy.GetModule("http.encoders.gzip") + if err != nil { + return 0, err + } + encodings["gzip"] = caddyconfig.JSON(gzip.New(), nil) + prefer = append(prefer, "gzip") + + zstd, err := caddy.GetModule("http.encoders.zstd") + if err != nil { + return 0, err + } + encodings["zstd"] = caddyconfig.JSON(zstd.New(), nil) + prefer = append(prefer, "zstd") + + handlers = append(handlers, caddyconfig.JSONModuleObject(encode.Encode{ + EncodingsRaw: encodings, + Prefer: prefer, + }, "handler", "encode", nil)) + } + if templates { handler := caddytpl.Templates{FileRoot: root} handlers = append(handlers, caddyconfig.JSONModuleObject(handler, "handler", "templates", nil)) From b5cd6f407021b6c096b16d5dda3c89f5ff9205cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Thu, 5 Oct 2023 00:17:53 +0200 Subject: [PATCH 2/5] refactor --- modules/caddyhttp/fileserver/command.go | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/modules/caddyhttp/fileserver/command.go b/modules/caddyhttp/fileserver/command.go index 34171751319..30d48254c0b 100644 --- a/modules/caddyhttp/fileserver/command.go +++ b/modules/caddyhttp/fileserver/command.go @@ -91,26 +91,22 @@ func cmdFileServer(fs caddycmd.Flags) (int, error) { var handlers []json.RawMessage if compress { - encodings := make(caddy.ModuleMap, 2) - prefer := make([]string, 0, 2) - - gzip, err := caddy.GetModule("http.encoders.gzip") + zstd, err := caddy.GetModule("http.encoders.zstd") if err != nil { return 0, err } - encodings["gzip"] = caddyconfig.JSON(gzip.New(), nil) - prefer = append(prefer, "gzip") - zstd, err := caddy.GetModule("http.encoders.zstd") + gzip, err := caddy.GetModule("http.encoders.gzip") if err != nil { return 0, err } - encodings["zstd"] = caddyconfig.JSON(zstd.New(), nil) - prefer = append(prefer, "zstd") handlers = append(handlers, caddyconfig.JSONModuleObject(encode.Encode{ - EncodingsRaw: encodings, - Prefer: prefer, + EncodingsRaw: caddy.ModuleMap{ + "zstd": caddyconfig.JSON(zstd.New(), nil), + "gzip": caddyconfig.JSON(gzip.New(), nil), + }, + Prefer: []string{"zstd", "gzip"}, }, "handler", "encode", nil)) } From 7208810d591048e8f2303e2435c7a39d685185c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Thu, 5 Oct 2023 00:20:49 +0200 Subject: [PATCH 3/5] const --- modules/caddyhttp/fileserver/command.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/caddyhttp/fileserver/command.go b/modules/caddyhttp/fileserver/command.go index 30d48254c0b..81482537682 100644 --- a/modules/caddyhttp/fileserver/command.go +++ b/modules/caddyhttp/fileserver/command.go @@ -93,12 +93,12 @@ func cmdFileServer(fs caddycmd.Flags) (int, error) { if compress { zstd, err := caddy.GetModule("http.encoders.zstd") if err != nil { - return 0, err + return caddy.ExitCodeFailedStartup, err } gzip, err := caddy.GetModule("http.encoders.gzip") if err != nil { - return 0, err + return caddy.ExitCodeFailedStartup, err } handlers = append(handlers, caddyconfig.JSONModuleObject(encode.Encode{ From 807acc42de970e0e847e010bfb46f3f5bdd8afbd Mon Sep 17 00:00:00 2001 From: Francis Lavoie Date: Tue, 10 Oct 2023 16:50:10 -0400 Subject: [PATCH 4/5] Update help text --- modules/caddyhttp/fileserver/command.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/modules/caddyhttp/fileserver/command.go b/modules/caddyhttp/fileserver/command.go index 81482537682..62f6b9867f6 100644 --- a/modules/caddyhttp/fileserver/command.go +++ b/modules/caddyhttp/fileserver/command.go @@ -51,6 +51,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) { From 79538783ab6b46058196fd9e4ae02d2d87258054 Mon Sep 17 00:00:00 2001 From: Matt Holt Date: Wed, 13 Dec 2023 20:38:15 -0700 Subject: [PATCH 5/5] Update modules/caddyhttp/fileserver/command.go --- modules/caddyhttp/fileserver/command.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/caddyhttp/fileserver/command.go b/modules/caddyhttp/fileserver/command.go index fdae0191eb8..2bc816743c6 100644 --- a/modules/caddyhttp/fileserver/command.go +++ b/modules/caddyhttp/fileserver/command.go @@ -65,7 +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().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{