From 8e8139e709211bb97a7aaa488314a5e855030b0e Mon Sep 17 00:00:00 2001 From: Xuan Sang LE Date: Tue, 28 Aug 2018 10:21:27 +0200 Subject: [PATCH] Silk can now handle the asset file directly in url --- blog/Makefile | 2 +- blog/robot.txt | 2 + info/Makefile | 2 +- info/robot.txt | 2 + info/views/default/layout.ls | 6 +-- silk/BaseController.lua | 18 ++++++--- silk/Router.lua | 76 ++++++++++++++++++++++-------------- 7 files changed, 68 insertions(+), 40 deletions(-) create mode 100644 blog/robot.txt create mode 100644 info/robot.txt diff --git a/blog/Makefile b/blog/Makefile index 65a463c..40caa87 100644 --- a/blog/Makefile +++ b/blog/Makefile @@ -1,6 +1,6 @@ BUILDDIR = ../build/blog -copyfiles = *.html assets *.lua view +copyfiles = *.html assets *.lua view robot.txt main: - mkdir $(BUILDDIR) diff --git a/blog/robot.txt b/blog/robot.txt new file mode 100644 index 0000000..c2a49f4 --- /dev/null +++ b/blog/robot.txt @@ -0,0 +1,2 @@ +User-agent: * +Allow: / diff --git a/info/Makefile b/info/Makefile index 9d0fd55..d527173 100644 --- a/info/Makefile +++ b/info/Makefile @@ -1,6 +1,6 @@ BUILDDIR = ../build/info -copyfiles = index.ls style.css router.lua models views controllers logs +copyfiles = index.ls style.css router.lua models views controllers logs robot.txt main: - mkdir $(BUILDDIR) diff --git a/info/robot.txt b/info/robot.txt new file mode 100644 index 0000000..c2a49f4 --- /dev/null +++ b/info/robot.txt @@ -0,0 +1,2 @@ +User-agent: * +Allow: / diff --git a/info/views/default/layout.ls b/info/views/default/layout.ls index b050238..436fdf8 100644 --- a/info/views/default/layout.ls +++ b/info/views/default/layout.ls @@ -5,9 +5,9 @@ - - - + + + Porfolio diff --git a/silk/BaseController.lua b/silk/BaseController.lua index ccb1e8d..1426a19 100644 --- a/silk/BaseController.lua +++ b/silk/BaseController.lua @@ -90,13 +90,19 @@ function AssetController:get(...) local path = WWW_ROOT..DIR_SEP..implode({...}, DIR_SEP) local mime = std.mimeOf(path) - if POLICY.mimes[mime] then - std.header(mime) - if std.isBinary(path) then - std.fb(path) - else - std.f(path) + if ulib.exists(path) then + if POLICY.mimes[mime] then + std.header(mime) + if std.isBinary(path) then + std.fb(path) + else + std.f(path) + end + else + self:error("Access fobiden: "..path) end + else + self:error("Assset file not found: "..path) end return false end \ No newline at end of file diff --git a/silk/Router.lua b/silk/Router.lua index 6884baa..3be9ecb 100644 --- a/silk/Router.lua +++ b/silk/Router.lua @@ -1,6 +1,5 @@ - --define the class -BaseObject:subclass ("Router",{registry = {}}) +BaseObject:subclass("Router", {registry = {}}) function Router:setPath(path) self.path = path end @@ -25,76 +24,95 @@ function Router:infer(url) -- if user dont provice the url, try to infer it -- from the REQUEST url = url or REQUEST.query.r - url = std.trim(url,"/") - local args = explode(url,"/") + url = std.trim(url, "/") + local args = explode(url, "/") local data = { - name = 'index', - action = 'index', + name = "index", + action = "index", args = {} } if args and #args > 0 and args[1] ~= "" then data.name = args[1] - if args[2] then data.action = args[2] end - for i = 3, #args do table.insert( data.args, args[i] ) end + if args[2] then + data.action = args[2] + end + for i = 3, #args do + table.insert(data.args, args[i]) + end end - self:log('Controller: '..JSON.encode(data)) -- find the controller class and init it - local controller_name = firstToUpper(data.name).."Controller" - local controller_path = self.path.."."..controller_name + local controller_name = firstToUpper(data.name) .. "Controller" + local controller_path = self.path .. "." .. controller_name -- require the controller module -- ignore the error - local r,e = 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, e} - data.action = "index" - data.name = "notfound" + -- verify if it is an asset + url = url:gsub("/", DIR_SEP) + if ulib.exists(WWW_ROOT..DIR_SEP..url) then + data.controller = AssetController:new {registry = self.registry} + data.action = "get" + data.name = "asset" + data.args ={url} + else + -- let the notfound controller handle the error + data.controller = NotfoundController:new {registry = self.registry} + data.args = {controller_name, e} + data.action = "index" + data.name = "notfound" + end else - data.controller = _G[controller_name]:new{ registry = self.registry } - if not data.controller[data.action] then + -- create the coresponding controller + data.controller = _G[controller_name]:new {registry = self.registry} + if not data.controller[data.action] then data.args = {data.action} data.action = "actionnotfound" end end + + self:log("Controller: " .. data.controller.class .. ", action: "..data.action..", args: ".. JSON.encode(data.args)) return data 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 + if not views.__main__ then + return + end -- get all visible routes - local routes = self:dependencies(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) end -- now require the main page to put the view - 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") html() if fn then - local r,o = pcall(fn, views) + local r, o = pcall(fn, views) if not r then self:error(o) end else - self:error("The index page is not found for layout: "..self.registry.layout) + self:error("The index page is not found for layout: " .. self.registry.layout) end end function Router:dependencies(url) - if not self.routes[self.registry.layout] then return {} end + if not self.routes[self.registry.layout] then + return {} + end local list = {} --self:log("comparing "..url) - for k,v in pairs(self.routes[self.registry.layout]) do - v.url = std.trim(v.url,"/") + 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 @@ -114,7 +132,7 @@ 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)) + local obj = data.controller[data.action](data.controller, table.unpack(data.args)) if obj then return data.controller.template else @@ -124,4 +142,4 @@ end function Router:route(layout, dependencies) self.routes[layout] = dependencies -end \ No newline at end of file +end