Skip to content

Commit 77bfe26

Browse files
committed
Add StyLua and luacheck to precommit config file and apply it
1 parent cd1f87b commit 77bfe26

29 files changed

Lines changed: 1429 additions & 1221 deletions

File tree

.luacheckrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
globals = {"ngx", "delay", "unpack"}
2+
ignore = {"411"}

.pre-commit-config.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@ repos:
2626
- id: prettier
2727
name: Prettier Code Formatter
2828

29+
- repo: https://github.com/JohnnyMorganz/StyLua
30+
rev: 27e6b388796604181e810ef05c9fb15a9f7a7769 # frozen: v0.18.2
31+
hooks:
32+
- id: stylua-github
33+
exclude: ^src/(bw/lua/middleclass.lua|common/core/antibot/captcha.lua)$
34+
35+
- repo: https://github.com/lunarmodules/luacheck
36+
rev: ababb6d403d634eb74d2c541035e9ede966e710d # frozen: v1.1.1
37+
hooks:
38+
- id: luacheck
39+
exclude: ^src/(bw/lua/middleclass.lua|common/core/antibot/captcha.lua)$
40+
args: ["--std", "min", "--codes", "--ranges", "--no-cache"]
41+
2942
- repo: https://github.com/pycqa/flake8
3043
rev: 10f4af6dbcf93456ba7df762278ae61ba3120dc6 # frozen: 6.1.0
3144
hooks:

src/bw/lua/bunkerweb/api.lua

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
local class = require "middleclass"
1+
local cjson = require "cjson"
2+
local class = require "middleclass"
23
local 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

1414
function api:initialize()
1515
self.datastore = datastore:new()
@@ -26,21 +26,23 @@ function api:initialize()
2626
end
2727
end
2828

29+
-- luacheck: ignore 212
2930
function 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)
3940
end
4041

4142
-- TODO : use this if we switch to OpenResty
4243
function 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
5254
end
5355

56+
-- luacheck: ignore 212
5457
function 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

177181
api.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)
196206
end
197207

198208
api.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

src/bw/lua/bunkerweb/cachestore.lua

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,38 @@
1-
local mlcache = require "resty.mlcache"
1+
local class = require "middleclass"
22
local clusterstore = require "bunkerweb.clusterstore"
3-
local logger = require "bunkerweb.logger"
4-
local utils = require "bunkerweb.utils"
5-
local class = require "middleclass"
6-
local cachestore = class("cachestore")
3+
local logger = require "bunkerweb.logger"
4+
local mlcache = require "resty.mlcache"
5+
local utils = require "bunkerweb.utils"
6+
local cachestore = class("cachestore")
77

88
-- Instantiate mlcache object at module level (which will be cached when running init phase)
99
-- TODO : custom settings
10-
local shm = "cachestore"
11-
local ipc_shm = "cachestore_ipc"
12-
local shm_miss = "cachestore_miss"
13-
local shm_locks = "cachestore_locks"
10+
local shm = "cachestore"
11+
local ipc_shm = "cachestore_ipc"
12+
local shm_miss = "cachestore_miss"
13+
local shm_locks = "cachestore_locks"
1414
if not ngx.shared.cachestore then
15-
shm = "cachestore_stream"
16-
ipc_shm = "cachestore_ipc_stream"
17-
shm_miss = "cachestore_miss_stream"
15+
shm = "cachestore_stream"
16+
ipc_shm = "cachestore_ipc_stream"
17+
shm_miss = "cachestore_miss_stream"
1818
shm_locks = "cachestore_locks_stream"
1919
end
20-
local cache, err = mlcache.new(
21-
"cachestore",
22-
shm,
23-
{
24-
lru_size = 100,
25-
ttl = 30,
26-
neg_ttl = 0.1,
27-
shm_set_tries = 3,
28-
shm_miss = shm_miss,
29-
shm_locks = shm_locks,
30-
resty_lock_opts = {
31-
exptime = 30,
32-
timeout = 5,
33-
step = 0.001,
34-
ratio = 2,
35-
max_step = 0.5
36-
},
37-
ipc_shm = ipc_shm
38-
}
39-
)
20+
local cache, err = mlcache.new("cachestore", shm, {
21+
lru_size = 100,
22+
ttl = 30,
23+
neg_ttl = 0.1,
24+
shm_set_tries = 3,
25+
shm_miss = shm_miss,
26+
shm_locks = shm_locks,
27+
resty_lock_opts = {
28+
exptime = 30,
29+
timeout = 5,
30+
step = 0.001,
31+
ratio = 2,
32+
max_step = 0.5,
33+
},
34+
ipc_shm = ipc_shm,
35+
})
4036
local module_logger = logger:new("CACHESTORE")
4137
if not cache then
4238
module_logger:log(ngx.ERR, "can't instantiate mlcache : " .. err)
@@ -57,10 +53,12 @@ function cachestore:initialize(use_redis, new_cs, ctx)
5753
end
5854

5955
function cachestore:get(key)
56+
-- luacheck: ignore 432
6057
local callback = function(key, cs)
6158
-- Connect to redis
59+
-- luacheck: ignore 431
6260
local clusterstore = cs or require "bunkerweb.clusterstore":new(false)
63-
local ok, err, reused = clusterstore:connect()
61+
local ok, err, _ = clusterstore:connect()
6462
if not ok then
6563
return nil, "can't connect to redis : " .. err, nil
6664
end
@@ -96,6 +94,7 @@ function cachestore:get(key)
9694
local callback_no_miss = function()
9795
return nil, nil, -1
9896
end
97+
-- luacheck: ignore 431
9998
local value, err, hit_level
10099
if self.use_redis and utils.is_cosocket_available() then
101100
local cs = nil
@@ -114,13 +113,14 @@ function cachestore:get(key)
114113
end
115114

116115
function cachestore:set(key, value, ex)
116+
-- luacheck: ignore 431
117+
local ok, err
117118
if self.use_redis and utils.is_cosocket_available() then
118-
local ok, err = self:set_redis(key, value, ex)
119+
ok, err = self:set_redis(key, value, ex)
119120
if not ok then
120121
self.logger:log(ngx.ERR, err)
121122
end
122123
end
123-
local ok, err
124124
if ex then
125125
ok, err = self.cache:set(key, { ttl = ex }, value)
126126
else
@@ -134,13 +134,14 @@ end
134134

135135
function cachestore:set_redis(key, value, ex)
136136
-- Connect to redis
137-
local ok, err, reused = self.clusterstore:connect()
137+
-- luacheck: ignore 431
138+
local ok, err, _ = self.clusterstore:connect()
138139
if not ok then
139140
return false, "can't connect to redis : " .. err
140141
end
141142
-- Set value with ttl
142143
local default_ex = ex or 30
143-
local ok, err = self.clusterstore:call("set", key, value, "EX", default_ex)
144+
local _, err = self.clusterstore:call("set", key, value, "EX", default_ex)
144145
if err then
145146
self.clusterstore:close()
146147
return false, "SET failed : " .. err
@@ -149,14 +150,16 @@ function cachestore:set_redis(key, value, ex)
149150
return true
150151
end
151152

152-
function cachestore:delete(key, value, ex)
153+
function cachestore:delete(key)
154+
-- luacheck: ignore 431
155+
local ok, err
153156
if self.use_redis and utils.is_cosocket_available() then
154-
local ok, err = self.del_redis(key)
157+
ok, err = self:del_redis(key)
155158
if not ok then
156159
self.logger:log(ngx.ERR, err)
157160
end
158161
end
159-
local ok, err = self.cache:delete(key)
162+
ok, err = self.cache:delete(key)
160163
if not ok then
161164
return false, err
162165
end
@@ -165,12 +168,13 @@ end
165168

166169
function cachestore:del_redis(key)
167170
-- Connect to redis
171+
-- luacheck: ignore 431
168172
local ok, err = self.clusterstore:connect()
169173
if not ok then
170174
return false, "can't connect to redis : " .. err
171175
end
172176
-- Set value with ttl
173-
local ok, err = self.clusterstore:del(key)
177+
local _, err = self.clusterstore:del(key)
174178
if err then
175179
self.clusterstore:close()
176180
return false, "DEL failed : " .. err

0 commit comments

Comments
 (0)