unify httpc usage
This commit is contained in:
@@ -3,10 +3,13 @@
|
||||
package.path = package.path .. ';<SCRIPT_PATH>/?.lua';
|
||||
|
||||
local cjson = require "cjson"
|
||||
local http = require 'resty.http'
|
||||
local uuid = require "resty.jit-uuid"
|
||||
local upyun_upload = require("upyun_upload")
|
||||
local get_wx_media = require("wx_download")
|
||||
|
||||
local httpc = http:new()
|
||||
|
||||
local function convert()
|
||||
|
||||
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())
|
||||
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";
|
||||
|
||||
ngx.log(ngx.INFO, "Input audio file -> ", input_audio_file);
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
local cjson = require "cjson"
|
||||
local http = require "resty.http"
|
||||
|
||||
|
||||
local TaskTypes = {
|
||||
@@ -15,8 +14,7 @@ ngx.log(ngx.INFO, "TASK_SERVER -> ", task_server)
|
||||
ngx.log(ngx.INFO, "HTY HOST -> ", htyhost)
|
||||
|
||||
|
||||
local function create_task(files, authHeader, sudoerToken)
|
||||
local httpc = http.new()
|
||||
local function create_task(httpc, files, authHeader, sudoerToken)
|
||||
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, 'Authorization -> ', authHeader)
|
||||
@@ -48,8 +46,6 @@ local function create_task(files, authHeader, sudoerToken)
|
||||
|
||||
end
|
||||
ngx.say(res.body)
|
||||
|
||||
httpc:close()
|
||||
|
||||
ngx.exit(res.status)
|
||||
end
|
||||
|
||||
@@ -68,7 +68,7 @@ local function read_form_file()
|
||||
file_name = nil
|
||||
file = nil
|
||||
elseif type == "eof" then
|
||||
create_task(files, authHeader, sudoerToken)
|
||||
create_task(httpc, files, authHeader, sudoerToken)
|
||||
break
|
||||
else
|
||||
-- do nothing
|
||||
|
||||
@@ -28,12 +28,10 @@ local function cleanupString(str, remove)
|
||||
return lcMergeStr
|
||||
end
|
||||
|
||||
local function verifyJwtToken(token)
|
||||
local function verifyJwtToken(httpc, token)
|
||||
local htyuc = ngx.var.htyuc
|
||||
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)
|
||||
|
||||
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
|
||||
ngx.log(ngx.ERR, "CAN'T VERIFY JWT TOKEN -> ", err)
|
||||
ngx.exit(ngx.HTTP_FORBIDDEN)
|
||||
@@ -86,16 +82,16 @@ local function decodeJwtClaim(jwtClaim)
|
||||
return htyToken
|
||||
end
|
||||
|
||||
local function verify(authHeader, sudoerToken)
|
||||
local function verify(httpc, authHeader, sudoerToken)
|
||||
|
||||
if authHeader then
|
||||
ngx.log(ngx.INFO, 'CHECK_AUTH_HEADER -> ', authHeader)
|
||||
-- local token = decodeJwtClaim(authHeader)
|
||||
verifyJwtToken(authHeader)
|
||||
verifyJwtToken(httpc, authHeader)
|
||||
elseif sudoerToken then
|
||||
ngx.log(ngx.INFO, 'CHECK_SUDOER_HEADER -> ', sudoerToken)
|
||||
-- local token = decodeJwtClaim(sudoerToken)
|
||||
verifyJwtToken(sudoerToken)
|
||||
verifyJwtToken(httpc, sudoerToken)
|
||||
else
|
||||
ngx.log(ngx.ERR, 'NO AUTH/SUDOER HEADERS!')
|
||||
ngx.status = 500
|
||||
|
||||
@@ -41,8 +41,6 @@ local function create_task()
|
||||
}
|
||||
)
|
||||
|
||||
httpc:close()
|
||||
|
||||
if res == nil then
|
||||
ngx.log(ngx.ERR, "FAILED TO CONNECT TO *TASK_SERVER*", err)
|
||||
end
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
local cjson = require "cjson"
|
||||
local http = require "resty.http"
|
||||
local pl = require "pl.pretty"
|
||||
local httpc = http.new()
|
||||
local sudoerToken = ngx.req.get_headers().HtySudoerToken
|
||||
local htyhostHeader = ngx.req.get_headers().HtyHost
|
||||
|
||||
local function get_access_token()
|
||||
local function get_access_token(httpc)
|
||||
local htyuc = ngx.var.htyuc
|
||||
local host = ngx.var.host
|
||||
local wx_domain = ngx.var.wx_domain
|
||||
@@ -31,7 +29,6 @@ local function get_access_token()
|
||||
})
|
||||
|
||||
ngx.log(ngx.INFO, "RESP -> ", pl.write(resp))
|
||||
httpc:close()
|
||||
if no_exception == true then
|
||||
if 200 ~= resp.status then
|
||||
ngx.log(ngx.ERR, "GET ACCESS TOKEN *FAILED*", pl.write(resp_err))
|
||||
@@ -57,8 +54,8 @@ local function get_access_token()
|
||||
|
||||
end
|
||||
|
||||
local function get_wx_media(media_id)
|
||||
local access_token = get_access_token()
|
||||
local function get_wx_media(httpc, media_id)
|
||||
local access_token = get_access_token(httpc)
|
||||
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)
|
||||
print("wx_media_url: " .. wx_media_url)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
-- 指定模块引用目录,否则无法加载同目录下的其他文件
|
||||
--package.path = package.path .. ';/usr/local/opt/openresty/nginx/scripts/?.lua';
|
||||
package.path = package.path .. ';<SCRIPT_PATH>/?.lua';
|
||||
|
||||
local http = require "resty.http"
|
||||
local cjson = require "cjson"
|
||||
local uuid = require "resty.jit-uuid"
|
||||
local upyun_upload = require("upyun_upload")
|
||||
@@ -9,9 +9,11 @@ local get_wx_media = require("wx_download")
|
||||
|
||||
local verify = require('jwt_verify')
|
||||
|
||||
local httpc = http:new()
|
||||
|
||||
local authHeader = ngx.req.get_headers().Authorization
|
||||
local sudoerToken = ngx.req.get_headers().HtySudoerToken
|
||||
verify(authHeader, sudoerToken)
|
||||
verify(httpc, authHeader, sudoerToken)
|
||||
|
||||
local function run()
|
||||
|
||||
@@ -19,7 +21,7 @@ local function run()
|
||||
local req_body = cjson.decode(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")
|
||||
end
|
||||
|
||||
@@ -3,14 +3,16 @@
|
||||
package.path = package.path .. ';<SCRIPT_PATH>/?.lua';
|
||||
|
||||
local cjson = require "cjson"
|
||||
local http = require 'resty.http'
|
||||
local get_wx_media = require("wx_download")
|
||||
local create_task = require("create_task")
|
||||
local verify = require('jwt_verify')
|
||||
local httpc = http:new()
|
||||
|
||||
|
||||
local authHeader = ngx.req.get_headers().Authorization
|
||||
local sudoerToken = ngx.req.get_headers().HtySudoerToken
|
||||
verify(authHeader, sudoerToken)
|
||||
verify(httpc, authHeader, sudoerToken)
|
||||
|
||||
|
||||
local function read_wx_file()
|
||||
@@ -21,11 +23,11 @@ local function read_wx_file()
|
||||
local files = {}
|
||||
for i = 1, #req_body["media_ids"] do
|
||||
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);
|
||||
end
|
||||
|
||||
create_task(files, authHeader, sudoerToken)
|
||||
create_task(httpc, files, authHeader, sudoerToken)
|
||||
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user