unify httpc usage

This commit is contained in:
moicen
2022-06-27 03:56:15 +08:00
parent 27e928f610
commit f71d0c9e27
8 changed files with 23 additions and 29 deletions
+4 -1
View File
@@ -3,10 +3,13 @@
package.path = package.path .. ';<SCRIPT_PATH>/?.lua'; package.path = package.path .. ';<SCRIPT_PATH>/?.lua';
local cjson = require "cjson" local cjson = require "cjson"
local http = require 'resty.http'
local uuid = require "resty.jit-uuid" local uuid = require "resty.jit-uuid"
local upyun_upload = require("upyun_upload") local upyun_upload = require("upyun_upload")
local get_wx_media = require("wx_download") local get_wx_media = require("wx_download")
local httpc = http:new()
local function convert() local function convert()
local file_dir = ngx.var.tmp_file_dir local file_dir = ngx.var.tmp_file_dir
@@ -14,7 +17,7 @@ local function convert()
local req_body = cjson.decode(ngx.req.get_body_data()) local req_body = cjson.decode(ngx.req.get_body_data())
ngx.log(ngx.INFO, 'REQ_BODY -> ', ngx.req.get_body_data()) ngx.log(ngx.INFO, 'REQ_BODY -> ', ngx.req.get_body_data())
local input_audio_file = get_wx_media(req_body["payload"]["media_id"]); local input_audio_file = get_wx_media(httpc, req_body["payload"]["media_id"]);
local converted_audio_file = file_dir .. "/" .. uuid.generate_v4() .. ".mp3"; local converted_audio_file = file_dir .. "/" .. uuid.generate_v4() .. ".mp3";
ngx.log(ngx.INFO, "Input audio file -> ", input_audio_file); ngx.log(ngx.INFO, "Input audio file -> ", input_audio_file);
+1 -5
View File
@@ -1,5 +1,4 @@
local cjson = require "cjson" local cjson = require "cjson"
local http = require "resty.http"
local TaskTypes = { local TaskTypes = {
@@ -15,8 +14,7 @@ ngx.log(ngx.INFO, "TASK_SERVER -> ", task_server)
ngx.log(ngx.INFO, "HTY HOST -> ", htyhost) ngx.log(ngx.INFO, "HTY HOST -> ", htyhost)
local function create_task(files, authHeader, sudoerToken) local function create_task(httpc, files, authHeader, sudoerToken)
local httpc = http.new()
local remote_url = string.format("%s/api/v1/ts/create_task", task_server) local remote_url = string.format("%s/api/v1/ts/create_task", task_server)
ngx.log(ngx.INFO, 'remote_url -> ', remote_url) ngx.log(ngx.INFO, 'remote_url -> ', remote_url)
ngx.log(ngx.INFO, 'Authorization -> ', authHeader) ngx.log(ngx.INFO, 'Authorization -> ', authHeader)
@@ -48,8 +46,6 @@ local function create_task(files, authHeader, sudoerToken)
end end
ngx.say(res.body) ngx.say(res.body)
httpc:close()
ngx.exit(res.status) ngx.exit(res.status)
end end
+1 -1
View File
@@ -68,7 +68,7 @@ local function read_form_file()
file_name = nil file_name = nil
file = nil file = nil
elseif type == "eof" then elseif type == "eof" then
create_task(files, authHeader, sudoerToken) create_task(httpc, files, authHeader, sudoerToken)
break break
else else
-- do nothing -- do nothing
+4 -8
View File
@@ -28,12 +28,10 @@ local function cleanupString(str, remove)
return lcMergeStr return lcMergeStr
end end
local function verifyJwtToken(token) local function verifyJwtToken(httpc, token)
local htyuc = ngx.var.htyuc local htyuc = ngx.var.htyuc
local htyhost = ngx.req.get_headers().HtyHost local htyhost = ngx.req.get_headers().HtyHost
local http = require "resty.http"
local httpc = http.new()
local verify_jwt_url = string.format("%s/api/v1/uc/verify_jwt_token", htyuc) local verify_jwt_url = string.format("%s/api/v1/uc/verify_jwt_token", htyuc)
ngx.log(ngx.INFO, 'HTYUC VERIFY_JWT_TOKEN_URL -> ' .. verify_jwt_url) ngx.log(ngx.INFO, 'HTYUC VERIFY_JWT_TOKEN_URL -> ' .. verify_jwt_url)
@@ -50,8 +48,6 @@ local function verifyJwtToken(token)
} }
) )
httpc:close()
if not res then if not res then
ngx.log(ngx.ERR, "CAN'T VERIFY JWT TOKEN -> ", err) ngx.log(ngx.ERR, "CAN'T VERIFY JWT TOKEN -> ", err)
ngx.exit(ngx.HTTP_FORBIDDEN) ngx.exit(ngx.HTTP_FORBIDDEN)
@@ -86,16 +82,16 @@ local function decodeJwtClaim(jwtClaim)
return htyToken return htyToken
end end
local function verify(authHeader, sudoerToken) local function verify(httpc, authHeader, sudoerToken)
if authHeader then if authHeader then
ngx.log(ngx.INFO, 'CHECK_AUTH_HEADER -> ', authHeader) ngx.log(ngx.INFO, 'CHECK_AUTH_HEADER -> ', authHeader)
-- local token = decodeJwtClaim(authHeader) -- local token = decodeJwtClaim(authHeader)
verifyJwtToken(authHeader) verifyJwtToken(httpc, authHeader)
elseif sudoerToken then elseif sudoerToken then
ngx.log(ngx.INFO, 'CHECK_SUDOER_HEADER -> ', sudoerToken) ngx.log(ngx.INFO, 'CHECK_SUDOER_HEADER -> ', sudoerToken)
-- local token = decodeJwtClaim(sudoerToken) -- local token = decodeJwtClaim(sudoerToken)
verifyJwtToken(sudoerToken) verifyJwtToken(httpc, sudoerToken)
else else
ngx.log(ngx.ERR, 'NO AUTH/SUDOER HEADERS!') ngx.log(ngx.ERR, 'NO AUTH/SUDOER HEADERS!')
ngx.status = 500 ngx.status = 500
-2
View File
@@ -41,8 +41,6 @@ local function create_task()
} }
) )
httpc:close()
if res == nil then if res == nil then
ngx.log(ngx.ERR, "FAILED TO CONNECT TO *TASK_SERVER*", err) ngx.log(ngx.ERR, "FAILED TO CONNECT TO *TASK_SERVER*", err)
end end
+3 -6
View File
@@ -1,11 +1,9 @@
local cjson = require "cjson" local cjson = require "cjson"
local http = require "resty.http"
local pl = require "pl.pretty" local pl = require "pl.pretty"
local httpc = http.new()
local sudoerToken = ngx.req.get_headers().HtySudoerToken local sudoerToken = ngx.req.get_headers().HtySudoerToken
local htyhostHeader = ngx.req.get_headers().HtyHost local htyhostHeader = ngx.req.get_headers().HtyHost
local function get_access_token() local function get_access_token(httpc)
local htyuc = ngx.var.htyuc local htyuc = ngx.var.htyuc
local host = ngx.var.host local host = ngx.var.host
local wx_domain = ngx.var.wx_domain local wx_domain = ngx.var.wx_domain
@@ -31,7 +29,6 @@ local function get_access_token()
}) })
ngx.log(ngx.INFO, "RESP -> ", pl.write(resp)) ngx.log(ngx.INFO, "RESP -> ", pl.write(resp))
httpc:close()
if no_exception == true then if no_exception == true then
if 200 ~= resp.status then if 200 ~= resp.status then
ngx.log(ngx.ERR, "GET ACCESS TOKEN *FAILED*", pl.write(resp_err)) ngx.log(ngx.ERR, "GET ACCESS TOKEN *FAILED*", pl.write(resp_err))
@@ -57,8 +54,8 @@ local function get_access_token()
end end
local function get_wx_media(media_id) local function get_wx_media(httpc, media_id)
local access_token = get_access_token() local access_token = get_access_token(httpc)
print("access token: " .. access_token) print("access token: " .. access_token)
local wx_media_url = string.format("https://api.weixin.qq.com/cgi-bin/media/get?access_token=%s&media_id=%s", access_token, media_id) local wx_media_url = string.format("https://api.weixin.qq.com/cgi-bin/media/get?access_token=%s&media_id=%s", access_token, media_id)
print("wx_media_url: " .. wx_media_url) print("wx_media_url: " .. wx_media_url)
+5 -3
View File
@@ -1,7 +1,7 @@
-- 指定模块引用目录,否则无法加载同目录下的其他文件 -- 指定模块引用目录,否则无法加载同目录下的其他文件
--package.path = package.path .. ';/usr/local/opt/openresty/nginx/scripts/?.lua'; --package.path = package.path .. ';/usr/local/opt/openresty/nginx/scripts/?.lua';
package.path = package.path .. ';<SCRIPT_PATH>/?.lua'; package.path = package.path .. ';<SCRIPT_PATH>/?.lua';
local http = require "resty.http"
local cjson = require "cjson" local cjson = require "cjson"
local uuid = require "resty.jit-uuid" local uuid = require "resty.jit-uuid"
local upyun_upload = require("upyun_upload") local upyun_upload = require("upyun_upload")
@@ -9,9 +9,11 @@ local get_wx_media = require("wx_download")
local verify = require('jwt_verify') local verify = require('jwt_verify')
local httpc = http:new()
local authHeader = ngx.req.get_headers().Authorization local authHeader = ngx.req.get_headers().Authorization
local sudoerToken = ngx.req.get_headers().HtySudoerToken local sudoerToken = ngx.req.get_headers().HtySudoerToken
verify(authHeader, sudoerToken) verify(httpc, authHeader, sudoerToken)
local function run() local function run()
@@ -19,7 +21,7 @@ local function run()
local req_body = cjson.decode(ngx.req.get_body_data()) local req_body = cjson.decode(ngx.req.get_body_data())
ngx.log(ngx.INFO, 'REQ_BODY -> ', ngx.req.get_body_data()) ngx.log(ngx.INFO, 'REQ_BODY -> ', ngx.req.get_body_data())
local downloaded_file = get_wx_media(req_body["media_id"]); local downloaded_file = get_wx_media(httpc, req_body["media_id"]);
upyun_upload.upload(downloaded_file, uuid.generate_v4() .. ".jpg") upyun_upload.upload(downloaded_file, uuid.generate_v4() .. ".jpg")
end end
+5 -3
View File
@@ -3,14 +3,16 @@
package.path = package.path .. ';<SCRIPT_PATH>/?.lua'; package.path = package.path .. ';<SCRIPT_PATH>/?.lua';
local cjson = require "cjson" local cjson = require "cjson"
local http = require 'resty.http'
local get_wx_media = require("wx_download") local get_wx_media = require("wx_download")
local create_task = require("create_task") local create_task = require("create_task")
local verify = require('jwt_verify') local verify = require('jwt_verify')
local httpc = http:new()
local authHeader = ngx.req.get_headers().Authorization local authHeader = ngx.req.get_headers().Authorization
local sudoerToken = ngx.req.get_headers().HtySudoerToken local sudoerToken = ngx.req.get_headers().HtySudoerToken
verify(authHeader, sudoerToken) verify(httpc, authHeader, sudoerToken)
local function read_wx_file() local function read_wx_file()
@@ -21,11 +23,11 @@ local function read_wx_file()
local files = {} local files = {}
for i = 1, #req_body["media_ids"] do for i = 1, #req_body["media_ids"] do
print("media_id -> ", i, req_body["media_ids"][i]) print("media_id -> ", i, req_body["media_ids"][i])
local _, filename = get_wx_media(req_body["media_ids"][i]) local _, filename = get_wx_media(httpc, req_body["media_ids"][i])
table.insert(files, filename); table.insert(files, filename);
end end
create_task(files, authHeader, sudoerToken) create_task(httpc, files, authHeader, sudoerToken)
end end