-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubapp.lua
64 lines (59 loc) · 1.87 KB
/
subapp.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
local lapis = require("lapis")
local app = lapis.Application()
local respond_to = require("lapis.application").respond_to
function app:include(app_path)
local subapp = assert(require(app_path))
if not type(subapp) == "table" then
error("Unable to include sub-app '" .. app_path .. "', table expected got " .. type(subapp))
end
if not subapp.name or not subapp.path then
error("Sub-app must have a name and path")
end
for route,action in pairs(subapp.routes) do
for k,v in pairs(route) do
self:match(subapp.name .. "_" .. k, subapp.path .. v, respond_to({
before = function(self)
if subapp.before_filter then
before = subapp.before_filter(self)
end
if action.before then
return action.before(self)
end
end,
GET = function(self)
if action.GET then
local result = action.GET(self)
if result.render ~= nil then
if type(result.render) == "boolean" and result.render == true then
result.render = subapp.name .. "." .. k
end
end
if result.layout == nil then
if subapp.layout then
result.layout = subapp.layout
end
end
return result
end
end,
POST = function(self)
if action.POST then
local result = action.POST(self)
if result.render ~= nil then
if type(result.render) == "boolean" and result.render == true then
result.render = subapp.name .. "." .. k
end
end
if result.layout == nil then
if subapp.layout then
result.layout = subapp.layout
end
end
return result
end
end,
}))
end
end
end
return app