util.lua 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. local _M = {}
  2. local config = require("config")
  3. function _M.ifnull(var, value)
  4. if var == nil then
  5. return value
  6. end
  7. return var
  8. end
  9. function _M.trim (s)
  10. return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
  11. end
  12. function _M.replace(s, s1, s2)
  13. local str = string.gsub(s, s1, s2)
  14. return str
  15. end
  16. function _M.endswith(str,endstr)
  17. return endstr=='' or string.sub(str,-string.len(endstr))==endstr
  18. end
  19. function _M.startswith(str,startstr)
  20. return startstr=='' or string.sub(str,1, string.len(startstr))==startstr
  21. end
  22. -- delimiter 应该是单个字符。如果是多个字符,表示以其中任意一个字符做分割。
  23. function _M.split(s, delimiter)
  24. local result = {};
  25. for match in string.gmatch(s, "[^"..delimiter.."]+") do
  26. table.insert(result, match);
  27. end
  28. return result;
  29. end
  30. function _M.redirect(uri, args)
  31. local uri_and_args = uri
  32. if args then
  33. uri_and_args = uri_and_args .. "?" .. args
  34. end
  35. ngx.header['Location'] = uri_and_args
  36. ngx.exit(ngx.HTTP_MOVED_TEMPORARILY)
  37. end
  38. function _M.url_in_ignore_list(url)
  39. if config.ignore_list == nil then
  40. return false
  41. end
  42. local matched = false
  43. -- equals match
  44. if type(config.ignore_list.equals)=='table' then
  45. for i, item in ipairs(config.ignore_list.equals) do
  46. if item == url then
  47. matched = true
  48. break
  49. end
  50. end
  51. end
  52. if matched then
  53. return matched
  54. end
  55. -- suffix match
  56. if not matched and type(config.ignore_list.suffix)=='table' then
  57. for i, item in ipairs(config.ignore_list.suffix) do
  58. if _M.endswith(url, item) then
  59. matched = true
  60. break
  61. end
  62. end
  63. end
  64. if matched then
  65. return matched
  66. end
  67. -- prefix match
  68. if not matched and type(config.ignore_list.prefix)=='table' then
  69. for i, item in ipairs(config.ignore_list.prefix) do
  70. if _M.startswith(url, item) then
  71. matched = true
  72. break
  73. end
  74. end
  75. end
  76. if matched then
  77. return matched
  78. end
  79. return matched
  80. end
  81. function _M.localtime(seconds, format)
  82. seconds = tonumber(seconds)
  83. format = format or "%Y-%m-%d %H:%M:%S"
  84. return os.date(format, seconds)
  85. end
  86. function _M.clientIP()
  87. local headers = ngx.req.get_headers()
  88. local ip = headers["x-forwarded-for"]
  89. if ip == nil then
  90. ip = headers["x-real-ip"]
  91. end
  92. if ip == nil then
  93. ip = ngx.var.remote_addr
  94. end
  95. if type(ip) == 'table' then
  96. ip = ip[#ip]
  97. end
  98. return ip, ngx.var.country_code, ngx.var.city_name
  99. end
  100. return _M