1- local class = require " middleclass"
1+ local cjson = require " cjson"
2+ local class = require " middleclass"
23local datastore = require " bunkerweb.datastore"
3- local utils = require " bunkerweb.utils"
4- local logger = require " bunkerweb.logger"
5- local cjson = require " cjson"
6- local upload = require " resty.upload"
7- local rsignal = require " resty.signal"
8- local process = require " ngx.process"
4+ local logger = require " bunkerweb.logger"
5+ local process = require " ngx.process"
6+ local rsignal = require " resty.signal"
7+ local upload = require " resty.upload"
8+ local utils = require " bunkerweb.utils"
99
10- local api = class (" api" )
10+ local api = class (" api" )
1111
12- api .global = { GET = {}, POST = {}, PUT = {}, DELETE = {} }
12+ api .global = { GET = {}, POST = {}, PUT = {}, DELETE = {} }
1313
1414function api :initialize ()
1515 self .datastore = datastore :new ()
@@ -26,21 +26,23 @@ function api:initialize()
2626 end
2727end
2828
29+ -- luacheck: ignore 212
2930function api :log_cmd (cmd , status , stdout , stderr )
3031 local level = ngx .NOTICE
3132 local prefix = " success"
3233 if status ~= 0 then
3334 level = ngx .ERR
3435 prefix = " error"
3536 end
36- self .logger :log (level , prefix .. " while running command " .. command )
37+ self .logger :log (level , prefix .. " while running command " .. cmd )
3738 self .logger :log (level , " stdout = " .. stdout )
3839 self .logger :log (level , " stdout = " .. stderr )
3940end
4041
4142-- TODO : use this if we switch to OpenResty
4243function api :cmd (cmd )
4344 -- Non-blocking command
45+ -- luacheck: ignore 113
4446 local ok , stdout , stderr , reason , status = shell .run (cmd , nil , 10000 )
4547 self .logger :log_cmd (cmd , status , stdout , stderr )
4648 -- Timeout
@@ -51,6 +53,7 @@ function api:cmd(cmd)
5153 return status == 0 , reason , status
5254end
5355
56+ -- luacheck: ignore 212
5457function api :response (http_status , api_status , msg )
5558 local resp = {}
5659 resp [" status" ] = api_status
@@ -101,6 +104,7 @@ api.global.POST["^/confs$"] = function(self)
101104 form :set_timeout (1000 )
102105 local file = io.open (tmp , " w+" )
103106 while true do
107+ -- luacheck: ignore 421
104108 local typ , res , err = form :read ()
105109 if not typ then
106110 file :close ()
@@ -117,9 +121,9 @@ api.global.POST["^/confs$"] = function(self)
117121 file :close ()
118122 local cmds = {
119123 " rm -rf " .. destination .. " /*" ,
120- " tar xzf " .. tmp .. " -C " .. destination
124+ " tar xzf " .. tmp .. " -C " .. destination ,
121125 }
122- for i , cmd in ipairs (cmds ) do
126+ for _ , cmd in ipairs (cmds ) do
123127 local status = os.execute (cmd )
124128 if status ~= 0 then
125129 return self :response (ngx .HTTP_INTERNAL_SERVER_ERROR , " error" , " exit status = " .. tostring (status ))
@@ -176,17 +180,23 @@ end
176180
177181api .global .GET [" ^/bans$" ] = function (self )
178182 local data = {}
179- for i , k in ipairs (self .datastore :keys ()) do
183+ for _ , k in ipairs (self .datastore :keys ()) do
180184 if k :find (" ^bans_ip_" ) then
181185 local reason , err = self .datastore :get (k )
182186 if err then
183- return self :response (ngx .HTTP_INTERNAL_SERVER_ERROR , " error" ,
184- " can't access " .. k .. " from datastore : " .. reason )
187+ return self :response (
188+ ngx .HTTP_INTERNAL_SERVER_ERROR ,
189+ " error" ,
190+ " can't access " .. k .. " from datastore : " .. reason
191+ )
185192 end
186193 local ok , ttl = self .datastore :ttl (k )
187194 if not ok then
188- return self :response (ngx .HTTP_INTERNAL_SERVER_ERROR , " error" ,
189- " can't access ttl " .. k .. " from datastore : " .. ttl )
195+ return self :response (
196+ ngx .HTTP_INTERNAL_SERVER_ERROR ,
197+ " error" ,
198+ " can't access ttl " .. k .. " from datastore : " .. ttl
199+ )
190200 end
191201 local ban = { ip = k :sub (9 , # k ), reason = reason , exp = math.floor (ttl ) }
192202 table.insert (data , ban )
@@ -196,7 +206,7 @@ api.global.GET["^/bans$"] = function(self)
196206end
197207
198208api .global .GET [" ^/variables$" ] = function (self )
199- local variables , err = datastore :get (' variables' , true )
209+ local variables , err = datastore :get (" variables" , true )
200210 if not variables then
201211 return self :response (ngx .HTTP_INTERNAL_SERVER_ERROR , " error" , " can't access variables from datastore : " .. err )
202212 end
@@ -219,9 +229,9 @@ function api:do_api_call()
219229 if status ~= ngx .HTTP_OK then
220230 ret = false
221231 end
222- if ( # resp [" msg" ] == 0 ) then
232+ if # resp [" msg" ] == 0 then
223233 resp [" msg" ] = " "
224- elseif ( type (resp [" msg" ]) == " table" ) then
234+ elseif type (resp [" msg" ]) == " table" then
225235 resp [" data" ] = resp [" msg" ]
226236 resp [" msg" ] = resp [" status" ]
227237 end
@@ -231,10 +241,10 @@ function api:do_api_call()
231241 end
232242 local list , err = self .datastore :get (" plugins" , true )
233243 if not list then
234- local status , resp = self :response (ngx .HTTP_INTERNAL_SERVER_ERROR , " error" , " can't list loaded plugins : " .. err )
244+ local _ , resp = self :response (ngx .HTTP_INTERNAL_SERVER_ERROR , " error" , " can't list loaded plugins : " .. err )
235245 return false , resp [" msg" ], ngx .HTTP_INTERNAL_SERVER_ERROR , cjson .encode (resp )
236246 end
237- for i , plugin in ipairs (list ) do
247+ for _ , plugin in ipairs (list ) do
238248 if pcall (require , plugin .id .. " /" .. plugin .id ) then
239249 local plugin_lua = require (plugin .id .. " /" .. plugin .id )
240250 if plugin_lua .api ~= nil then
0 commit comments