mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-28 06:33:47 +03:00
file_server: Automatically hide all involved Caddyfiles
This commit is contained in:
parent
8420a2f250
commit
c0da7d487a
3 changed files with 30 additions and 0 deletions
|
@ -81,6 +81,26 @@ type Helper struct {
|
||||||
*caddyfile.Dispenser
|
*caddyfile.Dispenser
|
||||||
warnings *[]caddyconfig.Warning
|
warnings *[]caddyconfig.Warning
|
||||||
matcherDefs map[string]map[string]json.RawMessage
|
matcherDefs map[string]map[string]json.RawMessage
|
||||||
|
parentBlock caddyfile.ServerBlock
|
||||||
|
}
|
||||||
|
|
||||||
|
// Caddyfiles returns the list of config files from
|
||||||
|
// which tokens in the current server block were loaded.
|
||||||
|
func (h Helper) Caddyfiles() []string {
|
||||||
|
// first obtain set of names of files involved
|
||||||
|
// in this server block, without duplicates
|
||||||
|
files := make(map[string]struct{})
|
||||||
|
for _, segment := range h.parentBlock.Segments {
|
||||||
|
for _, token := range segment {
|
||||||
|
files[token.File] = struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// then convert the set into a slice
|
||||||
|
filesSlice := make([]string, 0, len(files))
|
||||||
|
for file := range files {
|
||||||
|
filesSlice = append(filesSlice, file)
|
||||||
|
}
|
||||||
|
return filesSlice
|
||||||
}
|
}
|
||||||
|
|
||||||
// JSON converts val into JSON. Any errors are added to warnings.
|
// JSON converts val into JSON. Any errors are added to warnings.
|
||||||
|
|
|
@ -95,6 +95,7 @@ func (st ServerType) Setup(originalServerBlocks []caddyfile.ServerBlock,
|
||||||
Dispenser: caddyfile.NewDispenser(segment),
|
Dispenser: caddyfile.NewDispenser(segment),
|
||||||
warnings: &warnings,
|
warnings: &warnings,
|
||||||
matcherDefs: matcherDefs,
|
matcherDefs: matcherDefs,
|
||||||
|
parentBlock: sb.block,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, warnings, fmt.Errorf("parsing caddyfile tokens for '%s': %v", dir, err)
|
return nil, warnings, fmt.Errorf("parsing caddyfile tokens for '%s': %v", dir, err)
|
||||||
|
|
|
@ -76,6 +76,15 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error)
|
||||||
fsrv.Root = "{http.var.root}"
|
fsrv.Root = "{http.var.root}"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// hide the Caddyfile (and any imported Caddyfiles)
|
||||||
|
if configFiles := h.Caddyfiles(); len(configFiles) > 0 {
|
||||||
|
for _, file := range configFiles {
|
||||||
|
if !fileHidden(file, fsrv.Hide) {
|
||||||
|
fsrv.Hide = append(fsrv.Hide, file)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return &fsrv, nil
|
return &fsrv, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue