Caddy JSON config - subroutes, ids and directive order

1. The problem I’m having:

I am using Caddy JSON config format directly, by manipulating it via API.
My Caddy use-case has multiple domains which I would like to group by id on a route level matching by domain on route level and a number of subroutes containting matchers and handlers for different cases.

Questions:
Sometimes I need to add a new subroute, which must be above other subroutes(as the path length based route ordering does not work on JSON config level), is there a way to specify order, or all subroutes must be deleted, and then readded in a correct order?
Is it a good idea to group by domain on a route level to avoid any collisions, when modifying one domain would affect other?

I feel that these questions may be answered by experimenting, still I hope a possible answer would save some time.

2. Error messages and/or full log output:

no errors, only question

3. Caddy version:

docker caddy 2-builder

4. How I installed and ran Caddy:

a. System environment:

Docker

b. Command:

PASTE OVER THIS, BETWEEN THE ``` LINES.
Please use the preview pane to ensure it looks nice.

c. Service/unit/compose file:

PASTE OVER THIS, BETWEEN THE ``` LINES.
Please use the preview pane to ensure it looks nice.

d. My complete Caddy config:

{
  "apps": {
    "http": {
      "servers": {
        "srv0": {
          "routes": [
            {
              "@id": "id_123",
              "match": [
                {
                  "host": ["app.example.com"]
                }
              ],
              "handle": [
                {
                  "handler": "subroute",
                  "routes": [
                    {
                      "@id": "id_123_api",
                      "match": [
                        {
                          "path": ["/api/*"]
                        }
                      ],
                      "handle": [
                        {
                          "handler": "reverse_proxy",
                          "upstreams": [
                            {
                              "dial": "backend:8080"
                            }
                          ]
                        }
                      ],
                      "terminal": true
                    },
                    {
                      "@id": "id_123_app",
                      "handle": [
                        {
                          "handler": "file_server",
                          "root": "/srv/123/app"
                        }
                      ]
                    }
                  ],
                  "errors": {
                    "routes": [
                      {
                        "handle": [
                          {
                            "handler": "static_response",
                            "status_code": 500,
                            "body": "Server Error"
                          }
                        ]
                      }
                    ]
                  }
                }
              ]
            }
          ]
        }
      }
    }
  }
}

5. Links to relevant resources:

If you’re pre-pending, use PUT with the path traversal ends with the index at which you want the insert to start. So if you’re prepending to the beginning of the array, use PUT and traversal of index /0. Link to the PUT docs:

I guess? It depends on your strict routing and matching criteria. If domain name is your reference, then, yes, use domain name grouping. I’ve touched on this aspect here:

1 Like

Thanks, is it a good idea to always add index when adding routes in a sequence to avoid race condition?