1- local normalize = require " resty.etcd.path" .normalize
2- local typeof = require " resty.etcd.typeof"
31-- https://github.com/ledgetech/lua-resty-http
42local HttpCli = require " resty.http"
3+ local normalize = require " resty.etcd.path"
4+ local typeof = require " resty.etcd.typeof"
55local encode_args = ngx .encode_args
66local setmetatable = setmetatable
77local decode_json , encode_json
@@ -24,7 +24,7 @@ function _M.new(opts)
2424 elseif not typeof .table (opts ) then
2525 return nil , ' opts must be table'
2626 end
27-
27+
2828 local timeout = opts .timeout or 5000 -- 5 sec
2929 local http_host = opts .host or " http://127.0.0.1:2379"
3030 local ttl = opts .ttl or - 1
@@ -146,22 +146,22 @@ local function set(self, key, val, attr)
146146 -- ngx.log(ngx.WARN, require("cjson").encode(opts))
147147
148148 -- todo: check arguments
149-
149+
150150 -- verify key
151151 key = normalize (key )
152152 if key == ' /' then
153153 return nil , " key should not be a slash"
154154 end
155-
156- local res , err = _request (attr .inOrder and ' POST' or ' PUT' ,
157- self .endpoints .full_prefix .. key ,
155+
156+ local res , err = _request (attr .inOrder and ' POST' or ' PUT' ,
157+ self .endpoints .full_prefix .. key ,
158158 opts , self .timeout )
159159 if err then
160160 return nil , err
161161 end
162-
162+
163163 -- get
164- if res .status < 300 and res .body .node and
164+ if res .status < 300 and res .body .node and
165165 not res .body .node .dir then
166166 res .body .node .value , err = decode_json (res .body .node .value )
167167 if err then
@@ -185,7 +185,7 @@ local function get(self, key, attr)
185185 if attr .recursive then
186186 attr_recursive = attr .recursive and ' true' or ' false'
187187 end
188-
188+
189189 opts = {
190190 query = {
191191 wait = attr_wait ,
@@ -196,7 +196,7 @@ local function get(self, key, attr)
196196 }
197197 end
198198
199- local res , err = _request (" GET" ,
199+ local res , err = _request (" GET" ,
200200 self .endpoints .full_prefix .. normalize (key ),
201201 opts , attr and attr .timeout or self .timeout )
202202 if err then
@@ -206,7 +206,7 @@ local function get(self, key, attr)
206206 -- readdir
207207 if attr and attr .dir then
208208 -- set 404 not found if result node is not directory
209- if res .status == 200 and res .body .node and
209+ if res .status == 200 and res .body .node and
210210 not res .body .node .dir then
211211 res .status = 404
212212 res .body .node .dir = false
@@ -229,14 +229,14 @@ local function get(self, key, attr)
229229 ngx .log (ngx .WARN , " read addr: " , encode_json (res .body .node ))
230230
231231 -- get
232- elseif res .status == 200 and res .body .node and
232+ elseif res .status == 200 and res .body .node and
233233 not res .body .node .dir then
234234 res .body .node .value , err = decode_json (res .body .node .value )
235235 if err then
236236 return nil , err
237237 end
238238 end
239-
239+
240240 return res
241241end
242242
@@ -271,8 +271,8 @@ local function delete(self, key, attr)
271271
272272 -- todo: check arguments
273273
274- return _request (" DELETE" ,
275- self .endpoints .full_prefix .. normalize (key ),
274+ return _request (" DELETE" ,
275+ self .endpoints .full_prefix .. normalize (key ),
276276 opts , self .timeout )
277277end
278278
@@ -292,7 +292,7 @@ function _M.wait(self, key, modifiedIndex, timeout)
292292 attr .wait = true
293293 attr .waitIndex = modifiedIndex
294294 attr .timeout = timeout
295-
295+
296296 return get (self , key , attr )
297297end
298298
@@ -312,7 +312,7 @@ function _M.waitdir(self, key, modifiedIndex, timeout)
312312 attr .recursive = true
313313 attr .waitIndex = modifiedIndex
314314 attr .timeout = timeout
315-
315+
316316 return get (self , key , attr )
317317end
318318
0 commit comments