coupergateway / couper

Couper is a lightweight API gateway designed to support developers in building and operating API-driven Web projects
https://couper.io
MIT License
85 stars 15 forks source link

Local variables #696

Open johakoch opened 1 year ago

johakoch commented 1 year ago

Provide a way to use locally defined variables

Example:

proxy {
  ...
  vars {
    expires_header = backend_responses.default.headers.expires
    http_date_format = "%a, %e %b %Y %T GMT"
  }
  add_response_headers = {
    # parse_date() is not (yet) a supported function
    Cache-Control = "max-age=${parse_date(http_date_format, expires_header) - unixtime()}" 
  }
}
# here: expires_header == null
johakoch commented 1 year ago

Attributes in a block are defined by Couper's HCL flavour; in contrast to keys in an object type value (like add_response_headers), which are free to choose by the config writer.

So, if there should be a block, which can potentially appear anywhere in the config, the block must have an attribute with an object type value, like

proxy {
  declare {
    vars = {
      expires_header = backend_responses.default.headers.expires
      http_date_format = "%a, %e %b %Y %T GMT"
    }
  }
  ...
}

However, this may look quite verbose.