@marcel91200 / caddy Mirror · read-only / commits / b16aba5c
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 12, 19:30 UTC

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 <lavofr@gmail.com> Co-authored-by: Matt Holt <mholt@users.noreply.github.com>

Kévin Dunglas committed Dec 14, 2023 at 04:44 UTC b16aba5c27ace2c6ada9c2525f44329bf6dfcd37
1 file changed +25
modules/caddyhttp/fileserver/command.go
+25
@@ -52,6 +52,9 @@ will be changed to the HTTPS port and the server will use HTTPS. If using
52 a public domain, ensure A/AAAA records are properly configured before
53 using this option.
54
55 +By default, Zstandard and Gzip compression are enabled. Use --no-compress
56 +to disable compression.
57 +
58 If --browse is enabled, requests for folders without an index file will
59 respond with a file listing.`,
60 CobraFunc: func(cmd *cobra.Command) {
@@ -62,6 +65,7 @@ respond with a file listing.`,
65 cmd.Flags().BoolP("templates", "t", false, "Enable template rendering")
66 cmd.Flags().BoolP("access-log", "a", false, "Enable the access log")
67 cmd.Flags().BoolP("debug", "v", false, "Enable verbose debug logs")
68 + cmd.Flags().BoolP("no-compress", "", false, "Disable Zstandard and Gzip compression")
69 cmd.Flags().StringSliceP("precompressed", "p", []string{}, "Specify precompression file extensions. Compression preference implied from flag order.")
70 cmd.RunE = caddycmd.WrapCommandFuncForCobra(cmdFileServer)
71 cmd.AddCommand(&cobra.Command{
@@ -87,6 +91,7 @@ func cmdFileServer(fs caddycmd.Flags) (int, error) {
91 templates := fs.Bool("templates")
92 accessLog := fs.Bool("access-log")
93 debug := fs.Bool("debug")
94 + compress := !fs.Bool("no-compress")
95 precompressed, err := fs.GetStringSlice("precompressed")
96 if err != nil {
97 return caddy.ExitCodeFailedStartup, fmt.Errorf("invalid precompressed flag: %v", err)
@@ -94,6 +99,26 @@ func cmdFileServer(fs caddycmd.Flags) (int, error) {
99
100 var handlers []json.RawMessage
101
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 +
122 if templates {
123 handler := caddytpl.Templates{FileRoot: root}
124 handlers = append(handlers, caddyconfig.JSONModuleObject(handler, "handler", "templates", nil))