Rest api and @id usage

I have created my base configuration using Caddyfile. For production I will convert the file to json as I will be managing the config file via the the rest api. In this snippet I have a route that uses a map to do the reverse proxy.

            {
              "match": [{ "host": ["*.foundryserver.ca"] }],
              "handle": [
                {
                  "handler": "subroute",
                  "routes": [
                    {
                      "handle": [
                        {
                          "destinations": ["{backend_ip}"],
                          "handler": "map",
                          "mappings": [{ "input": "example.foundryserver.ca", "outputs": ["192.168.255.200"] }],
                          "source": "{http.request.host}"
                        },
                        {
                          "handler": "reverse_proxy",
                          "stream_close_delay": 28800000000000,
                          "upstreams": [{ "dial": "{http.request.host}:8080" }, { "dial": "{backend_ip}:30000" }]
                        }
                      ]
                    }
                  ]
                }
              ],
              "terminal": true
            }

Can I put a @id on the match and the handle? Like so

            @somematch {
              "match": [{ "host": ["*.foundryserver.ca"] }],
              "handle": [
                {
                  "handler": "subroute",
                  "routes": [
                    @somehandle {
                      "handle": [
                        {
                          "destinations": ["{backend_ip}"],
                          "handler": "map",
                          "mappings": [{ "input": "example.foundryserver.ca", "outputs": ["192.168.255.200"] }],
                          "source": "{http.request.host}"
                        },
                        {
                          "handler": "reverse_proxy",
                          "stream_close_delay": 28800000000000,
                          "upstreams": [{ "dial": "{http.request.host}:8080" }, { "dial": "{backend_ip}:30000" }]
                        }
                      ]
                    }
                  ]
                }
              ],
              "terminal": true
            }

then to reference the path via the rest api I would do something like:

curl -X DELETE "http://localhost:2019/id/somematch/handle/routes/id/somehandle

The @id is actually a JSON field with a value of the ID you’ve chosen. You’ll have something like "@id": "myID". I’ve covered the use of the @id fields and the API is this article.

Ok, I think I got it. I will have 3 domains that could have map values. So the intent of the following block is to create an @id for each domain, and then an @id for each map. When adding a map, I would use the domain @id to locate the correct domain block. When removing the map, I would reference the @id for the specific map, provided that the map name was unique. Have I got this correct then?

           {
              "match": [{ "host": ["*.foundryserver.ca"] }],
              "handle": [
                {
                  "handler": "subroute",
                  "routes": [
                    {
                      "handle": [
                        {
                          "@id": "foundryserver.ca", 
                          "destinations": ["{backend_ip}"],
                          "handler": "map",
                          "mappings": [
                            { "@id": "example.foundryserver.ca", "input": "example.foundryserver.ca", "outputs": ["192.168.255.200"] }
                          ],
                          "source": "{http.request.host}"
                        },
                        {
                          "handler": "reverse_proxy",
                          "stream_close_delay": 28800000000000,
                          "upstreams": [{ "dial": "{http.request.host}:8080" }, { "dial": "{backend_ip}:30000" }]
                        }
                      ]
                    }
                  ]
                }
              ],
              "terminal": true
            }

to add a map value:

curl
-H “Content-Type: application/json”
-d “@id=example.foundryserver.ca&input=example.foundryserver.ca&outputs=192.168.255.200”
“http://localhost:2019/id/foundyrserver.ca/mappings”

to remove a value:

curl -X DELETE “http://localhost:2019/id/example.foundryserver.ca”

Here was the correct path for post

http://12.16.25.42:4664/id/foundryserver.ca/handle/0/mappings

Here was the payload

{
    "@id": "test",
    "input": "test.example.com",
    "outputs": ["1.1.1.1"]
}

To delete the entry

curl --location --request DELETE '192.168.255.242:4334/id/test' \ --header 'Content-Type: application/json' \'

Hope this can help someone else with constructing an api request.

1 Like