diff options
author | zhouruiyu <zhouruiyu@huawei.com> | 2016-09-26 15:07:35 +0800 |
---|---|---|
committer | zhouruiyu <zhouruiyu@huawei.com> | 2016-09-26 15:07:35 +0800 |
commit | b1d981e6b826982707623c5abadd0d341735aec2 (patch) | |
tree | 514bf9e24932e86ceccf70c6a2ed781d82a849e3 /msb-core/openresty-ext | |
parent | 91973919a40c6325daf2cdb5e574cfc1a0ec73c3 (diff) |
add the driver lua
Change-Id: I1d8b2adaaaae25a0990530fc48a497f9c201da4a
Signed-off-by: zhouruiyu <zhouruiyu@huawei.com>
Diffstat (limited to 'msb-core/openresty-ext')
-rw-r--r-- | msb-core/openresty-ext/src/assembly/resources/openresty/nginx/luaext/plugins/driver_manager.lua | 93 |
1 files changed, 90 insertions, 3 deletions
diff --git a/msb-core/openresty-ext/src/assembly/resources/openresty/nginx/luaext/plugins/driver_manager.lua b/msb-core/openresty-ext/src/assembly/resources/openresty/nginx/luaext/plugins/driver_manager.lua index d337a25..4caa9e2 100644 --- a/msb-core/openresty-ext/src/assembly/resources/openresty/nginx/luaext/plugins/driver_manager.lua +++ b/msb-core/openresty-ext/src/assembly/resources/openresty/nginx/luaext/plugins/driver_manager.lua @@ -17,11 +17,98 @@ ]] local _M = {} _M._VERSION = '1.0.0' +local _HEADER = "X-Driver-Parameter" + +--extract driver header if present in request +local function get_driver_header() + local header = "" + local driver_header = ngx.req.get_headers()[_HEADER] + if (driver_header ~= nil) + then + header = driver_header + end + return header +end + +-- generate query url +local function get_query_url(x_driver_header) + local drivermgr_uri = '/openoapi/drivermgr/v1/drivers' + local url = drivermgr_uri.."?".._HEADER.."="..tostring(ngx.escape_uri(x_driver_header)).."&service_url="..ngx.var.uri + return url +end + +-- generate driver url +local function get_driver_url(driver_header) + local cjson = require "cjson" + local query_url = get_query_url(driver_header) + local res = ngx.location.capture(query_url, { method = ngx.HTTP_GET}) + ngx.log (ngx.ERR, "Driver manager resp url : ", tostring(res.body)) + if (res.status == 200 and res.body ~= nil and res.body ~= '') + then + return tostring(cjson.new().decode(res.body).url) + else + return '' + end +end + +-- get headers +local function get_headers() + local headers = {} + local h = ngx.req.get_headers() + for k, value in pairs(h) + do + headers[k] = value + end + return headers +end + +local function get_body_params() + ngx.req.read_body() + local actual_body = "" + local body_param = ngx.req.get_body_data() + if(body_param ~= nil) + then + actual_body = tostring(body_param) + end + return actual_body +end function _M.access() - ngx.log(ngx.INFO, "running driver_manager plugin") - --add your own code here - --choose the right backend server,and then tell nginx, e.g. ngx.var.backend = XX.XX.XX.XX:8888 + ngx.log(ngx.ERR, "DRIVER MANAGER LUA", "***********************") + + -- extract X-Driver-Parameter header param + local driver_header = get_driver_header() + ngx.log(ngx.ERR, "X-Driver-Parameter: ", driver_header) + + + -- ignore driver redirection if not driver manager request. + if (driver_header ~= "") + then + + local driver_url = get_driver_url(driver_header) + ngx.log (ngx.ERR, "Driver manager URl:: ", driver_url) + + local http = require "resty.http" + local actual_headers = get_headers() + local actual_body = get_body_params() + + ngx.log(ngx.ERR, "HTTP request to driver... ", " Request to driver manager") + local res, err = http.new():request_uri(driver_url, { + method = ngx.req.get_method(), + body = actual_body, + headers = actual_headers + }) + + if not res then + ngx.say("Request to driver failed : ", err) + return + end + ngx.log(ngx.ERR, "Response from driver : ", tostring(res.body)) + ngx.say(res.body) + + else + ngx.log(ngx.ERR, "X-Driver-Parameter not present", " Redirect to same url") + end end return _M
\ No newline at end of file |