If else logic inside Caddyfile

Hi,

I would like to have an if else statement based on environment variables and files.

Caddy version 2 on Docker (alpine)

So the code I’d like to be something like this:

if {env.CADDY_CONFIG_FILE} is "something1" {
   http://, https://
   tls internal {
     on_demand
    }
} elif {env.CADDY_CONFIG_FILE} is "something2" {
  http://
} elif {env.CADDY_CONFIG_FILE} is "somethinge" {
  {{if eq . "f1.key"}} [how can an add an and here ... as i'd like to test if f1.pem file exists as well]
     https://
     tls f1.key f1.pem
  {{else}}
    http://, https://
    tls internal {
      on_demand
     }
  {{end}}
}

Is this possible?
How can I add an if "1" AND "2" then as I'd like to check if f1 pem and key exists?

Regards,

NOTE: I know I answered that in past as well, but I don’t find any library for this type of thing. Can someone please direct me?

There’s two ways your question could be interpreted.

Are you looking for dynamic configuration, e.g. Caddy’s behaviour changes each time you change the environmental variable? In that case, no, you’d need to use the API for that, e.g. at the same time as you change the variable.

Are you looking for Caddy’s initial configuration to be dependent on starting environmental variables? In that case, yes, to an extent, but you can’t if/else the blocks like this; you should instead look into configuration templating, with tools such as:

3 Likes

This topic was automatically closed after 30 days. New replies are no longer allowed.