unify httpc usage

This commit is contained in:
moicen
2022-06-27 04:16:16 +08:00
parent f71d0c9e27
commit c892e8fb41
4 changed files with 15 additions and 49 deletions
+4 -4
View File
@@ -14,15 +14,15 @@ ngx.log(ngx.INFO, "TASK_SERVER -> ", task_server)
ngx.log(ngx.INFO, "HTY HOST -> ", htyhost)
local function create_task(httpc, files, authHeader, sudoerToken)
local function create_task(httpc, task_text, 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)
ngx.log(ngx.INFO, 'HtySudoerToken -> ', sudoerToken)
local body_text = cjson.encode({ task_type = TaskTypes.UPLOAD_PICTURE, payload = { images = files } })
ngx.log(ngx.INFO, 'UPLOAD_PICTURE *body_text* ->', body_text)
ngx.log(ngx.INFO, 'UPLOAD_PICTURE *body_text* ->', task_text)
local res, err = httpc:request_uri(
remote_url,
{
@@ -34,7 +34,7 @@ local function create_task(httpc, files, authHeader, sudoerToken)
["HtySudoerToken"] = sudoerToken,
['HtyHost'] = htyhost
},
body = body_text,
body = task_text,
}
)
if res == nil then
+5 -3
View File
@@ -4,13 +4,14 @@ package.path = package.path .. ';<SCRIPT_PATH>/?.lua';
local upload = require "resty.upload"
local uuid = require "resty.jit-uuid"
local http = require "resty.http"
local verify = require('jwt_verify')
local create_task = require("create_task")
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_form_file()
local chunk_size = 4096
@@ -68,7 +69,8 @@ local function read_form_file()
file_name = nil
file = nil
elseif type == "eof" then
create_task(httpc, files, authHeader, sudoerToken)
local task_text = cjson.encode({ task_type = TaskTypes.UPLOAD_PICTURE, payload = { images = files } })
create_task(httpc, task_text, authHeader, sudoerToken)
break
else
-- do nothing
+4 -40
View File
@@ -1,6 +1,7 @@
package.path = package.path .. ';<SCRIPT_PATH>/?.lua';
local cjson = require "cjson"
local http = require "resty.http"
local create_task = require "create_task"
local TaskTypes = {
NOOP = 'NOOP',
CONVERT_AUDIO_FILE = "CONVERT_AUDIO_FILE"
@@ -13,45 +14,8 @@ print('req_body...', ngx.req.get_body_data())
local media_id = req_body['media_id']
local authHeader = ngx.req.get_headers().Authorization
local sudoerToken = ngx.req.get_headers().HtySudoerToken
local htyHost = ngx.req.get_headers().HtyHost
local function create_task()
local task_server = ngx.var.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, 'Authorization -> ', authHeader)
ngx.log(ngx.INFO, 'HtySudoerToken -> ', sudoerToken)
ngx.log(ngx.INFO, 'HtyHost -> ', htyHost)
local body_text = cjson.encode({ task_type = TaskTypes.CONVERT_AUDIO_FILE, payload = { media_id = media_id } })
ngx.log(ngx.INFO, 'AUDIO_CONVERT *body_text* ->', body_text)
local res, err = httpc:request_uri(
remote_url,
{
ssl_verify = false, -- 设置参数 ssl_verify 为false 不校验ssl证书
method = "POST",
headers = {
["Content-Type"] = "application/json",
["Authorization"] = authHeader,
["HtySudoerToken"] = sudoerToken,
["HtyHost"] = htyHost
},
body = body_text,
}
)
if res == nil then
ngx.log(ngx.ERR, "FAILED TO CONNECT TO *TASK_SERVER*", err)
end
if 201 ~= res.status then
ngx.log(ngx.ERR, "TASK CREATE *FAILED*", res.body)
end
ngx.say(res.body)
ngx.exit(res.status)
end
create_task()
local task_text = cjson.encode({ task_type = TaskTypes.CONVERT_AUDIO_FILE, payload = { media_id = media_id } })
create_task(httpc, task_text, authHeader, sudoerToken)
+2 -2
View File
@@ -26,8 +26,8 @@ local function read_wx_file()
local _, filename = get_wx_media(httpc, req_body["media_ids"][i])
table.insert(files, filename);
end
create_task(httpc, files, authHeader, sudoerToken)
local task_text = cjson.encode({ task_type = TaskTypes.UPLOAD_PICTURE, payload = { images = files } })
create_task(httpc, task_text, authHeader, sudoerToken)
end