From b07da22b52d13aa1fe3821583cff4cedba8319be Mon Sep 17 00:00:00 2001 From: Xuan Sang LE Date: Thu, 23 Aug 2018 11:15:08 +0200 Subject: [PATCH] using silk as base framework for all webapp --- Makefile | 4 +- apps/controllers/PostController.lua | 6 +-- apps/router.lua | 69 ++++++++++---------------- apps/silk/api.lua | 18 ------- apps/views/default/post/edit.ls | 3 ++ {apps/silk => silk}/BaseController.lua | 16 ++++-- {apps/silk => silk}/BaseModel.lua | 6 +-- {apps/silk => silk}/BaseObject.lua | 0 {apps/silk => silk}/DBHelper.lua | 0 {apps/silk => silk}/Logger.lua | 0 {apps/silk => silk}/Router.lua | 17 ++++--- {apps/silk => silk}/Template.lua | 0 silk/api.lua | 52 +++++++++++++++++++ 13 files changed, 110 insertions(+), 81 deletions(-) delete mode 100644 apps/silk/api.lua create mode 100644 apps/views/default/post/edit.ls rename {apps/silk => silk}/BaseController.lua (85%) rename {apps/silk => silk}/BaseModel.lua (91%) rename {apps/silk => silk}/BaseObject.lua (100%) rename {apps/silk => silk}/DBHelper.lua (100%) rename {apps/silk => silk}/Logger.lua (100%) rename {apps/silk => silk}/Router.lua (88%) rename {apps/silk => silk}/Template.lua (100%) create mode 100644 silk/api.lua diff --git a/Makefile b/Makefile index e20f3cd..3ff9df1 100644 --- a/Makefile +++ b/Makefile @@ -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 \ No newline at end of file + -for f in $(projs); do rm -r $(BUILDDIR)/"$${f}"; done + -rm -r $(BUILDDIR)/silk \ No newline at end of file diff --git a/apps/controllers/PostController.lua b/apps/controllers/PostController.lua index ef48e17..9bd7aca 100644 --- a/apps/controllers/PostController.lua +++ b/apps/controllers/PostController.lua @@ -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 diff --git a/apps/router.lua b/apps/router.lua index 287278f..df676a0 100644 --- a/apps/router.lua +++ b/apps/router.lua @@ -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 diff --git a/apps/silk/api.lua b/apps/silk/api.lua deleted file mode 100644 index 4542da0..0000000 --- a/apps/silk/api.lua +++ /dev/null @@ -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 \ No newline at end of file diff --git a/apps/views/default/post/edit.ls b/apps/views/default/post/edit.ls new file mode 100644 index 0000000..f0f0f46 --- /dev/null +++ b/apps/views/default/post/edit.ls @@ -0,0 +1,3 @@ +Public file of edit action
") +?> \ No newline at end of file diff --git a/apps/silk/BaseController.lua b/silk/BaseController.lua similarity index 85% rename from apps/silk/BaseController.lua rename to silk/BaseController.lua index dd1727a..6bb97a9 100644 --- a/apps/silk/BaseController.lua +++ b/silk/BaseController.lua @@ -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) diff --git a/apps/silk/BaseModel.lua b/silk/BaseModel.lua similarity index 91% rename from apps/silk/BaseModel.lua rename to silk/BaseModel.lua index 4009441..563979a 100644 --- a/apps/silk/BaseModel.lua +++ b/silk/BaseModel.lua @@ -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 diff --git a/apps/silk/BaseObject.lua b/silk/BaseObject.lua similarity index 100% rename from apps/silk/BaseObject.lua rename to silk/BaseObject.lua diff --git a/apps/silk/DBHelper.lua b/silk/DBHelper.lua similarity index 100% rename from apps/silk/DBHelper.lua rename to silk/DBHelper.lua diff --git a/apps/silk/Logger.lua b/silk/Logger.lua similarity index 100% rename from apps/silk/Logger.lua rename to silk/Logger.lua diff --git a/apps/silk/Router.lua b/silk/Router.lua similarity index 88% rename from apps/silk/Router.lua rename to silk/Router.lua index 1bfaf2a..a03fa63 100644 --- a/apps/silk/Router.lua +++ b/silk/Router.lua @@ -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 \ No newline at end of file diff --git a/apps/silk/Template.lua b/silk/Template.lua similarity index 100% rename from apps/silk/Template.lua rename to silk/Template.lua diff --git a/silk/api.lua b/silk/api.lua new file mode 100644 index 0000000..441a99b --- /dev/null +++ b/silk/api.lua @@ -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 \ No newline at end of file