serverless-post-function
Runs Lua functions after the other plugins of a phase.
serverless-post-functionPriority -2000What it does
serverless-post-function runs Lua code written on the route itself. Each box under Functions holds a chunk that returns a function, and Phase says in which phase the gateway calls it, access unless you choose another. Within that phase it runs after the route's other plugins.
The phase decides what the code acts on. In rewrite, access and before_proxy it runs before the backend is called, in header_filter and body_filter on the answer, and in log once the answer is sent. How G8 handles a request explains phases and priority.
When to use it
- Every answer of the orders API should carry a header that names the gateway, written once the other plugins of the route are done with the answer.
- A value the other plugins of the route have worked out, read by your own code after they ran.
- A small piece of logic tried out on one route before anyone writes a plugin for it.
Not for changing the status, headers or body of the answer without code — see response-rewrite (Rewrites the response status, headers and body), code that runs before the other plugins of a phase — see serverless-pre-function (Runs Lua functions before the other plugins of a phase) or answering with a made-up response and no backend at all — see mocking (Answers requests with a mocked response instead of an upstream).
Set it up
Add a response header with a few lines of Lua once the other plugins have run
Fill Functions as in the JSON below and set Phase to header_filter.
{
"functions": [
"return function(conf, ctx)\n ngx.header[\"X-Served-By\"] = \"gateway\"\nend"
],
"phase": "header_filter"
}
Things to know
- Functions — Compiled and run when the object is checked: a chunk that fails to compile, fails when called or returns no function is refused, as is a $secret:// or $env:// reference. A refused chunk has still run once on the gateway.
- Functions — A function in the
header_filterphase can set a header on the answer: with the example in place, a request is answered200carryingX-Served-Bywithgateway. - Phase — In
rewrite,accessandbefore_proxya function that returns a status answers the request itself, with or without a body. Inheader_filter,body_filterandlogwhat it returns is thrown away. - AdviceTry a new chunk on a route of its own before you add it to a route your clients call.
Settings
The fields as the console shows them, section by section.
functionsarray of stringsRequiredEach box is a Lua chunk returning function(conf, ctx), such as return function(conf, ctx) ngx.log(ngx.INFO, "hi") end. In rewrite, access and before_proxy a function that returns a status answers the request itself, with or without a body. In header_filter, body_filter and log what it returns is thrown away.
["return function(conf, ctx)\n ngx.log(ngx.INFO, \"hello from the gateway\")\nend"]phasestringdefault accessWhen the functions run: rewrite, access or before_proxy before the upstream is called, header_filter or body_filter on the answer, log after it is sent.