Caddy: Conditional global directive

Created on 1 Sep 2020  路  4Comments  路  Source: caddyserver/caddy

Hello. Thank you so much for developing and supporting Caddy. It's a great piece of software.

I have two Caddyfiles - one for production and one for development environment. Thanks to placeholders I've been able to unify them almost completely - the only difference is that in development I want the debug directive and in production I don't. I'd like to have one file to avoid introducing accidental differences. Is there a mechanism to enable or disable the debugging? I suppose this question can be generalized to all global directives.

I tried to import production Caddyfile after the development specific directives, like this:

# Caddyfile.development

{
    debug
}

import Caddyflile # the common part

It works in principle, but then watch doesn't pick up changes in the main Caddyfile.

I also tried loading two Caddyfiles from command line but it doesn't seem to work.

Is there a way to achieve make debug global directive conditional?

If there isn't, perhaps simplest way to get what I want would be to allow setting debug flag from command line. Alternatively make watch traverse imported files. There is an open issue about watching with import * (#3517) but for this use case it's not required.

invalid

Most helpful comment

The solution suggested by @francislavoie works very well for me. Happily closing the issue. Thanks!

All 4 comments

So it sounds like this does actually work, but it's just that --watch doesn't pick up the changes the way you want? Can you elaborate on that?

What you could do is use an Caddyfile-style environment variable that you fill with the value debug, and leave it empty otherwise. This should do what you want.

It would look something like this:

{
    {$CADDY_DEBUG}
}

example.com {
    reverse_proxy 127.0.0.1:8080
}

Thank you, @francislavoie! That's very smart.

@mholt thanks for getting back to me. The issue with --watch is that with a Caddyfile like the one I shared, here again for convenience:

# Caddyfile.development

{
    debug
}

import Caddyflile # the common part

and the imported one like this:

# Caddyfile

example.com {
    reverse_proxy 127.0.0.1:8080
}

If I run caddy run --watch --config Caddyfile.development and then change the imported Caddyfile, the config won't be automatically reloaded. Since most of the logic is in Caddyfile, and Caddyfile.development is just a thin wrapper, it's more important for the first one to be watched than the second. But if the solution suggested by @francislavoie works (I'll check later today) then it will become a very minor issue for me.

The solution suggested by @francislavoie works very well for me. Happily closing the issue. Thanks!

Was this page helpful?
0 / 5 - 0 ratings