-- This software is copyright Kong Inc. and its licensors.
-- Use of the software is subject to the agreement between your organization
-- and Kong Inc. If there is no such agreement, use is governed by and
-- subject to the terms of the Kong Master Software License Agreement found
-- at https://konghq.com/enterprisesoftwarelicense/.
-- [ END OF LICENSE 0867164ffc95e54f04670b5169c09574bdbd9bba ]

return {
  ["/slow-resource"] = {
    GET = function(self)
      local delay = self.params.delay or 1

      -- remove the stack traceback in the error message
      local function wrap_query_error(err)
        if type(err) == "string" then
          local pos = err:find("stack traceback:")
          if pos then
            err = err:sub(1, pos - 1)
            return err:gsub("%s*$", "")
          end
        end
        return err
      end

      if self.params.prime then
        ngx.timer.at(0, function()
          local _, err = kong.db.connector:query("SELECT pg_sleep(" .. delay .. ")")
          if err then
            err = wrap_query_error(err)
            ngx.log(ngx.ERR, err)
          end
        end)

        return kong.response.exit(204)
      end

      local _, err = kong.db.connector:query("SELECT pg_sleep(" .. delay .. ")")
      if err then
        err = wrap_query_error(err)
        return kong.response.exit(500, { error = err })
      end

      return kong.response.exit(204)
    end,
  },
}
