Skip to content

Instantly share code, notes, and snippets.

@solisoft
Last active January 5, 2024 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save solisoft/b534055a1f5832f56520c18ed739a6aa to your computer and use it in GitHub Desktop.
Save solisoft/b534055a1f5832f56520c18ed739a6aa to your computer and use it in GitHub Desktop.
Redbean Redis Client
package.path = package.path .. ";.lua/?.lua"
local db_config = {
db_name = "db_delupay",
url = "http://localhost:8529",
username = "root",
password = "3fe2003d-6f42-474f-aa43-1dde7a2a2bf1"
}
redis = require 'redis'
Redis_client = null
-- ProgramMaxWorkers(2)
Jwt = ""
local last_db_connect = GetTime()
function Table_merge(t1, t2)
for k, v in ipairs(t2) do
table.insert(t1, v)
end
return t1
end
function Api_url(path)
return db_config.url .. "/_db/" .. db_config.db_name .. "/_api" .. path
end
function Api_run(path, method, params, headers)
params = params or {}
headers = headers or {}
local ok, h, body = Fetch(
Api_url(path), {
method = method,
body = EncodeJson(params),
headers = Table_merge({ ["Authorization"] = "bearer " .. Jwt }, headers)
}
)
return DecodeJson(body), ok, h
end
function Auth(db_config)
local ok, headers, body = Fetch(
db_config.url .. "/_open/auth", {
method = "POST",
body = "{ \"username\": \"" .. db_config.username .. "\", \"password\": \"" .. db_config.password .. "\" }"
}
)
if ok == 200 then
Jwt = DecodeJson(body)['jwt']
end
return Jwt
end
function Raw_aql(stm)
local body, status_code = Api_run('/cursor', 'POST', stm)
local result = body['result']
local has_more = body['hasMore']
local extra = body['extra']
if body['error'] then
print(status_code)
print(EncodeJson(stm))
print(EncodeJson(body))
end
while has_more do
body = Api_run("/cursor/" .. body["id"], 'PUT')
result = Table_merge(result, body['result'])
has_more = body['hasMore']
end
if result == nil then
result = {}
end
return EncodeJson({ result = result, extra = extra })
end
function Aql(str, bindvars, options)
bindvars = bindvars or {}
options = options or {}
return Raw_aql({ query = str, cache = true, bindVars = bindvars, options = options })
end
Auth(db_config)
--Redis_client = redis.connect('127.0.0.1', 6379)
function OnWorkerStart()
print("Creating worker")
Redis_client = redis.connect('127.0.0.1', 6379)
end
function OnWorkerStop()
print("Closing worker")
Redis_client.quit(Redis_client)
end
function OnServerStart()
-- ProgramMaxWorkers(5)
end
function OnHttpRequest()
if GetTime() - last_db_connect > 60 then
Auth(db_config)
last_db_connect = GetTime()
end
-- Route()
if GetMethod() == "GET" then
params = {}
-- GET /demo/:id
local parser = re.compile([[^/demo/([0-9]{1,3})$]])
local matcher, id = parser:search(GetPath())
if(matcher) then
params["id"] = id
RoutePath("/demo2.lua")
else
Route()
end
elseif GetMethod() == "POST" then
Route()
else
Route()
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment