Skip to content

Commit 9ccdde6

Browse files
committed
Move http server to metatable
1 parent 33aac0b commit 9ccdde6

File tree

1 file changed

+63
-55
lines changed

1 file changed

+63
-55
lines changed

http/server.lua

Lines changed: 63 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ local function sprintf(fmt, ...)
2323
return string.format(fmt, ...)
2424
end
2525

26+
local function assertf(ok, fmt, ...)
27+
if not ok then
28+
fmt = tostring(fmt)
29+
if select('#', ...) > 0 then fmt = fmt:format(...) end
30+
error(fmt, 2)
31+
end
32+
return ok
33+
end
34+
2635
local function uri_escape(str)
2736
local res = {}
2837
if type(str) == 'table' then
@@ -1149,62 +1158,61 @@ local function httpd_start(self)
11491158
return self
11501159
end
11511160

1152-
local exports = {
1153-
DETACHED = DETACHED,
1154-
1155-
new = function(host, port, options)
1156-
if options == nil then
1157-
options = {}
1158-
end
1159-
if type(options) ~= 'table' then
1160-
errorf("options must be table not '%s'", type(options))
1161-
end
1162-
local default = {
1163-
max_header_size = 4096,
1164-
header_timeout = 100,
1165-
handler = handler,
1166-
app_dir = '.',
1167-
charset = 'utf-8',
1168-
cache_templates = true,
1169-
cache_controllers = true,
1170-
cache_static = true,
1171-
log_requests = true,
1172-
log_errors = true,
1173-
display_errors = true,
1174-
}
1161+
local http_server_methods = {
1162+
stop = httpd_stop,
1163+
start = httpd_start,
1164+
route = add_route,
1165+
match = match_route,
1166+
helper = set_helper,
1167+
hook = set_hook,
1168+
url_for = url_for_httpd,
1169+
}
11751170

1176-
local self = {
1177-
host = host,
1178-
port = port,
1179-
is_run = false,
1180-
stop = httpd_stop,
1181-
start = httpd_start,
1182-
options = extend(default, options, true),
1183-
1184-
routes = { },
1185-
iroutes = { },
1186-
helpers = {
1187-
url_for = url_for_helper,
1188-
},
1189-
hooks = { },
1190-
1191-
-- methods
1192-
route = add_route,
1193-
match = match_route,
1194-
helper = set_helper,
1195-
hook = set_hook,
1196-
url_for = url_for_httpd,
1197-
1198-
-- caches
1199-
cache = {
1200-
tpl = {},
1201-
ctx = {},
1202-
static = {},
1203-
},
1204-
}
1171+
local http_server_mt = {
1172+
__index = http_server_methods
1173+
}
12051174

1206-
return self
1207-
end
1175+
local http_server_options_default = {
1176+
max_header_size = 4096,
1177+
header_timeout = 100,
1178+
handler = handler,
1179+
app_dir = '.',
1180+
charset = 'utf-8',
1181+
cache_templates = true,
1182+
cache_controllers = true,
1183+
cache_static = true,
1184+
log_requests = true,
1185+
log_errors = true,
1186+
display_errors = true,
12081187
}
12091188

1210-
return exports
1189+
local function http_server_new(host, port, options)
1190+
options = options or {}
1191+
local opts_tp = type(options)
1192+
assertf(opts_tp == 'table', "options must be table, not '%s'", opts_tp)
1193+
1194+
-- populate options table with default values
1195+
options = extend(table.copy(http_server_options_default), options, true)
1196+
1197+
local self = setmetatable({
1198+
host = host,
1199+
port = port,
1200+
is_run = false,
1201+
options = options,
1202+
1203+
routes = { },
1204+
iroutes = { },
1205+
helpers = { url_for = url_for_helper, },
1206+
hooks = { },
1207+
1208+
-- caches
1209+
cache = { tpl = {}, ctx = {}, static = {}, },
1210+
}, http_server_mt)
1211+
1212+
return self
1213+
end
1214+
1215+
return {
1216+
DETACHED = DETACHED,
1217+
new = http_server_new
1218+
}

0 commit comments

Comments
 (0)