Hi everyone! I recently migrated my infrastructure to Caddy. I love the simplicity of the config. But while migrating, I realised how large my Caddyfile had become.
I decided to make Stretchyfile. Its goal is to improve how variables work in Caddy. I’ve open-sourced it, so hopefully others can use it if they find it useful.
Here is an example of how it can simplify your config:
This is an example scenario that I feel will apply to many people.
- Using 2 mirror domains that are identical
- Using port 80 and 443
- Need subdomains to point to same server
Here is how I had it in my Caddyfile:
example.com:80, example.com:443, *.example.com:80, *.example.com:443, example.org:80, example.org:443, *.example.org:80, *.example.org:443 {
respond "Hello, world!"
}
At least for me, this is very hard to keep track of, especially considering I primarily edit my Caddyfile using CLI, and not a code editor. I sometimes missed a combination of port and domain for example.
Here is how you can implement the same rules with Stretchyfile:
$domains = example.com,example.org,*.example.com,*.example.org
$ports = 80,443
${domains}:{ports} {
respond "Hello, world!"
}
There are many combinations you can do. For example, if you have many domains or subdomains, maybe specifying the subdomains for each domain is too much for you. You can do this instead:
$domains = example.com,example.org
$domains = {domains},*.{domains}
$ports = 80,443
${domains}:{ports} {
respond "Hello, world!"
}
I hope some of you find this useful. Please let me know what you think. More examples and documentation are available on GitHub.
It’s worth noting, I have tested this for my use-cases. If you like the idea of Stretchyfile, but it doesn’t work for your use-case, feel free to improve on it and send a PR. It’s only 80 lines of Python.