# register the upload in accounts service (cookies should contain jwt)
log_by_lua_block {
    -- this block runs only when accounts are enabled
    if os.getenv("ACCOUNTS_ENABLED") ~= "true" then return end

    local function track(premature, skylink, jwt)
        if premature then return end

        local httpc = require("resty.http").new()

        -- 10.10.10.70 points to accounts service (alias not available when using resty-http)
        local res, err = httpc:request_uri("http://10.10.10.70:3000/track/upload/" .. skylink, {
            method = "POST",
            headers = { ["Cookie"] = "skynet-jwt=" .. jwt },
        })
        
        if err or (res and res.status ~= ngx.HTTP_NO_CONTENT) then
            ngx.log(ngx.ERR, "Failed accounts service request /track/upload/" .. skylink .. ": ", err or ("[HTTP " .. res.status .. "] " .. res.body))
        end
    end

    if ngx.header["Skynet-Skylink"] and ngx.var.skynet_jwt ~= "" then
        local ok, err = ngx.timer.at(0, track, ngx.header["Skynet-Skylink"], ngx.var.skynet_jwt)
        if err then ngx.log(ngx.ERR, "Failed to create timer: ", err) end
    end
}