Is there a way to put blocks and/or snippets in the Caddyfile conditionally depending on env vars for Caddy v2?
{
# staging lets encrypt for testing. Comment out for production.
acme_ca https://acme-staging-v02.api.letsencrypt.org/directory # Want to disable this if $CURRENT_ENV is not "prod"
admin off # Want to enable this if $CURRENT_ENV is not "dev"
debug # Want to enable this if $LOG_LEVEL DEBUG or lower
}
{
…
log {
level ${LOG_LEVEL}
# format single_field common_log
output file {$LOG_FILE} {
roll_size 10MB
roll_keep 10
}
}
}
If I can’t do this as above, outside of me using an external templating library, I am thinking could set the default config/Caddyfile to reflect what should be in prod and then patch it to be what I want for dev, staging and test environment
You can have literal Caddyfile tokens as your env var values, and they get expanded in-place. They can even be multiline (if the env var values contain newlines).
But there’s no conditional statements for this. You can set a default value for the env though.
export $Global_block = "
{
# staging lets encrypt for testing. Comment out for production.
acme_ca https://acme-staging-v02.api.letsencrypt.org/directory # Want to disable this if $CURRENT_ENV is not "prod"
admin off # Want to enable this if $CURRENT_ENV is not "dev"
debug # Want to enable this if $LOG_LEVEL DEBUG or lower
}
"
or
export $Global_block = "
{
}
"
?
If this is correct - then what’s the difference between a Caddyfile that’s:
{$ENV} only works in the Caddyfile (not in JSON) and is replaced at config adapt time i.e. when the Caddyfile is turned into JSON.
{env.ENV} is replaced at runtime, and may not work everywhere in the config (the code must actually run the placeholder replacer for that config value, many do but not all)