handler.lua 871 B

123456789101112131415161718192021222324252627282930313233343536
  1. local consts = require "resty.t1k.constants"
  2. local log = require "resty.t1k.log"
  3. local fmt = string.format
  4. local ngx = ngx
  5. local log_fmt = log.fmt
  6. local _M = {
  7. _VERSION = '1.0.0'
  8. }
  9. function _M.handle(t)
  10. local t_type = type(t)
  11. if t_type ~= "table" then
  12. local err = log_fmt("invalid result type: %s", t_type)
  13. return nil, err
  14. end
  15. local action = t["action"]
  16. if action == consts.ACTION_PASSED then
  17. return true, nil
  18. elseif action == consts.ACTION_BLOCKED then
  19. ngx.status = t["status"] or ngx.HTTP_FORBIDDEN
  20. ngx.header.content_type = consts.BLOCK_CONTENT_TYPE
  21. ngx.say(fmt(consts.BLOCK_CONTENT_FORMAT, ngx.status, t["event_id"]))
  22. return ngx.exit(ngx.status)
  23. else
  24. local err = log_fmt("unknown action from t1k server: %s", action)
  25. return nil, err
  26. end
  27. end
  28. return _M