fileserver: Export BrowseTemplate

This allows programs embedding Caddy to customize the browse template.
This commit is contained in:
Matthew Holt 2023-08-29 09:34:20 -06:00
parent b7e472d548
commit ed8bb13c5d
No known key found for this signature in database
GPG key ID: 2A349DD577D586A5
2 changed files with 10 additions and 4 deletions

View file

@ -36,12 +36,18 @@ import (
"github.com/caddyserver/caddy/v2/modules/caddyhttp/templates" "github.com/caddyserver/caddy/v2/modules/caddyhttp/templates"
) )
// BrowseTemplate is the default template document to use for
// file listings. By default, its default value is an embedded
// document. You can override this value at program start, or
// if you are running Caddy via config, you can specify a
// custom template_file in the browse configuration.
//
//go:embed browse.html //go:embed browse.html
var defaultBrowseTemplate string var BrowseTemplate string
// Browse configures directory browsing. // Browse configures directory browsing.
type Browse struct { type Browse struct {
// Use this template file instead of the default browse template. // Filename of the template to use instead of the embedded browse template.
TemplateFile string `json:"template_file,omitempty"` TemplateFile string `json:"template_file,omitempty"`
} }
@ -205,7 +211,7 @@ func (fsrv *FileServer) makeBrowseTemplate(tplCtx *templateContext) (*template.T
} }
} else { } else {
tpl = tplCtx.NewTemplate("default_listing") tpl = tplCtx.NewTemplate("default_listing")
tpl, err = tpl.Parse(defaultBrowseTemplate) tpl, err = tpl.Parse(BrowseTemplate)
if err != nil { if err != nil {
return nil, fmt.Errorf("parsing default browse template: %v", err) return nil, fmt.Errorf("parsing default browse template: %v", err)
} }

View file

@ -66,7 +66,7 @@ respond with a file listing.`,
Short: "Exports the default file browser template", Short: "Exports the default file browser template",
Example: "caddy file-server export-template > browse.html", Example: "caddy file-server export-template > browse.html",
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
_, err := io.WriteString(os.Stdout, defaultBrowseTemplate) _, err := io.WriteString(os.Stdout, BrowseTemplate)
return err return err
}, },
}) })