mirror of
https://github.com/caddyserver/caddy.git
synced 2026-05-13 09:06:41 +00:00
caddyfile: Allow block to do nothing if nothing passed to import (#7206)
This commit is contained in:
parent
bcd4055e89
commit
0ba8786b35
3 changed files with 120 additions and 11 deletions
|
|
@ -533,29 +533,24 @@ func (p *parser) doImport(nesting int) error {
|
||||||
}
|
}
|
||||||
// if it is {block}, we substitute with all tokens in the block
|
// if it is {block}, we substitute with all tokens in the block
|
||||||
// if it is {blocks.*}, we substitute with the tokens in the mapping for the *
|
// if it is {blocks.*}, we substitute with the tokens in the mapping for the *
|
||||||
var skip bool
|
|
||||||
var tokensToAdd []Token
|
var tokensToAdd []Token
|
||||||
|
foundBlockDirective := false
|
||||||
switch {
|
switch {
|
||||||
case token.Text == "{block}":
|
case token.Text == "{block}":
|
||||||
|
foundBlockDirective = true
|
||||||
tokensToAdd = blockTokens
|
tokensToAdd = blockTokens
|
||||||
case strings.HasPrefix(token.Text, "{blocks.") && strings.HasSuffix(token.Text, "}"):
|
case strings.HasPrefix(token.Text, "{blocks.") && strings.HasSuffix(token.Text, "}"):
|
||||||
|
foundBlockDirective = true
|
||||||
// {blocks.foo.bar} will be extracted to key `foo.bar`
|
// {blocks.foo.bar} will be extracted to key `foo.bar`
|
||||||
blockKey := strings.TrimPrefix(strings.TrimSuffix(token.Text, "}"), "{blocks.")
|
blockKey := strings.TrimPrefix(strings.TrimSuffix(token.Text, "}"), "{blocks.")
|
||||||
val, ok := blockMapping[blockKey]
|
val, ok := blockMapping[blockKey]
|
||||||
if ok {
|
if ok {
|
||||||
tokensToAdd = val
|
tokensToAdd = val
|
||||||
}
|
}
|
||||||
default:
|
|
||||||
skip = true
|
|
||||||
}
|
}
|
||||||
if !skip {
|
|
||||||
if len(tokensToAdd) == 0 {
|
if foundBlockDirective {
|
||||||
// if there is no content in the snippet block, don't do any replacement
|
tokensCopy = append(tokensCopy, tokensToAdd...)
|
||||||
// this allows snippets which contained {block}/{block.*} before this change to continue functioning as normal
|
|
||||||
tokensCopy = append(tokensCopy, token)
|
|
||||||
} else {
|
|
||||||
tokensCopy = append(tokensCopy, tokensToAdd...)
|
|
||||||
}
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,57 @@
|
||||||
|
(snippet) {
|
||||||
|
header {
|
||||||
|
reverse_proxy localhost:3000
|
||||||
|
{block}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
example.com {
|
||||||
|
import snippet
|
||||||
|
}
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
":443"
|
||||||
|
],
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"host": [
|
||||||
|
"example.com"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "subroute",
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "headers",
|
||||||
|
"response": {
|
||||||
|
"set": {
|
||||||
|
"Reverse_proxy": [
|
||||||
|
"localhost:3000"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"terminal": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,57 @@
|
||||||
|
(snippet) {
|
||||||
|
header {
|
||||||
|
reverse_proxy localhost:3000
|
||||||
|
{blocks.content_type}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
example.com {
|
||||||
|
import snippet
|
||||||
|
}
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
":443"
|
||||||
|
],
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"host": [
|
||||||
|
"example.com"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "subroute",
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "headers",
|
||||||
|
"response": {
|
||||||
|
"set": {
|
||||||
|
"Reverse_proxy": [
|
||||||
|
"localhost:3000"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"terminal": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue