1
0
mirror of https://github.com/lxsang/antd-web-apps synced 2024-11-19 18:08:21 +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:
cp -rf $(copyfiles) $(BUILDDIR)
cp -r silk $(BUILDDIR)
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(...)
local args = {...}
self.template:set("index", args[1])
self.template:set("dummy", "This is a dummy string")
self:setSession("postsession", "Huehuehue")
self.template:set("post", self.post:findAll())
return true
end
@ -18,8 +17,7 @@ function PostController:edit(...)
else
self.template:set("auth", false)
end
self:setLayout("admin")
--self:redirect("/category/put/1")
self:switchLayout("admin")
return true
end

View File

@ -6,12 +6,14 @@
DIR_SEP = "/"
WWW_ROOT = "/opt/www/htdocs/apps"
HTTP_ROOT = "https://apps.localhost:9195/"
BASE_FRW = "apps."
LOG_ROOT = WWW_ROOT..DIR_SEP.."logs"
CONTROLLER_ROOT = BASE_FRW.."controllers"
MODEL_ROOT = BASE_FRW.."models"
-- class path: path.to.class
BASE_FRW = ""
-- class path: path.to.class
CONTROLLER_ROOT = BASE_FRW.."apps.controllers"
MODEL_ROOT = BASE_FRW.."apps.models"
-- file path: path/to/file
VIEW_ROOT = WWW_ROOT..DIR_SEP.."views"
LOG_ROOT = WWW_ROOT..DIR_SEP.."logs"
-- require needed library
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.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()
local router = Router:new{registry = REGISTRY}
REGISTRY.router = router
router:setPath(CONTROLLER_ROOT)
--router:route('edit', 'post/edit', "ALL" )
router:route('edit', 'post/edit', {
shown = true,
routes = {
["post/index"] = true
}
} )
-- example of depedencies to the current main route
-- each layout may have different dependencies
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()
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
BaseController = BaseObject:extends{class="BaseController", registry = {}, models = {}}
BaseController = BaseObject:extends{
class="BaseController",
registry = {},
models = {},
main = false }
-- set the name here in each subclasses
function BaseController:initialize()
for k, v in pairs(self.models) do
@ -27,8 +31,12 @@ function BaseController:redirect(url)
std.header_flush()
end
function BaseController:setLayout(name)
self.registry.layout = name
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
end
function BaseController:setSession(key, value)
@ -75,7 +83,7 @@ function AssetController:get(...)
local path = WWW_ROOT..DIR_SEP..implode({...}, DIR_SEP)
local mime = std.mimeOf(path)
if self.registry.mimes[mime] then
if POLICY.mimes[mime] then
std.header(mime)
if std.isBinary(path) then
std.f(path)

View File

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

View File

@ -65,10 +65,12 @@ end
function Router:delegate()
local views = {}
local data = self:infer()
-- set the controller to the main controller
data.controller.main = true
views.__main__ = self:call(data)
if not views.__main__ then return end
-- 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
data = self:infer(v)
views[k] = self:call(data)
@ -87,10 +89,12 @@ function Router:delegate()
end
end
function Router:visibleRoutes(url)
function Router:dependencies(url)
if not self.routes[self.registry.layout] then return {} end
local list = {}
--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
list[k] = v.url
elseif v.visibility.routes then
@ -118,9 +122,6 @@ function Router:call(data)
end
end
function Router:route(name, url, visibility)
self.routes[name] = {
url = std.trim(url,"/"),
visibility = visibility
}
function Router:route(layout, dependencies)
self.routes[layout] = dependencies
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