Skip to main content

Version: 2.13.2304

Create API without Upstream

Binding upstream for APIs is mandatory according to the API7EE restrictions. However, based on the principle of our gateway, we can execute the logic and terminate the forwarding process before the request is actually forwarded upstream.

In APISIX, on the other hand, upstream is relatively more flexible, and an upstream-free route is perfectly fine. However, its flexibility may bring troubles in usage, so this possibility is forbidden in API7 EE.

Prepare environment

Please refer to API7 EE Introduction to complete the environment preparation.

Configure the serverless plugin

We use the serverless-pre-function plugin here to inject some custom Lua code to achieve the effect we need.

Create a plugin template with the serverless-pre-function plugin enabled with the following configuration as described in API7 EE Introduction.

{
"phase": "access",
"functions": [
"return function(conf, ctx) local core = require('apisix.core'); local case = tostring(ctx.var.arg_case or 000); if case == '100' then core.response.exit(403, 'this is case 100'); elseif case == '101' then local httpc = require('resty.http').new(); httpc:set_timeout(conf.timeout); local res, err = httpc:request_uri('https://luarocks.org', {ssl_verify=false}); core.response.exit(not res and 500 or 200, res.body or err); else core.response.exit(200, 'this is default case'); end; end"
]
}
return function(conf, ctx)
local core = require('apisix.core')
local case = tostring(ctx.var.arg_case or 000)
if case == '100' then
core.response.exit(403, 'this is case 100')
elseif case == '101' then
local httpc = require('resty.http').new()
httpc:set_timeout(conf.timeout)
local res, err = httpc:request_uri('https://luarocks.org', {ssl_verify=false})
core.response.exit(not res and 500 or 200, res.body or err)
else
core.response.exit(200, 'this is default case')
end
end

Test

default case

curl 127.0.0.1/anything -i -H "Host: example.com"
HTTP/1.1 200 OK
Date: Sun, 19 Mar 2023 05:20:20 GMT
Content-Type: text/plain; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Server: APISIX/2.13.2304

this is default case

case 100 (return)

curl 127.0.0.1/anything?case=100 -i -H "Host: example.com"
HTTP/1.1 403 Forbidden
Date: Sun, 19 Mar 2023 05:31:42 GMT
Content-Type: text/plain; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Server: APISIX/2.13.2304

this is case 100

case 101 (HTTP Client)

curl 127.0.0.1/anything?case=101 -i -H "Host: example.com"
HTTP/1.1 200 OK
Date: Sun, 19 Mar 2023 05:32:53 GMT
Content-Type: text/plain; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Server: APISIX/2.13.2304

<luarocks.org homepage source code>

API7.ai Logo

API Management for Modern Architectures with Edge, API Gateway, Kubernetes, and Service Mesh.

Product

API7 Cloud

SOC2 Type IIISO 27001HIPAAGDPRRed Herring

Copyright © APISEVEN Ltd. 2019 – 2024. Apache, Apache APISIX, APISIX, and associated open source project names are trademarks of the

Apache Software Foundation