Route templates?

1. Output of caddy version:

v2.4.6

2. How I run Caddy:

JSON config

a. System environment:

Ubuntu 20.04.3 LTS

b. Command:

service caddy start

c. Service/unit/compose file:

[Service]
ExecStart=
ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/caddy.json
ExecReload=
ExecReload=/usr/bin/caddy reload --config /etc/caddy/caddy.json

3. The problem I’m having:

We use JSON for our Caddy config, and there are hundreds of routes that are identical except for a couple of variables.

Here is an example route for one website:


{
                  "@id": "custom-domain_pactennisacademy.com_pactennisacademy",
                  "handle": [
                    {
                      "handler": "subroute",
                      "routes": [
                        {
                          "handle": [
                            {
                              "handler": "rewrite",
                              "uri": "?{http.request.uri.query}&e=pactennisacademy"
                            },
                            {
                              "handler": "reverse_proxy",
                              "upstreams": [
                                {
                                  "dial": "localhost:8315"
                                }
                              ]
                            }
                          ],
                          "match": [
                            {
                              "host": [
                                "www.pactennisacademy.com"
                              ]
                            }
                          ]
                        },
                        {
                          "handle": [
                            {
                              "handler": "static_response",
                              "headers": {
                                "Location": [
                                  "https://www.{http.request.host}{http.request.uri}"
                                ]
                              },
                              "status_code": 302
                            }
                          ],
                          "match": [
                            {
                              "host": [
                                "pactennisacademy.com"
                              ]
                            }
                          ]
                        }
                      ]
                    }
                  ],
                  "match": [
                    {
                      "host": [
                        "pactennisacademy.com",
                        "www.pactennisacademy.com"
                      ]
                    }
                  ],
                  "terminal": true
                },

Does Caddy provide any way to “template” a route, so we could just have one route, and then an array of variables?

It seems like this would make our Caddy config smaller and easier to read.

5. What I already tried:

Searching documentation and internet

You’re using a pretty old version at this point. Please upgrade to the latest, v2.6.2.

Not currently, no.

The Caddyfile has “snippets” which do have arguments support.

But I do have a proposal for “named routes” Reusable named routes · Issue #4995 · caddyserver/caddy · GitHub which would probably do what you want. I have an implementation in this PR:

Feel free to give your +1 to that PR or issue to explain your usecase.

Thanks @francislavoie!

1 Like