-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #496 from 3scale/configure-policy-chain
ability to configure policy chains globally
- Loading branch information
Showing
6 changed files
with
69 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Configure Policy Chain | ||
|
||
Environment configuration can define the global policy chain. You can provide custom chain or insert policies to the default one. | ||
|
||
## Using Echo Policy | ||
|
||
[Echo policy](../../gateway/src/apicast/policy/echo.lua) accepts configuration option to terminate the request phase. See the example in [`configuration.lua`](./configuration.lua). | ||
|
||
You can start it as: | ||
|
||
```shell | ||
ECHO_STATUS=202 bin/apicast --environment examples/policy_chain/configuration.lua | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
local resty_env = require('resty.env') | ||
local policy_chain = require('apicast.policy_chain').default() | ||
local echo_policy = require('apicast.policy.echo').new({ | ||
status = tonumber(resty_env.value('ECHO_STATUS') or 201), | ||
exit = 'request' | ||
}) | ||
|
||
-- add Echo policy to the chain on the 1st place | ||
policy_chain:insert(echo_policy, 1) | ||
|
||
return { | ||
policy_chain = policy_chain | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,36 @@ | ||
local policy = require('apicast.policy') | ||
local _M = policy.new('Echo Policy') | ||
--- Echo policy | ||
-- Print the request back to the client and optionally set a status code. | ||
-- Also can interrupt the execution and skip the current phase or | ||
-- the whole processing of the request. | ||
|
||
local _M = require('apicast.policy').new('Echo Policy') | ||
|
||
local tonumber = tonumber | ||
local new = _M.new | ||
|
||
function _M.new(configuration) | ||
local policy = new(configuration) | ||
|
||
if configuration then | ||
policy.status = tonumber(configuration.status) | ||
policy.exit = configuration.exit | ||
end | ||
|
||
return policy | ||
end | ||
|
||
function _M:rewrite() | ||
if self.status then | ||
ngx.status = self.status | ||
end | ||
|
||
function _M.rewrite() | ||
ngx.say(ngx.var.request) | ||
ngx.exit(0) | ||
|
||
if self.exit == 'request' then | ||
return ngx.exit(ngx.status) | ||
elseif self.exit == 'phase' then | ||
return ngx.exit(0) | ||
end | ||
end | ||
|
||
return _M |