update
Browse files
app.lua
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
-- 这是一个单行注释
|
| 2 |
+
|
| 3 |
+
--[[
|
| 4 |
+
这是一个多行注释
|
| 5 |
+
这是一个多行注释
|
| 6 |
+
]]
|
| 7 |
+
local uri = ngx.var.uri
|
| 8 |
+
|
| 9 |
+
local api_version = ''
|
| 10 |
+
local api_pxoxy_pass_url_indicator = ''
|
| 11 |
+
|
| 12 |
+
-- 提取版本号和 proxy_pass url 标识
|
| 13 |
+
if uri == '/' then -- uri为 / ,退出运行
|
| 14 |
+
ngx.exit(ngx.HTTP_OK)
|
| 15 |
+
end
|
| 16 |
+
|
| 17 |
+
local api_version, api_pxoxy_pass_url_indicator = uri:match("/?([^/]+)/?(.*)")
|
| 18 |
+
|
| 19 |
+
ngx.say("api_version: ", api_version)
|
| 20 |
+
ngx.say("api_pxoxy_pass_url_indicator: ", api_pxoxy_pass_url_indicator)
|
| 21 |
+
pass_url = api_pxoxy_pass_url_indicator
|
| 22 |
+
-- local upstream_dict = ngx.shared.upstream_dict
|
| 23 |
+
-- upstream_dict:set("upstream", pass_url)
|
| 24 |
+
pass_url = pass_url:gsub("http/", "http://")
|
| 25 |
+
pass_url = pass_url:gsub("https/", "https://")
|
| 26 |
+
ngx.say("pass_url: ", pass_url)
|
| 27 |
+
|
| 28 |
+
-- 根据版本号加载不同的 Lua 文件
|
| 29 |
+
-- local lua_path = "app_" .. api_version
|
| 30 |
+
-- ngx.say("lua_path: ", lua_path)
|
| 31 |
+
-- require(lua_path)
|
| 32 |
+
local lua_path = "app/" ..api_version .. "/app"
|
| 33 |
+
ngx.say("lua_path: ", lua_path)
|
| 34 |
+
module = require("app/" .. api_version .. "/app") --require 是没问题的
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
-- 检查模块是否加载成功
|
| 38 |
+
if type(module) ~= "table" then
|
| 39 |
+
ngx.say("Error: Failed to load module")
|
| 40 |
+
ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
|
| 41 |
+
end
|
| 42 |
+
|
| 43 |
+
-- 调用模块中的函数
|
| 44 |
+
module.some_function() -- 调用 some_function
|
| 45 |
+
module.another_function() -- 调用 another_function
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
ngx.say("module.a: ", module.a)
|