Skip to content

Discussion around using variables in Caddyfile #4650

Description

@brucejo75

I was reading the variable assignment thread in the forum essentially outlining that you could assign a placeholder using map or import. The author of the post specifically asked about being able to assign non-environmental variables (e.g. placeholders).

It occurred to me that "Environment Variables" could be more generalized to "Variables" if you require a declaration before usage...

I would like to do something like this:

:55555 {
{$MY_BASE_SITE=env.MYBASESITE | mysite.com}
  map {host} {port} {
    "a.{$MY_BASE_SITE}" 1111
    "b.{$MY_BASE_SITE}" 2222
  }

  reverse_proxy 0.0.0.0:{port}
}

I can make this work by assigning an environment variable before running caddy, but oftentimes it would be useful to declare in the Caddyfile (e.g. use of more than a few variables).

Feature Request

  • Create a new variable type, modelled after Environment Variables with the requirement that they need to be explicitly declared.
# Always need to declare variables before using them
# set to DEFAULTVALUE unless env.VARIABLE exists
{$VARIABLE=env.VARIABLE | DEFAULTVALUE}

# subsequent references will be equal to whatever was assigned e.g. will be DEFAULTVALUE
# if env.VARIABLE is not defined
respond {$VARIABLE}

# and a default override in place could still be used in the reference
respond {$VARIABLE:foobar}

# if env.VARIABLE2 does not exist VARIABLE2 will be empty
{$VARIABLE2= env.VARIABLE2}

#invalid because there is no declaration e.g. {$VARIABLE3=...}
{$VARIABLE3}

Metadata

Metadata

Assignees

No one assigned

    Labels

    discussion 💬The right solution needs to be found

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions