respond
Lets the route answer by itself from a template, with the signed-in caller available to it. Two uses: exposing the identity endpoint a hosted application expects in its own shape, and serving a small fixed document - a robots.txt, a public configuration - with no service behind it.
This is a terminal filter: nothing is proxied and no upstream is called.
Parameters
| Name | Type | Required | What it does |
|---|---|---|---|
body |
string | yes | A Go text/template. See below for what it can reach. |
contentType |
string | no | Content-Type of the answer. Default: application/json; charset=utf-8. |
status |
integer | no | HTTP status of the answer. Default: 200. |
Example
filters:
- type: respond
args:
body: '{"name": {{json .Username}}, "authorities": {{json (wrap "authority" .Roles)}}}'
contentType: application/json; charset=utf-8
status: 200
An application asking /user gets {"name":"jsmith","authorities":[{"authority":"BILLING"}]}.
Notes
The caller is available as {{.Username}}, {{.UserID}}, {{.Fullname}}, {{.Email}}, {{.Tenant}}, {{.TenantID}}, {{.Timezone}}, {{.Currency}}, {{.Roles}}, plus {{.SignedIn}}, which is false when nobody is signed in. Nothing else is reachable: no store, no filesystem, no other account.
{{.Timezone}} is the zone the person chose on their profile, and empty when they chose none: their browser's zone then applies, and only the browser knows it.
{{.Currency}} is the person's currency, as an ISO 4217 code such as EUR: the one they chose on their profile, else the one their browser's language points to. It is empty when nobody is signed in, and when the currency preference is switched off for every account in Infra > Model.
Three functions come with it:
jsonrenders a value as JSON, quotes and escaping included.joinflattens a list, e.g.{{join "," .Roles}}.wrapturns a list into one-key objects:{{json (wrap "authority" .Roles)}}gives[{"authority":"A"}], which is the shape half the applications out there expect for roles.
"name": {{json .Username}} and not "name": "{{.Username}}". The second looks right and breaks the day a name holds a quote - and names come from directories, not from you.The template is parsed and run once when the route is saved, against a witness caller: a field that does not exist ({{.Usernme}}) is caught there rather than in production. The console previews the rendered answer as you type, through the same code.
The answer carries Cache-Control: no-store, since what it says depends on who is asking.
The template is taken verbatim: a $ in it is never read as a vault reference.