-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: support body filter plugin
echo
. (#1632)
- Loading branch information
Ayeshmantha Perera
authored
Jun 13, 2020
1 parent
56aeb4a
commit 1764890
Showing
7 changed files
with
478 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
-- | ||
-- Licensed to the Apache Software Foundation (ASF) under one or more | ||
-- contributor license agreements. See the NOTICE file distributed with | ||
-- this work for additional information regarding copyright ownership. | ||
-- The ASF licenses this file to You under the Apache License, Version 2.0 | ||
-- (the "License"); you may not use this file except in compliance with | ||
-- the License. You may obtain a copy of the License at | ||
-- | ||
-- http://www.apache.org/licenses/LICENSE-2.0 | ||
-- | ||
-- Unless required by applicable law or agreed to in writing, software | ||
-- distributed under the License is distributed on an "AS IS" BASIS, | ||
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
-- See the License for the specific language governing permissions and | ||
-- limitations under the License. | ||
-- | ||
local core = require("apisix.core") | ||
local ngx = ngx | ||
|
||
local schema = { | ||
type = "object", | ||
properties = { | ||
before_body = { | ||
description = "body before the filter phase.", | ||
type = "string" | ||
}, | ||
body = { | ||
description = "body to replace upstream response.", | ||
type = "string" | ||
}, | ||
after_body = { | ||
description = "body after the modification of filter phase.", | ||
type = "string" | ||
} | ||
}, | ||
anyOf = { | ||
{required = {"before_body"}}, | ||
{required = {"body"}}, | ||
{required = {"after_body"}} | ||
}, | ||
minProperties = 1 | ||
} | ||
|
||
local plugin_name = "echo" | ||
|
||
local _M = { | ||
version = 0.1, | ||
priority = 412, | ||
name = plugin_name, | ||
schema = schema, | ||
} | ||
|
||
|
||
function _M.check_schema(conf) | ||
return core.schema.check(schema, conf) | ||
end | ||
|
||
function _M.body_filter(conf, ctx) | ||
if conf.body then | ||
ngx.arg[1] = conf.body | ||
end | ||
|
||
if conf.before_body then | ||
ngx.arg[1] = conf.before_body .. ngx.arg[1] | ||
end | ||
|
||
if conf.after_body then | ||
ngx.arg[1] = ngx.arg[1] .. conf.after_body | ||
end | ||
ngx.arg[2] = true | ||
end | ||
|
||
return _M |
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 |
---|---|---|
|
@@ -166,6 +166,7 @@ plugins: # plugin list | |
- batch-requests | ||
- http-logger | ||
- skywalking | ||
- echo | ||
|
||
stream_plugins: | ||
- mqtt-proxy |
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,98 @@ | ||
<!-- | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one or more | ||
# contributor license agreements. See the NOTICE file distributed with | ||
# this work for additional information regarding copyright ownership. | ||
# The ASF licenses this file to You under the Apache License, Version 2.0 | ||
# (the "License"); you may not use this file except in compliance with | ||
# the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
--> | ||
|
||
# 目录 | ||
- [**简介**](#简介) | ||
- [**属性**](#属性) | ||
- [**如何启用**](#如何启用) | ||
- [**测试插件**](#测试插件) | ||
- [**禁用插件**](#禁用插件) | ||
|
||
## 简介 | ||
|
||
echo是一个有用的插件,可帮助用户尽可能全面地了解如何开发APISIX插件。 | ||
|
||
|
||
该插件解决了常见阶段中的相应功能,例如初始化,重写,访问,平衡器 | ||
,标题文件管理器,正文过滤器和日志。 | ||
|
||
## 属性 | ||
|
||
|属性名称 |必选项 |描述| | ||
|--------- |--------|-----------| | ||
| before_body |可选| 主体在过滤阶段之前。。| | ||
| body |可选| 身体替代上游反应。| | ||
| after_body |可选| 车身经过改装后的滤光相。| | ||
|
||
## 如何启用 | ||
|
||
1. 为特定路由启用 echo 插件。 | ||
|
||
```shell | ||
curl http://127.0.0.1:9080/apisix/admin/routes/5 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' | ||
{ | ||
"plugins": { | ||
"kafka-logger": { | ||
"broker_list" : | ||
{ | ||
"127.0.0.1":9092 | ||
}, | ||
"kafka_topic" : "test2", | ||
"key" : "key1" | ||
} | ||
}, | ||
"upstream": { | ||
"nodes": { | ||
"127.0.0.1:1980": 1 | ||
}, | ||
"type": "roundrobin" | ||
}, | ||
"uri": "/hello" | ||
}' | ||
``` | ||
|
||
## 测试插件 | ||
|
||
* 成功: | ||
|
||
```shell | ||
$ curl -i http://127.0.0.1:9080/hello | ||
HTTP/1.1 200 OK | ||
... | ||
hello, world | ||
``` | ||
|
||
## 禁用插件 | ||
|
||
当您要禁用`echo`插件时,这很简单,您可以在插件配置中删除相应的json配置,无需重新启动服务,它将立即生效: | ||
|
||
```shell | ||
$ curl http://127.0.0.1:2379/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d value=' | ||
{ | ||
"methods": ["GET"], | ||
"uri": "/hello", | ||
"plugins": {}, | ||
"upstream": { | ||
"type": "roundrobin", | ||
"nodes": { | ||
"127.0.0.1:1980": 1 | ||
} | ||
} | ||
}' | ||
``` |
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,95 @@ | ||
<!-- | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one or more | ||
# contributor license agreements. See the NOTICE file distributed with | ||
# this work for additional information regarding copyright ownership. | ||
# The ASF licenses this file to You under the Apache License, Version 2.0 | ||
# (the "License"); you may not use this file except in compliance with | ||
# the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
--> | ||
|
||
# Summary | ||
- [**Name**](#name) | ||
- [**Attributes**](#attributes) | ||
- [**How To Enable**](#how-to-enable) | ||
- [**Test Plugin**](#test-plugin) | ||
- [**Disable Plugin**](#disable-plugin) | ||
|
||
|
||
## Name | ||
|
||
`echo` is a a useful plugin to help users understand as fully as possible how to develop an APISIX plugin. | ||
|
||
This plugin addresses the corresponding functionality in the common phases such as init, rewrite, access, balancer | ||
, header filer, body filter and log. | ||
|
||
## Attributes | ||
|
||
|Name |Requirement |Description| | ||
|--------- |-------- |-----------| | ||
| before_body |optional | Body before the filter phase.| | ||
| body |optional | Body to replace upstream response.| | ||
| after_body |optional |Body after the modification of filter phase.| | ||
|
||
|
||
## How To Enable | ||
|
||
The following is an example on how to enable the echo plugin for a specific route. | ||
|
||
```shell | ||
curl http://127.0.0.1:9080/apisix/admin/routes/5 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' | ||
{ | ||
"plugins": { | ||
"echo": { | ||
"before_body": "before the body modification " | ||
} | ||
}, | ||
"upstream": { | ||
"nodes": { | ||
"127.0.0.1:1980": 1 | ||
}, | ||
"type": "roundrobin" | ||
}, | ||
"uri": "/hello" | ||
}' | ||
``` | ||
|
||
## Test Plugin | ||
|
||
* success: | ||
|
||
```shell | ||
$ curl -i http://127.0.0.1:9080/hello | ||
HTTP/1.1 200 OK | ||
... | ||
before the body modification hello world | ||
``` | ||
|
||
## Disable Plugin | ||
|
||
Remove the corresponding json configuration in the plugin configuration to disable the `echo`. | ||
APISIX plugins are hot-reloaded, therefore no need to restart APISIX. | ||
|
||
```shell | ||
$ curl http://127.0.0.1:2379/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d value=' | ||
{ | ||
"methods": ["GET"], | ||
"uri": "/hello", | ||
"plugins": {}, | ||
"upstream": { | ||
"type": "roundrobin", | ||
"nodes": { | ||
"127.0.0.1:1980": 1 | ||
} | ||
} | ||
}' | ||
``` |
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
Oops, something went wrong.