diff --git a/info/Makefile b/info/Makefile index 9ff2274..023daa4 100644 --- a/info/Makefile +++ b/info/Makefile @@ -1,6 +1,6 @@ BUILDDIR = ../build/info -copyfiles = index.ls style.css router.lua models views controllers +copyfiles = index.ls style.css router.lua models views controllers logs main: - mkdir $(BUILDDIR) diff --git a/info/controllers/IndexController.lua b/info/controllers/IndexController.lua index 10621f6..af4cab6 100644 --- a/info/controllers/IndexController.lua +++ b/info/controllers/IndexController.lua @@ -1,10 +1,47 @@ IndexController = BaseController:extends{ class = "IndexController", registry = {}, - models = { "sections", "user", "category" } + models = { "sections", "category" } } +local sectionsByCid = function(db, id) + local cond = { exp = { ["="] = { cid = id } } , order = { start = "DESC" } } + local data, a = db:find(cond) + return data,a +end + function IndexController:index(...) local args = {...} + -- now read all the data + -- get all root sections as the toc + local cond = { exp = { ["="] = { pid = 0 } }, order = { name = "ASC" } } + local data, a = self.category:find(cond) + local toc = {} + if not data then + return self:error("Cannot query the ToC") + end + -- find all children category of the toc + for key,cat in pairs(data) do + cat.name = cat.name:gsub("^%d+%.","") + table.insert( toc, {cat.name, cat.id} ) + cond = { exp = { ["="] = { pid = cat.id } }, order = { name = "ASC" } } + local children, b = self.category:find(cond) + if children and #children > 0 then + for k,v in pairs(children) do + v.sections = sectionsByCid(self.sections,v.id) + end + cat.children = children + else + cat.sections = sectionsByCid(self.sections,cat.id) + end + end + self.template:set("data", data) + self.template:set("toc", toc) return true end + +function IndexController:notoc(...) + self.template:setView("index") + return self:index(table.unpack({...})) +end + diff --git a/info/controllers/TocController.lua b/info/controllers/TocController.lua new file mode 100644 index 0000000..eea9d1f --- /dev/null +++ b/info/controllers/TocController.lua @@ -0,0 +1,7 @@ +TocController = BaseController:extends{ + class = "TocController" +} + +function TocController:index(...) + return true +end \ No newline at end of file diff --git a/info/controllers/UserController.lua b/info/controllers/UserController.lua new file mode 100644 index 0000000..73723fe --- /dev/null +++ b/info/controllers/UserController.lua @@ -0,0 +1,14 @@ +UserController = BaseController:extends{ + class = "UserController", + models = {"user"} +} + +function UserController:index(...) + local args = {...} + local data = self.user:findAll() + if not data or not data[1] then + self:error("Cannot fetch user info") + end + self.template:set("data", data[1]) + return true +end \ No newline at end of file diff --git a/info/index.ls b/info/index.ls.old similarity index 100% rename from info/index.ls rename to info/index.ls.old diff --git a/info/router.lua b/info/router.lua index 98c4847..a2a35cd 100644 --- a/info/router.lua +++ b/info/router.lua @@ -5,7 +5,7 @@ -- some global variables DIR_SEP = "/" WWW_ROOT = "/opt/www/htdocs/info" -HTTP_ROOT = "https://apps.localhost:9195/" +HTTP_ROOT = "https://info.localhost:9195/" -- class path: path.to.class BASE_FRW = "" -- class path: path.to.class @@ -34,19 +34,19 @@ router:setPath(CONTROLLER_ROOT) -- example of depedencies to the current main route -- each layout may have different dependencies local default_routes_dependencies = { - edit = { - url = "post/edit", + user = { + url = "user/index", + visibility = "ALL" + }, + toc = { + url = "toc/index", visibility = { shown = true, routes = { - ["post/index"] = true + ["index/index"] = true } } - }, - --category = { - -- url = "cat/index", - -- visibility = "ALL" - --} + } } router:route('default', default_routes_dependencies ) router:delegate() diff --git a/info/views/default/index/index.ls b/info/views/default/index/index.ls index 282580f..a4fd4e5 100644 --- a/info/views/default/index/index.ls +++ b/info/views/default/index/index.ls @@ -1,3 +1,92 @@ +
> +

+ +
+

+ +
+

+ + + + + + +

+
+ + + + +
+
+ +
+
+ +
+ +
+ +
> +

+ +
+

+ + + + + + +

+
+ + + + +
+
+ +
+
+ +
+ \ No newline at end of file diff --git a/info/views/default/layout.ls b/info/views/default/layout.ls index 0d25652..b050238 100644 --- a/info/views/default/layout.ls +++ b/info/views/default/layout.ls @@ -1,9 +1,51 @@ Default page") - local args = {...} local views = args[1] - if views.__main__ then - views.__main__:render() - end -?> \ No newline at end of file +?> + + + + + + + Porfolio + + +
+
+ +
+

+

Powered by antd server, (C) 2017-2018 Xuan Sang LE

+
+
+ +
+ + + + \ No newline at end of file diff --git a/info/views/default/toc/index.ls b/info/views/default/toc/index.ls new file mode 100644 index 0000000..c3941bb --- /dev/null +++ b/info/views/default/toc/index.ls @@ -0,0 +1,17 @@ + +
+ +
\ No newline at end of file diff --git a/info/views/default/user/index.ls b/info/views/default/user/index.ls new file mode 100644 index 0000000..d8d6c6c --- /dev/null +++ b/info/views/default/user/index.ls @@ -0,0 +1,24 @@ + + +

+ + Curriculum Vitae +

+

+

+

+ + + + + + +

+

+ + + +

\ No newline at end of file diff --git a/silk/BaseController.lua b/silk/BaseController.lua index 6bb97a9..d88593c 100644 --- a/silk/BaseController.lua +++ b/silk/BaseController.lua @@ -68,7 +68,8 @@ NotfoundController = BaseController:extends{ registry = {}, models = {} } function NotfoundController:index(...) local args = {...} - self:error("404: Controller "..args[1].." not found") + local error = args[2] or "" + self:error("404: Controller "..args[1].." not found : "..error) return false end @@ -86,9 +87,9 @@ function AssetController:get(...) if POLICY.mimes[mime] then std.header(mime) if std.isBinary(path) then - std.f(path) - else std.fb(path) + else + std.f(path) end end return false diff --git a/silk/BaseObject.lua b/silk/BaseObject.lua index 67d40df..360d4bd 100644 --- a/silk/BaseObject.lua +++ b/silk/BaseObject.lua @@ -22,4 +22,5 @@ function BaseObject:error(msg, trace) else error(msg) end + return false end \ No newline at end of file diff --git a/silk/Router.lua b/silk/Router.lua index 009e06e..f4d61a7 100644 --- a/silk/Router.lua +++ b/silk/Router.lua @@ -44,11 +44,11 @@ function Router:infer(url) local controller_path = self.path.."."..controller_name -- require the controller module -- ignore the error - pcall(require, controller_path) + local r,e = pcall(require, controller_path) --require(controller_path) if not _G[controller_name] then data.controller = NotfoundController:new{ registry = self.registry } - data.args = {controller_name} + data.args = {controller_name, e} data.action = "index" data.name = "notfound" else @@ -113,9 +113,9 @@ function Router:dependencies(url) end function Router:call(data) + data.controller.template:setView(data.action, data.name) local obj = data.controller[data.action](data.controller,table.unpack(data.args)) if obj then - data.controller.template:setView(data.action, data.name) return data.controller.template else return false diff --git a/silk/Template.lua b/silk/Template.lua index 03e38bb..fe2a8cb 100644 --- a/silk/Template.lua +++ b/silk/Template.lua @@ -11,21 +11,32 @@ function Template:set(k, v, ow) end end +function Template:get(k) + return self.vars[k] +end + function Template:remove(k) self.vars[k] = nil end -- infer view path function Template:setView(name, controller) - self.path = VIEW_ROOT..DIR_SEP..self.registry.layout..DIR_SEP..controller..DIR_SEP..name..".ls" - if ulib.exists(self.path) then + self.name = name + if controller then + self.controller = controller + end +end +function Template:path() + local path = VIEW_ROOT..DIR_SEP..self.registry.layout..DIR_SEP..self.controller..DIR_SEP..self.name..".ls" + if ulib.exists(path) then + return path else - self:error("View not found: "..self.path) + self:error("View not found: "..path) end end -- render the page function Template:render() - local fn, e = loadscript(self.path) + local fn, e = loadscript(self:path()) if fn then local r,o = pcall(fn, self.vars) if not r then