tinywebdis - a lean webdis replacement using turbo.lua
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

turbowebdis.lua 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/luajit
  2. -- tinywebdis 1.3 powered by turbo.lua
  3. local config = require("config")
  4. local turbo = require("turbo")
  5. local escape = require("turbo.escape")
  6. turbo.log.categories.success = config.turbo.logging
  7. local function splitIntoArgs(path)
  8. local args = {}
  9. for arg in string.gmatch(path, "[^/]+") do
  10. -- table.insert(args, arg)
  11. -- faster way (less function calls)
  12. args[#args+1] = arg
  13. end
  14. return args
  15. end -- function splitIntoArgs
  16. local function callRedis(string)
  17. -- make redis connection using https://github.com/soveran/resp
  18. local resp = require("resp")
  19. -- enter redis connection details here
  20. local client = resp.new(config.redis.host, config.redis.port)
  21. local args = splitIntoArgs(string)
  22. local key = args[1]
  23. -- execute command
  24. local unpack = unpack or table.unpack
  25. if #config.redis.auth > 0 then client:call("AUTH", config.redis.auth) end
  26. if config.redis.db ~= "0" then client:call("SELECT", config.redis.db) end
  27. local value = client:call(unpack(args))
  28. local ret = {}
  29. ret[key] = value
  30. return ret
  31. end -- function callRedis
  32. local IndexHandler = class("IndexHandler", turbo.web.RequestHandler)
  33. -- Method POST
  34. -- Sending json like this JSON.stringify({"auth": "", "db": 1, "command": ["GET", "SOME", "VALUE"]})
  35. function IndexHandler:post()
  36. self:add_header('Access-Control-Allow-Origin','*')
  37. local json = self:get_json(true)
  38. auth = json['auth'] or auth
  39. db = tostring(json['db'] or db) -- make sure that the db number is a string
  40. local cmd = json['command']
  41. self:write(callRedis(table.concat(cmd,'/')))
  42. end -- POST
  43. -- Method GET
  44. function IndexHandler:get(input)
  45. -- add cors header
  46. self:add_header('Access-Control-Allow-Origin','*')
  47. self:add_header('Content-Type', 'application/json')
  48. -- count arguments
  49. input,argCount = string.gsub(input, "/", "%1")
  50. if ((argCount <= 1) and (#input <= 1)) then
  51. self:write({usage="command/key/option[1]/option[2]/..."})
  52. else
  53. -- get/a?callback=mycallback
  54. -- jsonp return
  55. local callback = self:get_argument("callback", -1)
  56. if ((config.turbo.JSONP) and (type (callback) == "string")) then
  57. local retval = callRedis (input)
  58. self:write (callback .. "(" .. escape.json_encode (retval) .. ")")
  59. else
  60. self:write (callRedis (input))
  61. end
  62. end -- if
  63. end -- GET
  64. -- CORS preflight request
  65. -- necessary for POST method
  66. function IndexHandler:options()
  67. if (config.turbo.CORS) then
  68. self:add_header('Access-Control-Allow-Methods', 'POST')
  69. self:add_header('Access-Control-Allow-Headers', 'content-type')
  70. self:add_header('Access-Control-Allow-Origin', '*')
  71. end
  72. end -- CORS preflight
  73. local app = turbo.web.Application:new({
  74. -- parse arguments
  75. {"/(.*)$", IndexHandler},
  76. })
  77. app:listen(config.turbo.port)
  78. turbo.ioloop.instance():start()