Skip to content

Instantly share code, notes, and snippets.

@CandyMi
Last active October 26, 2020 07:47
Show Gist options
  • Save CandyMi/ca218fc39d7538fe7071667fd939a9c4 to your computer and use it in GitHub Desktop.
Save CandyMi/ca218fc39d7538fe7071667fd939a9c4 to your computer and use it in GitHub Desktop.
Lua consul SDK implementation
--[[
LICENSE: BSD
Author: CandyMi[https://github.com/candymi]
]]
local type = type
local assert = assert
local httpc = require "httpc"
local json = require "json"
local class = require "class"
local consul = class("consul-cli")
function consul:ctor (opt)
if type(opt) ~= 'table' then
opt = { debug = false }
end
self.domain = assert(opt.domain, "consul domain required! Like `http://localhost:8500/`")
self.debug = opt.debug
self.timeout = opt.timeout or 15
end
-- 获取kv内所有的key
function consul:getkvs ()
local code, ret = httpc.get(self.domain .. "/v1/kv?keys", self.timeout)
if code ~= 200 then
return nil, "Connect consul failed."
end
if self.debug then
print(ret)
end
local response = json.decode(ret)
if type(response) ~= 'table' then
return nil, "Invalid return content."
end
return response
end
-- 获取指定key的内容
function consul:getkv (key)
local code, ret = httpc.get(self.domain .. "/v1/kv/" .. (key or ""), {}, {}, self.timeout)
if not code then
return nil, "Connect consul failed."
end
if code == 404 then
return nil, "Can't find this key-value."
end
if self.debug then
print(ret)
end
local response = json.decode(ret)
if type(response) ~= 'table' then
return nil, "Invalid return content."
end
return response
end
-- 增加或修改指定key的内容
function consul:setkv (key, value)
local code, ret = httpc.put(self.domain .. "/v1/kv/" .. (key or ""), {{"Content-Type", "application/json"}}, json.encode(value), self.timeout)
if self.debug then
print(ret)
end
if code == 200 and (ret == "true\r\n" or ret == "true\n") then
return true
end
return false, ret
end
-- 删除指定key
function consul:unsetkv (key)
local code, ret = httpc.delete(self.domain .. "/v1/kv/" .. (key or ""), {}, {}, self.timeout)
if self.debug then
print(ret)
end
if code == 200 and (ret == "true\r\n" or ret == "true\n") then
return true
end
return false, ret
end
-- 获取所有配置
function consul:getconfigs (kind)
local code, ret = httpc.get(self.domain .. "/v1/config/" .. (kind or ""), {}, {}, self.timeout)
if not code then
return nil, "Connect consul failed."
end
if code == 404 then
return nil, "Can't find config list."
end
if self.debug then
print(ret)
end
local response = json.decode(ret)
if type(response) ~= 'table' then
return nil, "Invalid return content."
end
return response
end
-- 获取指定配置
function consul:getconfig (kind, name)
local code, ret = httpc.get(self.domain .. "/v1/config/" ..
assert(type(kind) == 'string' and kind ~= '' and (kind .. "/"), "Invalid kind.") ..
assert(type(name) == 'string' and name ~= '' and (name) , "Invalid name."),
{}, {}, self.timeout
)
if not code then
return nil, "Connect consul failed."
end
if code == 404 then
return nil, "Can't find config : " .. ( ret or "config unknown.")
end
if self.debug then
print(ret)
end
local response = json.decode(ret)
if type(response) ~= 'table' then
return nil, "Invalid return content."
end
-- print(code, ret)
return response
end
-- 增加或修改指定配置
function consul:setconfig (opt)
local code, ret = httpc.put(self.domain .. "/v1/config", {{"Content-Type", "application/json"}}, json.encode(opt), self.timeout)
if self.debug then
print(ret)
end
if code == 200 and (ret == "true\r\n" or ret == "true\n") then
return true
end
return false, ret
end
-- 删除指定配置
function consul:unsetconfig (kind, name)
local code, ret = httpc.delete(self.domain .. "/v1/config/" ..
assert(type(kind) == 'string' and kind ~= '' and (kind .. "/"), "Invalid kind.") ..
assert(type(name) == 'string' and name ~= '' and (name) , "Invalid name."),
{}, {}, self.timeout
)
local response = json.decode(ret)
if code == 200 and type(response) == 'table' then
return response
end
return false, ret
end
return consul
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment