Library to interface with the consul HTTP API from ngx_lua
#Table of Contents
localresty_consul=require('resty.consul')
localconsul=resty_consul:new({
host=10.10.10.10,
port=8500
})
localok, err=consul:put('/kv/foobar', 'My key value!')
ifnotokthenngx.log(ngx.ERR, err)
endlocalok, err=consul:put('/kv/some_json', { msg='This will be json encoded'})
ifnotokthenngx.log(ngx.ERR, err)
endlocalres,err=consul:get('/kv/foobar')
ifnotresthenngx.log(ngx.ERR, err)
endngx.say(res.Value) -- Prints "TXkga2V5IHZhbHVlIQo="localres,err=consul:get_decoded('/kv/foobar')
ifnotresthenngx.log(ngx.ERR, err)
endngx.say(res.Value) -- Prints "My key value!"localres, err=consul:get_json_decoded('/kv/some_json')
ifnotresthenngx.log(ngx.ERR, err)
endiftype(res.Value) =='table' thenngx.say(res.Value.msg) -- Prints "This will be json encoded"elsengx.log(ngx.ERR, "Failed to decode value :(")
endsyntax: client = consul:new(opts?)
Create a new consul client. opts is a table setting the following options:
hostDefaults to 127.0.0.1portDefaults to 8500connect_timeoutConnection timeout in ms. Defaults to 60sread_timeoutRead timeout in ms. Defaults to 60s
syntax: res, headers = consul:get(key, opts?)
Performs a GET request against the provided key. API Version is automatically prepended.
e.g. to get the value of a key at http://my.consul.server/v1/kv/foobar you would call consul:get('/kv/foobar')
opts is hash of query string parameters to add to the URI.
The wait query string param is a special case, it must be passed in as an number not as a string with 's' appended.
Returns a table representing the response from Consul and a second table of the Consul specific headers X-Consul-Lastcontact, X-Consul-KnownLeader and X-Consul-Index.
On error returns nil and an error message.
syntax: res, headers = consul:get_decoded(key, opts?)
Wrapper on the get method, but performs base64 decode on the value field in the Consul response
syntax: res, headers = consul:get_json_decoded(key, opts?)
Wrapper on the get method, but performs base64 decode on the value field in the Consul response and then attempts to parse the value as json.
syntax: res, err = consul:put(key, value, opts?)
Performs a PUT request against the provided key with provided value. API Version is automatically prepended.
opts is hash of query string parameters to add to the URI.
If value is a table or boolean value it is automatically json encoded before being sent.
Otherwise anything that lua-resty-http accepts as a body input is valid.
On a 200 response returns the response body if there is one or a boolean true.
On a non-200 response returns nil and the response body.
syntax: ok, err = consul:delete(key, value, recurse?)
Performs a DELETE request against the provided key.
recurse defaults to false.
Returns a boolean true if the response is 200.
Otherwise a table containing status, body and headers as well as the error from lua-resty-http
Proxy method to lua-resty-http