This repository has been archived on 2022-10-07. You can view files and clone it, but cannot push or open issues or pull requests.
skynet-webportal/docker/nginx/conf.d/include/track-registry

34 lines
1.5 KiB
Plaintext
Raw Permalink Normal View History

log_by_lua_block {
2022-03-15 21:43:35 +00:00
local skynet_account = require("skynet.account")
-- tracking runs only when request comes from authenticated user
if skynet_account.is_authenticated() then
local function track(premature, request_method, auth_headers)
if premature then return end
local httpc = require("resty.http").new()
2022-01-07 12:36:04 +00:00
-- based on request method we assign a registry action string used
2022-01-07 12:36:04 +00:00
-- in track endpoint namely "read" for GET and "write" for POST
local registry_action = request_method == "GET" and "read" or "write"
2021-08-27 12:15:22 +00:00
-- 10.10.10.70 points to accounts service (alias not available when using resty-http)
2022-01-07 12:36:04 +00:00
local res, err = httpc:request_uri("http://10.10.10.70:3000/track/registry/" .. registry_action, {
method = "POST",
2022-03-15 21:43:35 +00:00
headers = auth_headers,
})
if err or (res and res.status ~= ngx.HTTP_NO_CONTENT) then
local error_response = err or ("[HTTP " .. res.status .. "] " .. res.body)
ngx.log(ngx.ERR, "Failed accounts service request /track/registry/" .. registry_action .. ": ", error_response)
end
end
2021-08-27 12:15:22 +00:00
2022-03-15 21:43:35 +00:00
if ngx.status == ngx.HTTP_OK or ngx.status == ngx.HTTP_NOT_FOUND then
2022-03-21 13:43:42 +00:00
local auth_headers = skynet_account.get_auth_headers()
local ok, err = ngx.timer.at(0, track, ngx.req.get_method(), auth_headers)
if err then ngx.log(ngx.ERR, "Failed to create timer: ", err) end
end
2021-08-27 12:15:22 +00:00
end
}