1
0
mirror of https://github.com/lxsang/antd-web-apps synced 2024-11-19 18:08:21 +01:00
antd-web-apps/silk/BaseController.lua

108 lines
3.0 KiB
Lua
Raw Permalink Normal View History

2018-08-21 15:16:36 +02:00
-- create class
2018-08-27 20:10:53 +02:00
BaseObject:subclass("BaseController",
{
registry = {},
models = {},
main = false
})
2018-08-21 15:16:36 +02:00
-- set the name here in each subclasses
function BaseController:initialize()
2018-08-22 19:38:36 +02:00
for k, v in pairs(self.models) do
--- infer the class here
local modelname = firstToUpper(v).."Model"
local path = MODEL_ROOT.."."..modelname
-- require it
pcall(require, path)
--require(controller_path)
if not _G[modelname] then
self:modelnotfound(v)
else
-- create new model object
self[v] = _G[modelname]:new{registry = self.registry}
end
end
-- create template
self.template = Template:new{registry = self.registry}
2018-08-21 15:16:36 +02:00
end
2018-08-27 20:10:53 +02:00
function BaseController:print(...)
return self:actionnotfound("print")
end
2018-08-22 19:38:36 +02:00
function BaseController:redirect(url)
std.status(301, "Moved Permanently")
std.custom_header("Content-Type","text/html")
std.custom_header("Location", url)
std.header_flush()
2018-08-21 15:16:36 +02:00
end
function BaseController:switchLayout(name)
if self.main then
self.registry.layout = name
else
self:log("Cannot switch layout since the controller "..self.class.." is not the main controller")
end
2018-08-22 19:50:52 +02:00
end
2018-08-22 19:38:36 +02:00
function BaseController:setSession(key, value)
SESSION[key] = value
2018-08-21 15:16:36 +02:00
end
function BaseController:getSession(key)
2018-08-22 19:38:36 +02:00
return SESSION[key]
2018-08-21 15:16:36 +02:00
end
function BaseController:removeSession(key)
2018-08-22 19:38:36 +02:00
self:setSession(key, nil)
end
function BaseController:index(...)
self:error("#index: subclasses responsibility")
end
-- not found action
function BaseController:actionnotfound(...)
local args = {...}
self:error("#action "..args[1].." is not found in controller "..self.class)
end
-- not found model
function BaseController:modelnotfound(...)
local args = {...}
self:error("Model "..firstToUpper(args[1]).."Model is not found in controller "..self.class)
end
-- The not found controller
2018-08-27 20:10:53 +02:00
BaseController:subclass("NotfoundController",{ registry = {}, models = {} })
2018-08-22 19:38:36 +02:00
function NotfoundController:index(...)
local args = {...}
local error = args[2] or ""
2018-09-05 16:56:04 +02:00
if self.template:path() then
self.template:set("error", error)
self.template:set("title", "404 not found")
return true
end
self:error("404: Controller "..args[1].." not found : "..error)
2018-08-22 19:38:36 +02:00
return false
end
-- The asset controller for the static file
2018-08-27 20:10:53 +02:00
BaseController:subclass("AssetController", {registry={}, models={}})
2018-08-22 19:38:36 +02:00
function AssetController:index(...)
local args = {...}
return self:get(table.unpack(args))
2018-08-21 15:16:36 +02:00
end
2018-08-22 19:38:36 +02:00
function AssetController:get(...)
local path = WWW_ROOT..DIR_SEP..implode({...}, DIR_SEP)
2018-09-12 18:30:53 +02:00
if self.registry.fileaccess and ulib.exists(path) then
local mime = std.mimeOf(path)
if POLICY.mimes[mime] then
2019-12-04 19:51:26 +01:00
std.sendFile(path)
else
2018-08-28 10:24:35 +02:00
self:error("Access forbidden: "..path)
2018-08-22 19:38:36 +02:00
end
else
2018-09-12 18:30:53 +02:00
self:error("Asset file not found or access forbidden: "..path)
2018-08-22 19:38:36 +02:00
end
return false
2018-08-21 15:16:36 +02:00
end