ajxchapman / ReServ

A set of simple servers (currently HTTP/HTTPS and DNS) which allow configurable and scriptable responses to network requests.
60 stars 6 forks source link

Move route conditions into a new `filter` dict #15

Open ajxchapman opened 3 years ago

ajxchapman commented 3 years ago

The current route definition syntax mixes how route filters are applied, in an HTTP route the method filter is read from the root of the definition:

{
    "protocol" : "http",
    "method" : "HEAD",
    "route" : "^/(.*)",
    "action" : {}
}

Where as in a DNS route, the filters are read in the action object, e.g. the record key below:

{
    "protocol" : "dns",
    "route" : "simple\\.cname\\.{{default_domain}}",
    "action" : {
      "record" : "CNAME",
      "response" : "www.{{default_domain}}"
    }
}

I propose that these filters are moved into a new key filter, which will allow for much more robust filtering. The examples above would become:

{
    "protocol" : "http",
    "route" : "^/(.*)",
    "filter" : {
        "method" : "HEAD"
    },
    "action" : {}
}
{
    "protocol" : "dns",
    "route" : "simple\\.cname\\.{{default_domain}}",
    "filter" : {
        "record" : "CNAME"
    },
    "action" : {
      "response" : "www.{{default_domain}}"
    }
}

An implementation detail of the DNS route would be that the record type would either have to be duplicated in the filter and in the action, or derived from the filter.