mirror of
https://gitee.com/iresty/apisix.git
synced 2024-12-05 21:47:36 +08:00
1ac7166e80
Co-authored-by: 琚致远 <juzhiyuan@apache.org>
127 lines
4.6 KiB
Markdown
127 lines
4.6 KiB
Markdown
---
|
|
title: serverless
|
|
---
|
|
|
|
<!--
|
|
#
|
|
# 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.
|
|
#
|
|
-->
|
|
|
|
## Description
|
|
|
|
There are two plug-ins for serverless, namely `serverless-pre-function` and `serverless-post-function`.
|
|
|
|
The former runs at the beginning of the specified phase, while the latter runs at the end of the specified phase.
|
|
|
|
Both plug-ins receive the same parameters.
|
|
|
|
## Attributes
|
|
|
|
| Name | Type | Requirement | Default | Valid | Description |
|
|
| --------- | ------------- | ----------- | ---------- | ------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
| phase | string | optional | ["access"] | ["rewrite", "access", "header_filter", "body_filter", "log", "before_proxy"] | |
|
|
| functions | array[string] | required | | | A list of functions that are specified to run is an array type, which can contain either one function or multiple functions, executed sequentially. |
|
|
|
|
Note that only function is accepted here, not other types of Lua code. For example, anonymous functions are legal:<br />
|
|
|
|
```lua
|
|
return function()
|
|
ngx.log(ngx.ERR, 'one')
|
|
end
|
|
```
|
|
|
|
Closure is also legal:
|
|
|
|
```lua
|
|
local count = 1
|
|
return function()
|
|
count = count + 1
|
|
ngx.say(count)
|
|
end
|
|
```
|
|
|
|
But code that is not a function type is illegal:
|
|
|
|
```lua
|
|
local count = 1
|
|
ngx.say(count)
|
|
```
|
|
|
|
Since `v2.6`, we pass the `conf` and `ctx` as the first two arguments to the servelss function, like a regular plugin.
|
|
|
|
Prior to `v2.12.0`, the phase `before_proxy` used to be called `balancer`. Considering that this method actually runs after `access` and before the request goes upstream, and has nothing to do with `balancer`, the new naming would be more appropriate.
|
|
|
|
## How To Enable
|
|
|
|
Here's an example, enable the serverless plugin on the specified route:
|
|
|
|
```shell
|
|
curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
|
|
{
|
|
"uri": "/index.html",
|
|
"plugins": {
|
|
"serverless-pre-function": {
|
|
"phase": "rewrite",
|
|
"functions" : ["return function() ngx.log(ngx.ERR, \"serverless pre function\"); end"]
|
|
},
|
|
"serverless-post-function": {
|
|
"phase": "rewrite",
|
|
"functions" : ["return function(conf, ctx) ngx.log(ngx.ERR, \"match uri \", ctx.curr_req_matched and ctx.curr_req_matched._path); end"]
|
|
}
|
|
},
|
|
"upstream": {
|
|
"type": "roundrobin",
|
|
"nodes": {
|
|
"127.0.0.1:1980": 1
|
|
}
|
|
}
|
|
}'
|
|
```
|
|
|
|
## Test Plugin
|
|
|
|
Use curl to access:
|
|
|
|
```shell
|
|
curl -i http://127.0.0.1:9080/index.html
|
|
```
|
|
|
|
Then you will find the message 'serverless pre-function' and 'match uri /index.html' in the error.log,
|
|
which indicates that the specified function is in effect.
|
|
|
|
## Disable Plugin
|
|
|
|
When you want to disable the serverless plugin, it is very simple,
|
|
you can delete the corresponding json configuration in the plugin configuration,
|
|
no need to restart the service, it will take effect immediately:
|
|
|
|
```shell
|
|
curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
|
|
{
|
|
"methods": ["GET"],
|
|
"uri": "/index.html",
|
|
"upstream": {
|
|
"type": "roundrobin",
|
|
"nodes": {
|
|
"127.0.0.1:1980": 1
|
|
}
|
|
}
|
|
}'
|
|
```
|
|
|
|
The serverless plugin has been disabled now. It works for other plugins.
|