fileserver: Add --templates flag to file-server command

This commit is contained in:
Matthew Holt 2020-03-14 23:31:52 -06:00
parent acf4dde1dd
commit 2ce3deb540
No known key found for this signature in database
GPG Key ID: 2A349DD577D586A5
1 changed files with 15 additions and 6 deletions

View File

@ -25,6 +25,7 @@ import (
"github.com/caddyserver/caddy/v2/caddyconfig" "github.com/caddyserver/caddy/v2/caddyconfig"
caddycmd "github.com/caddyserver/caddy/v2/cmd" caddycmd "github.com/caddyserver/caddy/v2/cmd"
"github.com/caddyserver/caddy/v2/modules/caddyhttp" "github.com/caddyserver/caddy/v2/modules/caddyhttp"
caddytpl "github.com/caddyserver/caddy/v2/modules/caddyhttp/templates"
"github.com/caddyserver/certmagic" "github.com/caddyserver/certmagic"
) )
@ -52,7 +53,8 @@ respond with a file listing.`,
fs.String("domain", "", "Domain name at which to serve the files") fs.String("domain", "", "Domain name at which to serve the files")
fs.String("root", "", "The path to the root of the site") fs.String("root", "", "The path to the root of the site")
fs.String("listen", "", "The address to which to bind the listener") fs.String("listen", "", "The address to which to bind the listener")
fs.Bool("browse", false, "Whether to enable directory browsing") fs.Bool("browse", false, "Enable directory browsing")
fs.Bool("templates", false, "Enable template rendering")
return fs return fs
}(), }(),
}) })
@ -63,17 +65,24 @@ func cmdFileServer(fs caddycmd.Flags) (int, error) {
root := fs.String("root") root := fs.String("root")
listen := fs.String("listen") listen := fs.String("listen")
browse := fs.Bool("browse") browse := fs.Bool("browse")
templates := fs.Bool("templates")
var handlers []json.RawMessage
if templates {
handler := caddytpl.Templates{FileRoot: root}
handlers = append(handlers, caddyconfig.JSONModuleObject(handler, "handler", "templates", nil))
}
handler := FileServer{Root: root} handler := FileServer{Root: root}
if browse { if browse {
handler.Browse = new(Browse) handler.Browse = new(Browse)
} }
route := caddyhttp.Route{ handlers = append(handlers, caddyconfig.JSONModuleObject(handler, "handler", "file_server", nil))
HandlersRaw: []json.RawMessage{
caddyconfig.JSONModuleObject(handler, "handler", "file_server", nil), route := caddyhttp.Route{HandlersRaw: handlers}
},
}
if domain != "" { if domain != "" {
route.MatcherSetsRaw = []caddy.ModuleMap{ route.MatcherSetsRaw = []caddy.ModuleMap{
caddy.ModuleMap{ caddy.ModuleMap{