Use a for loop in a Caddyfile to generate directive?

1. The problem I’m having:

I’ve got a Caddyfile with a snippet like below; I’m trying to see if there’s a way to ‘generate’ the import statements based on a directory with files on my server.

So I have list of files in /data/mta-sts/. Could Caddy somehow ‘scan’ those files and add the import statement based on the filename?

The workaround is to just add these files with import and make the content of the file the import statement. But these files have a different content and I want to see if there’s a way to generate just the import statements based on the files existing on the filesystem.

Another workaround could be a little (Bash) script which generates the Caddyfile content with the import statements and then import that in the original Caddyfile. But again, maybe there’s a way to do this all in Caddy?

2. Error messages and/or full log output:

n/a

3. Caddy version:

v2.8.4 h1:q3pe0wpBj1OcHFZ3n/1nl4V4bxBrYoSoab7rL9BMYNk=

4. How I installed and ran Caddy:

Docker compose with a slightly modified Dockerfile from the original Dockerfile

a. System environment:

Docker on Debian arm64

b. Command:

d. My complete Caddy config:

(mta-sts) {
        mta-sts.{args[0]} {
                route {
                        handle_path /.well-known/mta-sts.txt {
                                header Content-Type text/plain
                                root * /var/www/mta-sts/{args[0]}.txt
                                file_server
                        }
                        redir https://{args[0]}
                }
        }
}

include mta-sts example1.com
include mta-sts example2.com
include mta-sts example3.com

2. Error messages and/or full log output:

n/a

3. Caddy version:

v2.8.4 h1:q3pe0wpBj1OcHFZ3n/1nl4V4bxBrYoSoab7rL9BMYNk=

4. How I installed and ran Caddy:

Docker compose with a slightly modified Dockerfile from the original Dockerfile

a. System environment:

Docker on Debian arm64

You can import all files in a directory with import path/to/dir/* but you still need valid Caddyfile config on disk for it to read from. Using a bash script to produce what you need is probably the way to go.

2 Likes

Thanks Francis!