mirror of
https://github.com/caddyserver/caddy.git
synced 2024-11-22 08:04:02 +08:00
fileserver: New --precompressed flag (#5880)
exposes the file_server precompressed functionality to be used with the file-server command Co-authored-by: Matt Holt <mholt@users.noreply.github.com>
This commit is contained in:
parent
3d7d60f7cf
commit
362f33daae
|
@ -16,6 +16,7 @@ package fileserver
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
|
@ -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,
|
||||
|
@ -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",
|
||||
|
@ -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
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user