File size: 1,365 Bytes
f23aede
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
-- 这是一个单行注释

--[[
这是一个多行注释
这是一个多行注释
]]
local uri = ngx.var.uri

local api_version = ''
local api_pxoxy_pass_url_indicator = ''

-- 提取版本号和 proxy_pass url 标识
if uri == '/' then -- uri为 / ,退出运行
  ngx.exit(ngx.HTTP_OK)
end

local api_version, api_pxoxy_pass_url_indicator = uri:match("/?([^/]+)/?(.*)")

ngx.say("api_version: ", api_version)
ngx.say("api_pxoxy_pass_url_indicator: ", api_pxoxy_pass_url_indicator)
pass_url = api_pxoxy_pass_url_indicator
-- local upstream_dict = ngx.shared.upstream_dict
-- upstream_dict:set("upstream", pass_url)
pass_url = pass_url:gsub("http/", "http://")
pass_url = pass_url:gsub("https/", "https://")
ngx.say("pass_url: ", pass_url)

-- 根据版本号加载不同的 Lua 文件
-- local lua_path = "app_" .. api_version
-- ngx.say("lua_path: ", lua_path)
-- require(lua_path)
local lua_path = "app/" ..api_version .. "/app"
ngx.say("lua_path: ", lua_path)
module = require("app/" .. api_version .. "/app") --require 是没问题的


-- 检查模块是否加载成功
if type(module) ~= "table" then
  ngx.say("Error: Failed to load module")
  ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
end

-- 调用模块中的函数
module.some_function()  -- 调用 some_function
module.another_function()  -- 调用 another_function


ngx.say("module.a: ", module.a)