1
0
mirror of https://github.com/lxsang/antd-web-apps synced 2025-07-17 06:09:50 +02:00

blog now use silk

This commit is contained in:
Xuan Sang LE
2018-09-05 16:56:04 +02:00
parent 1f57e18fc7
commit 914bff3498
28 changed files with 419 additions and 509 deletions

View File

@ -75,6 +75,11 @@ BaseController:subclass("NotfoundController",{ registry = {}, models = {} })
function NotfoundController:index(...)
local args = {...}
local error = args[2] or ""
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)
return false
end
@ -102,7 +107,7 @@ function AssetController:get(...)
self:error("Access forbidden: "..path)
end
else
self:error("Assset file not found: "..path)
self:error("Asset file not found: "..path)
end
return false
end

View File

@ -38,6 +38,12 @@ function BaseModel:find(cond)
return false
end
function BaseModel:get(id)
local data, order = self:find({exp = {["="] = { id = id}} })
if not data or #order == 0 then return false end
return data[1]
end
function BaseModel:findAll()
if self.db then
return self.db:getAll(self.name)

View File

@ -6,6 +6,7 @@ end
function Router:initialize()
self.routes = {}
self.remaps = {}
end
--function Router:setArgs(args)
@ -23,7 +24,7 @@ function Router:infer(url)
-- c,d,e is parameters
-- if user dont provide the url, try to infer it
-- from the REQUEST
url = url or REQUEST.query.r
url = url or REQUEST.query.r or ""
url = std.trim(url, "/")
local args = explode(url, "/")
local data = {
@ -41,6 +42,10 @@ function Router:infer(url)
end
end
-- remap if needed
if self.remaps[data.name] ~= nil then
data.name = self.remaps[data.name]
end
-- find the controller class and init it
local controller_name = firstToUpper(data.name) .. "Controller"
local controller_path = self.path .. "." .. controller_name
@ -84,7 +89,7 @@ function Router:delegate()
data.controller.main = true
views.__main__ = self:call(data)
if not views.__main__ then
--self:error("No main template is set")
--self:error("No view available for this action")
return
end
-- get all visible routes
@ -94,16 +99,23 @@ function Router:delegate()
views[k] = self:call(data)
end
-- now require the main page to put the view
local view_args = {}
local view_argv = {}
for k,v in pairs(views) do
table.insert( view_args, k )
table.insert( view_argv, v )
end
local fn, e = loadscript(VIEW_ROOT .. DIR_SEP .. self.registry.layout .. DIR_SEP .. "layout.ls")
local fn, e = loadscript(VIEW_ROOT .. DIR_SEP .. self.registry.layout .. DIR_SEP .. "layout.ls", view_args)
html()
if fn then
local r, o = pcall(fn, views)
local r, o = pcall(fn, table.unpack(view_argv))
if not r then
self:error(o)
end
else
self:error("The index page is not found for layout: " .. self.registry.layout)
e = e or ""
self:error("The index page is not found for layout: " .. self.registry.layout..": "..e)
end
end
@ -142,6 +154,10 @@ function Router:call(data)
end
end
function Router:remap(from, to)
self.remaps[from] = to
end
function Router:route(layout, dependencies)
self.routes[layout] = dependencies
end

View File

@ -31,14 +31,24 @@ function Template:path()
if ulib.exists(path) then
return path
else
self:error("View not found: "..path)
return false, path
end
end
-- render the page
function Template:render()
local fn, e = loadscript(self:path())
local path, err = self:path()
if not path then
return self:error("View not found: "..err)
end
local args = {}
local argv = {}
for k, v in pairs(self.vars) do
table.insert( args, k )
table.insert( argv,v )
end
local fn, e = loadscript(self:path(), args)
if fn then
local r,o = pcall(fn, self.vars)
local r,o = pcall(fn, table.unpack(argv))
if not r then
self:error(o)
end

View File

@ -16,6 +16,7 @@ POLICY.mimes = {
["application/javascript"] = true,
["image/bmp"] = true,
["image/jpeg"] = true,
["image/png"] = true,
["text/css"] = true,
["text/markdown"] = true,
["text/csv"] = true,
@ -49,4 +50,8 @@ function html()
std.chtml(SESSION)
HEADER_FLAG = true
end
end
function import(module)
return require(BASE_FRW.."silk.api."..module)
end