-
Notifications
You must be signed in to change notification settings - Fork 2.7k
feat: adds support for nginx variables in service_name param #12187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 12 commits
90b3c13
9def4fb
cfea8d8
3b90fe2
ac76dd7
2160c51
e8e086c
5a907fa
ab7bc5d
3a623bc
5b457df
b661517
5f19d54
7548cf8
b669e78
e8b18c1
7d411d3
8a14a9e
9c58805
dbb795a
54d12c9
3b5782d
c6da5d1
100fa55
b688b39
2c3d085
6fdd1ec
641fe78
e54929a
6f21de0
94920a4
e7f886c
fdcd511
3cd47e5
a7ca200
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -337,6 +337,24 @@ do | |
end | ||
end | ||
-- Resolve ngx.var in the given string | ||
-- @function core.utils.resolve_var | ||
-- @tparam string tpl The template string to resolve variables in | ||
-- @tparam table ctx The context table containing variables | ||
-- @tparam function escaper Optional function to escape resolved values | ||
-- @treturn string The resolved string | ||
-- @treturn string|nil Error message if any | ||
-- @treturn number Number of variables replaced | ||
-- @usage | ||
-- local utils = require("apisix.core.utils") | ||
-- | ||
-- -- Usage examples: | ||
-- local res = utils.resolve_var("$host", ctx.var) -- "example.com" | ||
-- local res = utils.resolve_var("${host}", ctx.var) -- "example.com" | ||
-- local res = utils.resolve_var("TMP_${VAR1}_${VAR2}", ctx.var) -- "TMP_value1_value2" | ||
-- local res = utils.resolve_var("\\$host", ctx.var) -- "$host" | ||
|
||
-- | ||
-- -- Usage in APISIX context: | ||
-- local service_name = utils.resolve_var(up_conf.service_name, api_ctx.var) | ||
undying marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
_M.resolve_var = resolve_var | ||
|
||
|
||
|
@@ -461,5 +479,4 @@ function _M.check_tls_bool(fields, conf, plugin_name) | |
end | ||
end | ||
|
||
|
||
Baoyuantop marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return _M |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -297,7 +297,12 @@ function _M.set_by_route(route, api_ctx) | |
return 503, err | ||
end | ||
|
||
local new_nodes, err = dis.nodes(up_conf.service_name, up_conf.discovery_args) | ||
local service_name, err = core.utils.resolve_var(up_conf.service_name, api_ctx.var) | ||
undying marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if not service_name or service_name == "" then | ||
return 503, "service_name is empty: " .. (err or "nil") | ||
undying marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
end | ||
|
||
local new_nodes, err = dis.nodes(service_name, up_conf.discovery_args) | ||
if not new_nodes then | ||
return HTTP_CODE_UPSTREAM_UNAVAILABLE, "no valid upstream node: " .. (err or "nil") | ||
|
||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -393,3 +393,5 @@ res:nil | |
res:5 | ||
res:12 | ||
res:7 | ||
|
||
|
||
undying marked this conversation as resolved.
Show resolved
Hide resolved
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these comments necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added comments from the approach in order to generate documentation for the methods later.
If there are no such plans, I can remove it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can keep it, but we need to make sure it's correct.