include /etc/nginx/conf.d/include/init-optional-variables;

location /skynet/blocklist {
    client_max_body_size 10m; # increase max body size to account for large lists
    client_body_buffer_size 10m; # force whole body to memory so we can read it

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

        ngx.req.read_body() -- ensure the post body data is read before using get_body_data

        -- proxy blocklist update request
        -- 10.10.10.10 points to sia service (alias not available when using resty-http)
        local res, err = httpc:request_uri("http://10.10.10.10:9980/skynet/blocklist", {
            method = "POST",
            body = ngx.req.get_body_data(),
            headers = {
                ["Content-Type"] = "application/x-www-form-urlencoded",
                ["Authorization"] = require("skynet.utils").authorization_header(),
                ["User-Agent"] = "Sia-Agent",
            }
        })

        -- print error and exit with 500 or exit with response if status is not 204
        if err or (res and res.status ~= ngx.HTTP_NO_CONTENT) then
            ngx.status = (err and ngx.HTTP_INTERNAL_SERVER_ERROR) or res.status
            ngx.header["content-type"] = "text/plain"
            ngx.say(err or res.body)
            return ngx.exit(ngx.status)
        end

        require("skynet.blocklist").reload()

        ngx.status = ngx.HTTP_NO_CONTENT
        return ngx.exit(ngx.status)
    }
}