Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fileserver: Add precompressed flag to command #5880

Merged
merged 2 commits into from
Dec 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions modules/caddyhttp/fileserver/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package fileserver

import (
"encoding/json"
"fmt"
"io"
"log"
"os"
Expand All @@ -31,13 +32,14 @@ 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"
)

func init() {
caddycmd.RegisterCommand(caddycmd.Command{
Name: "file-server",
Usage: "[--domain <example.com>] [--root <path>] [--listen <addr>] [--browse] [--access-log]",
Usage: "[--domain <example.com>] [--root <path>] [--listen <addr>] [--browse] [--access-log] [--precompressed]",
Short: "Spins up a production-ready file server",
Long: `
A simple but production-ready file server. Useful for quick deployments,
Expand All @@ -60,6 +62,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().StringSliceP("precompressed", "p", []string{}, "Specify precompression file extensions. Compression preference implied from flag order.")
cmd.RunE = caddycmd.WrapCommandFuncForCobra(cmdFileServer)
cmd.AddCommand(&cobra.Command{
Use: "export-template",
Expand All @@ -84,6 +87,10 @@ func cmdFileServer(fs caddycmd.Flags) (int, error) {
templates := fs.Bool("templates")
accessLog := fs.Bool("access-log")
debug := fs.Bool("debug")
precompressed, err := fs.GetStringSlice("precompressed")
if err != nil {
return caddy.ExitCodeFailedStartup, fmt.Errorf("invalid precompressed flag: %v", err)
}

var handlers []json.RawMessage

Expand All @@ -93,6 +100,30 @@ func cmdFileServer(fs caddycmd.Flags) (int, error) {
}

handler := FileServer{Root: root}

if len(precompressed) != 0 {
// logic mirrors modules/caddyhttp/fileserver/caddyfile.go case "precompressed"
var order []string
for _, compression := range precompressed {
modID := "http.precompressed." + compression
mod, err := caddy.GetModule(modID)
if err != nil {
return caddy.ExitCodeFailedStartup, fmt.Errorf("getting module named '%s': %v", modID, err)
}
inst := mod.New()
precompress, ok := inst.(encode.Precompressed)
if !ok {
return caddy.ExitCodeFailedStartup, fmt.Errorf("module %s is not a precompressor; is %T", modID, inst)
}
if handler.PrecompressedRaw == nil {
handler.PrecompressedRaw = make(caddy.ModuleMap)
}
handler.PrecompressedRaw[compression] = caddyconfig.JSON(precompress, nil)
order = append(order, compression)
}
handler.PrecompressedOrder = order
}

if browse {
handler.Browse = new(Browse)
}
Expand Down Expand Up @@ -154,7 +185,7 @@ func cmdFileServer(fs caddycmd.Flags) (int, error) {
}
}

err := caddy.Run(cfg)
err = caddy.Run(cfg)
if err != nil {
return caddy.ExitCodeFailedStartup, err
}
Expand Down
Loading