1
0
mirror of https://github.com/lxsang/antd-web-apps synced 2024-11-20 02:18:20 +01:00

using silk as base framework for all webapp

This commit is contained in:
Xuan Sang LE 2018-08-23 11:15:08 +02:00
parent dd778713d3
commit b07da22b52
13 changed files with 110 additions and 81 deletions

View File

@ -6,6 +6,8 @@ main: clean copy
copy: copy:
cp -rf $(copyfiles) $(BUILDDIR) cp -rf $(copyfiles) $(BUILDDIR)
cp -r silk $(BUILDDIR)
clean: clean:
-for f in $(projs); do rm -r $(BUILDDIR)/"$${f}"; done -for f in $(projs); do rm -r $(BUILDDIR)/"$${f}"; done
-rm -r $(BUILDDIR)/silk

View File

@ -6,9 +6,8 @@ PostController = BaseController:extends{
function PostController:index(...) function PostController:index(...)
local args = {...} local args = {...}
self.template:set("index", args[1])
self.template:set("dummy", "This is a dummy string")
self:setSession("postsession", "Huehuehue") self:setSession("postsession", "Huehuehue")
self.template:set("post", self.post:findAll())
return true return true
end end
@ -18,8 +17,7 @@ function PostController:edit(...)
else else
self.template:set("auth", false) self.template:set("auth", false)
end end
self:setLayout("admin") self:switchLayout("admin")
--self:redirect("/category/put/1")
return true return true
end end

View File

@ -6,12 +6,14 @@
DIR_SEP = "/" DIR_SEP = "/"
WWW_ROOT = "/opt/www/htdocs/apps" WWW_ROOT = "/opt/www/htdocs/apps"
HTTP_ROOT = "https://apps.localhost:9195/" HTTP_ROOT = "https://apps.localhost:9195/"
BASE_FRW = "apps." -- class path: path.to.class
LOG_ROOT = WWW_ROOT..DIR_SEP.."logs" BASE_FRW = ""
CONTROLLER_ROOT = BASE_FRW.."controllers" -- class path: path.to.class
MODEL_ROOT = BASE_FRW.."models" CONTROLLER_ROOT = BASE_FRW.."apps.controllers"
MODEL_ROOT = BASE_FRW.."apps.models"
-- file path: path/to/file
VIEW_ROOT = WWW_ROOT..DIR_SEP.."views" VIEW_ROOT = WWW_ROOT..DIR_SEP.."views"
LOG_ROOT = WWW_ROOT..DIR_SEP.."logs"
-- require needed library -- require needed library
require(BASE_FRW.."silk.api") require(BASE_FRW.."silk.api")
@ -26,49 +28,30 @@ REGISTRY.logger = Logger:new{ levels = {INFO = true, ERROR = true, DEBUG = true}
REGISTRY.db = DBHelper:new{db="iosapps"} REGISTRY.db = DBHelper:new{db="iosapps"}
REGISTRY.layout = 'default' REGISTRY.layout = 'default'
-- mime type allows
-- this will bypass the default server security
-- the default list is from the server setting
REGISTRY.mimes = {
["application/javascript"] = true,
["image/bmp"] = true,
["image/jpeg"] = true,
["text/css"] = true,
["text/markdown"] = true,
["text/csv"] = true,
["application/pdf"] = true,
["image/gif"] = true,
["text/html"] = true,
["application/json"] = true,
["application/javascript"] = true,
["image/x-portable-pixmap"] = true,
["application/x-rar-compressed"] = true,
["image/tiff"] = true,
["application/x-tar"] = true,
["text/plain"] = true,
["application/x-font-ttf"] = true,
["application/xhtml+xml"] = true,
["application/xml"] = true,
["application/zip"] = true,
["image/svg+xml"] = true,
["application/vnd.ms-fontobject"] = true,
["application/x-font-woff"] = true,
["application/x-font-otf"] = true,
["audio/mpeg"] = true,
}
REGISTRY.db:open() REGISTRY.db:open()
local router = Router:new{registry = REGISTRY} local router = Router:new{registry = REGISTRY}
REGISTRY.router = router REGISTRY.router = router
router:setPath(CONTROLLER_ROOT) router:setPath(CONTROLLER_ROOT)
--router:route('edit', 'post/edit', "ALL" ) --router:route('edit', 'post/edit', "ALL" )
router:route('edit', 'post/edit', {
shown = true, -- example of depedencies to the current main route
routes = { -- each layout may have different dependencies
["post/index"] = true local default_routes_dependencies = {
} edit = {
} ) url = "post/edit",
visibility = {
shown = true,
routes = {
["post/index"] = true
}
}
},
--category = {
-- url = "cat/index",
-- visibility = "ALL"
--}
}
router:route('default', default_routes_dependencies )
router:delegate() router:delegate()
if REGISTRY.db then REGISTRY.db:close() end if REGISTRY.db then REGISTRY.db:close() end

View File

@ -1,18 +0,0 @@
require("OOP")
ulib = require("ulib")
require(BASE_FRW.."silk.BaseObject")
require(BASE_FRW.."silk.DBHelper")
require(BASE_FRW.."silk.Router")
require(BASE_FRW.."silk.BaseController")
require(BASE_FRW.."silk.BaseModel")
require(BASE_FRW.."silk.Logger")
require(BASE_FRW.."silk.Template")
HEADER_FLAG = false
function html()
if not HEADER_FLAG then
std.chtml(SESSION)
HEADER_FLAG = true
end
end

View File

@ -0,0 +1,3 @@
<?lua
echo("<h1>Public file of edit action</h1><br/>")
?>

View File

@ -1,5 +1,9 @@
-- create class -- create class
BaseController = BaseObject:extends{class="BaseController", registry = {}, models = {}} BaseController = BaseObject:extends{
class="BaseController",
registry = {},
models = {},
main = false }
-- set the name here in each subclasses -- set the name here in each subclasses
function BaseController:initialize() function BaseController:initialize()
for k, v in pairs(self.models) do for k, v in pairs(self.models) do
@ -27,8 +31,12 @@ function BaseController:redirect(url)
std.header_flush() std.header_flush()
end end
function BaseController:setLayout(name) function BaseController:switchLayout(name)
self.registry.layout = 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
end end
function BaseController:setSession(key, value) function BaseController:setSession(key, value)
@ -75,7 +83,7 @@ function AssetController:get(...)
local path = WWW_ROOT..DIR_SEP..implode({...}, DIR_SEP) local path = WWW_ROOT..DIR_SEP..implode({...}, DIR_SEP)
local mime = std.mimeOf(path) local mime = std.mimeOf(path)
if self.registry.mimes[mime] then if POLICY.mimes[mime] then
std.header(mime) std.header(mime)
if std.isBinary(path) then if std.isBinary(path) then
std.f(path) std.f(path)

View File

@ -24,7 +24,7 @@ function BaseModel:update(m)
end end
function BaseModel:delete(cond) function BaseModel:delete(cond)
if self.db and m then if self.db and cond then
return self.db:delete(self.name,cond) return self.db:delete(self.name,cond)
end end
return false return false
@ -32,14 +32,14 @@ end
function BaseModel:find(cond) function BaseModel:find(cond)
if self.db and m then if self.db and cond then
return self.db:find(self.name, cond) return self.db:find(self.name, cond)
end end
return false return false
end end
function BaseModel:findAll() function BaseModel:findAll()
if self.db and m then if self.db then
return self.db:getAll(self.name) return self.db:getAll(self.name)
end end
return false return false

View File

@ -65,10 +65,12 @@ end
function Router:delegate() function Router:delegate()
local views = {} local views = {}
local data = self:infer() local data = self:infer()
-- set the controller to the main controller
data.controller.main = true
views.__main__ = self:call(data) views.__main__ = self:call(data)
if not views.__main__ then return end if not views.__main__ then return end
-- get all visible routes -- get all visible routes
local routes = self:visibleRoutes(data.name.."/"..data.action) local routes = self:dependencies(data.name.."/"..data.action)
for k, v in pairs(routes) do for k, v in pairs(routes) do
data = self:infer(v) data = self:infer(v)
views[k] = self:call(data) views[k] = self:call(data)
@ -87,10 +89,12 @@ function Router:delegate()
end end
end end
function Router:visibleRoutes(url) function Router:dependencies(url)
if not self.routes[self.registry.layout] then return {} end
local list = {} local list = {}
--self:log("comparing "..url) --self:log("comparing "..url)
for k,v in pairs(self.routes) do for k,v in pairs(self.routes[self.registry.layout]) do
v.url = std.trim(v.url,"/")
if v.visibility == "ALL" then if v.visibility == "ALL" then
list[k] = v.url list[k] = v.url
elseif v.visibility.routes then elseif v.visibility.routes then
@ -118,9 +122,6 @@ function Router:call(data)
end end
end end
function Router:route(name, url, visibility) function Router:route(layout, dependencies)
self.routes[name] = { self.routes[layout] = dependencies
url = std.trim(url,"/"),
visibility = visibility
}
end end

52
silk/api.lua Normal file
View File

@ -0,0 +1,52 @@
require("OOP")
ulib = require("ulib")
require(BASE_FRW.."silk.BaseObject")
require(BASE_FRW.."silk.DBHelper")
require(BASE_FRW.."silk.Router")
require(BASE_FRW.."silk.BaseController")
require(BASE_FRW.."silk.BaseModel")
require(BASE_FRW.."silk.Logger")
require(BASE_FRW.."silk.Template")
-- mime type allows
-- this will bypass the default server security
-- the default list is from the server setting
POLICY = {}
POLICY.mimes = {
["application/javascript"] = true,
["image/bmp"] = true,
["image/jpeg"] = true,
["text/css"] = true,
["text/markdown"] = true,
["text/csv"] = true,
["application/pdf"] = true,
["image/gif"] = true,
["text/html"] = true,
["application/json"] = true,
["application/javascript"] = true,
["image/x-portable-pixmap"] = true,
["application/x-rar-compressed"] = true,
["image/tiff"] = true,
["application/x-tar"] = true,
["text/plain"] = true,
["application/x-font-ttf"] = true,
["application/xhtml+xml"] = true,
["application/xml"] = true,
["application/zip"] = true,
["image/svg+xml"] = true,
["application/vnd.ms-fontobject"] = true,
["application/x-font-woff"] = true,
["application/x-font-otf"] = true,
["audio/mpeg"] = true,
}
HEADER_FLAG = false
function html()
if not HEADER_FLAG then
std.chtml(SESSION)
HEADER_FLAG = true
end
end