Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

caddyfile: Add support for args on imports #3423

Merged
merged 2 commits into from
Jun 1, 2020

Conversation

francislavoie
Copy link
Member

Stems from the discussion in https://caddy.community/t/logging-snippet-using-host-placeholder-for-log-filename/8218

The idea here is that it's common for users to want to have one element of an imported snippet or file be replaced with a context-specific value.

This PR adds support for {args.*} placeholders at Caddyfile-parse time when importing tokens from either a snippet or file, where * is the positional argument number.

The import syntax becomes:

import <pattern> <args...>

For example, consider this Caddyfile, where a user wants to enable logging using a common config for each of their sites. You'll see that the log filename is expanded to /var/log/caddy/a.example.com.access.log:

(logging) {
  log {
    output file /var/log/caddy/{args.0}.access.log
  }
}

a.example.com {
  import logging a.example.com
}

b.example.com {
  import logging b.example.com
}

Adapted JSON output:

{
	"logging": {
		"logs": {
			"default": {
				"exclude": [
					"http.log.access.log0",
					"http.log.access.log1"
				]
			},
			"log0": {
				"writer": {
					"filename": "/var/log/caddy/a.example.com.access.log",
					"output": "file"
				},
				"include": [
					"http.log.access.log0"
				]
			},
			"log1": {
				"writer": {
					"filename": "/var/log/caddy/b.example.com.access.log",
					"output": "file"
				},
				"include": [
					"http.log.access.log1"
				]
			}
		}
	},
	"apps": {
		"http": {
			"servers": {
				"srv0": {
					"listen": [
						":443"
					],
					"routes": [
						{
							"match": [
								{
									"host": [
										"a.example.com"
									]
								}
							],
							"terminal": true
						},
						{
							"match": [
								{
									"host": [
										"b.example.com"
									]
								}
							],
							"terminal": true
						}
					],
					"logs": {
						"logger_names": {
							"a.example.com": "log0",
							"b.example.com": "log1"
						}
					}
				}
			}
		}
	}
}

This also supports an arbitrary number of arguments, and supports imported files.

The file respond.txt

respond "I am {args.0}, hears {args.1}"

Caddyfile:

example.com

import respond.txt Groot Rocket
import respond.txt you "the confused man"

Adapted JSON:

{
	"apps": {
		"http": {
			"servers": {
				"srv0": {
					"listen": [
						":443"
					],
					"routes": [
						{
							"match": [
								{
									"host": [
										"example.com"
									]
								}
							],
							"handle": [
								{
									"handler": "subroute",
									"routes": [
										{
											"handle": [
												{
													"body": "I am groot, hears rocket",
													"handler": "static_response"
												},
												{
													"body": "I am you, hears the confused man",
													"handler": "static_response"
												}
											]
										}
									]
								}
							],
							"terminal": true
						}
					]
				}
			}
		}
	}
}

@francislavoie francislavoie added this to the 2.1 milestone May 18, 2020
@francislavoie francislavoie requested a review from mholt May 18, 2020 07:04
@francislavoie francislavoie force-pushed the import-args branch 3 times, most recently from 2df3cf4 to 8bd493f Compare May 18, 2020 07:33
@sumnerboy12
Copy link

Looks great @francislavoie, thanks!

@mholt mholt added the under review 🧐 Review is pending before merging label May 18, 2020
Copy link
Member

@mholt mholt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, we can give this a try. Thank you!

@mholt mholt merged commit fdf2a77 into caddyserver:master Jun 1, 2020
@mholt mholt removed the under review 🧐 Review is pending before merging label Jun 1, 2020
@francislavoie francislavoie deleted the import-args branch June 1, 2020 19:18
mholt added a commit to caddyserver/website that referenced this pull request May 11, 2021
* Added documentation for snippet arguments

Added documentation, the only resource available for this feature, a post on the forum and the PR : caddyserver/caddy#3423

* Update src/docs/markdown/caddyfile/concepts.md

Co-authored-by: Matt Holt <mholt@users.noreply.github.com>

* Update src/docs/markdown/caddyfile/concepts.md

Co-authored-by: Matt Holt <mholt@users.noreply.github.com>

* Change for more generic example

Co-authored-by: Matt Holt <mholt@users.noreply.github.com>

Co-authored-by: Matt Holt <mholt@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants