Enhancing API Gateway through OpenResty and Lua

Redis API String
local function check_public_api(ngx, api_str)
-- Converts Redis API string to Lua table
local api_table = get_api_table(api_str)
if (api_table[ngx.var.uri] == "true") then
return {
["status"] = true,
["message"] = "PUBLIC_API"
}
else
-- Handle dynamic URLs
for k,v in pairs(api_table) do
local final_api_string = k:gsub("%:id", "%%w+")

if (string.match( ngx.var.request_method .. ":" .. ngx.var.uri,
final_api_string) ~= nil) then
return {
["status"] = true,
["message"] = "PUBLIC_API"
}
end
end
end
return {
["status"] = false,
["message"] = "PRIVATE_API"
}
end
Archived User in Redis
function _M.is_user_allowed(self, ngx, redis_conn)
-- Get JWT Token
local jwt_token = helpers:fetch_jwt(ngx)
if (jwt_token == nil or jwt_token == "") then
return {
["status"] = false,
["message"] = "JWT_NOT_PRESENT"
}
end

-- Fetch User ID from JWT Payload
local jwt_payload_b64 = ngx.decode_base64(helpers:string_split(jwt_token, ".")[2])
local jwt_payload = cjson.decode(jwt_payload_b64)
local emp_id = jwt_payload["id"]

-- Fetch User Record from Redis
local emp_red_resp = redis_conn:get("ARCHIVED_" .. emp_id)
if emp_red_resp == "true" then
return {
["status"] = false,
["message"] = "USER_ARCHIVED"
}
end
return {
["status"] = true,
["message"] = "USER_ACTIVE"
}
end

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store