2017-02-08 21:14:30 +00:00
|
|
|
-- Copyright 2008 Steven Barth <steven@midlink.org>
|
|
|
|
-- Copyright 2008-2015 Jo-Philipp Wich <jow@openwrt.org>
|
2018-02-23 05:26:10 +00:00
|
|
|
-- Copyright 2017-2018 Matthias Schiffer <mschiffer@universe-factory.net>
|
2017-02-08 21:14:30 +00:00
|
|
|
-- Licensed to the public under the Apache License 2.0.
|
|
|
|
|
2018-07-13 20:45:23 +00:00
|
|
|
local glob = require 'posix.glob'
|
2018-01-18 12:17:34 +00:00
|
|
|
local json = require "jsonc"
|
2017-02-08 21:14:30 +00:00
|
|
|
local tpl = require "gluon.web.template"
|
|
|
|
local util = require "gluon.web.util"
|
|
|
|
local proto = require "gluon.web.http.protocol"
|
|
|
|
|
|
|
|
|
2018-02-25 05:17:40 +00:00
|
|
|
local function build_url(http, path)
|
2017-02-08 21:14:30 +00:00
|
|
|
return (http:getenv("SCRIPT_NAME") or "") .. "/" .. table.concat(path, "/")
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function set_language(renderer, accept)
|
|
|
|
local langs = {}
|
|
|
|
local weights = {}
|
|
|
|
local star = 0
|
|
|
|
|
|
|
|
local function add(lang, q)
|
|
|
|
if not weights[lang] then
|
|
|
|
table.insert(langs, lang)
|
|
|
|
weights[lang] = q
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
for match in accept:gmatch("[^,]+") do
|
2018-02-23 05:26:10 +00:00
|
|
|
local lang = match:match('^%s*([^%s;_-]+)')
|
2017-02-08 21:14:30 +00:00
|
|
|
local q = tonumber(match:match(';q=(%S+)%s*$') or 1)
|
|
|
|
|
|
|
|
if lang == '*' then
|
|
|
|
star = q
|
|
|
|
elseif lang and q > 0 then
|
|
|
|
add(lang, q)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
add('en', star)
|
|
|
|
|
|
|
|
table.sort(langs, function(a, b)
|
|
|
|
return (weights[a] or 0) > (weights[b] or 0)
|
|
|
|
end)
|
|
|
|
|
2018-02-23 05:26:10 +00:00
|
|
|
renderer.set_language(langs)
|
2017-02-08 21:14:30 +00:00
|
|
|
end
|
|
|
|
|
2018-02-25 05:17:40 +00:00
|
|
|
local function dispatch(config, http, request)
|
2017-02-08 21:14:30 +00:00
|
|
|
local tree = {nodes={}}
|
|
|
|
local nodes = {[''] = tree}
|
|
|
|
|
|
|
|
local function _node(path, create)
|
|
|
|
local name = table.concat(path, ".")
|
|
|
|
local c = nodes[name]
|
|
|
|
|
|
|
|
if not c and create then
|
|
|
|
local last = table.remove(path)
|
|
|
|
local parent = _node(path, true)
|
|
|
|
|
|
|
|
c = {nodes={}}
|
|
|
|
parent.nodes[last] = c
|
|
|
|
nodes[name] = c
|
|
|
|
end
|
|
|
|
return c
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Init template engine
|
|
|
|
local function attr(key, val)
|
|
|
|
if not val then
|
|
|
|
return ''
|
|
|
|
end
|
|
|
|
|
|
|
|
if type(val) == "table" then
|
2018-01-18 06:49:00 +00:00
|
|
|
val = json.stringify(val)
|
2017-02-08 21:14:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return string.format(' %s="%s"', key, util.pcdata(tostring(val)))
|
|
|
|
end
|
|
|
|
|
2018-02-25 05:17:40 +00:00
|
|
|
local renderer = tpl(config, setmetatable({
|
2017-02-08 21:14:30 +00:00
|
|
|
http = http,
|
|
|
|
request = request,
|
|
|
|
node = function(path) return _node({path}) end,
|
|
|
|
write = function(...) return http:write(...) end,
|
|
|
|
pcdata = util.pcdata,
|
|
|
|
urlencode = proto.urlencode,
|
|
|
|
attr = attr,
|
2018-08-18 11:56:00 +00:00
|
|
|
json = json.stringify,
|
2017-02-08 21:14:30 +00:00
|
|
|
url = function(path) return build_url(http, path) end,
|
|
|
|
}, { __index = _G }))
|
|
|
|
|
|
|
|
|
|
|
|
local function createtree()
|
2018-02-25 05:17:40 +00:00
|
|
|
local base = config.base_path .. "/controller/"
|
2017-02-08 21:14:30 +00:00
|
|
|
|
|
|
|
local function load_ctl(path)
|
|
|
|
local ctl = assert(loadfile(path))
|
|
|
|
|
2018-02-23 05:26:10 +00:00
|
|
|
local _pkg
|
|
|
|
|
|
|
|
local subdisp = setmetatable({
|
|
|
|
package = function(name)
|
|
|
|
_pkg = name
|
|
|
|
end,
|
|
|
|
|
|
|
|
node = function(...)
|
|
|
|
return _node({...})
|
|
|
|
end,
|
|
|
|
|
2019-06-16 18:06:45 +00:00
|
|
|
entry = function(entry_path, target, title, order)
|
|
|
|
local c = _node(entry_path, true)
|
2018-02-23 05:26:10 +00:00
|
|
|
|
|
|
|
c.target = target
|
|
|
|
c.title = title
|
|
|
|
c.order = order
|
|
|
|
c.pkg = _pkg
|
|
|
|
|
|
|
|
return c
|
|
|
|
end,
|
|
|
|
|
|
|
|
alias = function(...)
|
|
|
|
local req = {...}
|
|
|
|
return function()
|
|
|
|
http:redirect(build_url(http, req))
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
|
|
|
call = function(func, ...)
|
|
|
|
local args = {...}
|
|
|
|
return function()
|
|
|
|
func(http, renderer, unpack(args))
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
2018-02-25 18:32:08 +00:00
|
|
|
template = function(view, scope)
|
2018-02-23 05:26:10 +00:00
|
|
|
local pkg = _pkg
|
|
|
|
return function()
|
2018-02-25 18:32:08 +00:00
|
|
|
renderer.render_layout(view, scope, pkg)
|
2018-02-23 05:26:10 +00:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
|
|
|
model = function(name)
|
|
|
|
local pkg = _pkg
|
|
|
|
return function()
|
2018-02-25 17:14:24 +00:00
|
|
|
require('gluon.web.model')(config, http, renderer, name, pkg)
|
2018-02-23 05:26:10 +00:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
|
|
|
_ = function(text)
|
|
|
|
return text
|
|
|
|
end,
|
|
|
|
}, { __index = _G })
|
|
|
|
|
2017-02-08 21:14:30 +00:00
|
|
|
local env = setmetatable({}, { __index = subdisp })
|
|
|
|
setfenv(ctl, env)
|
|
|
|
|
|
|
|
ctl()
|
|
|
|
end
|
|
|
|
|
2018-07-13 20:45:23 +00:00
|
|
|
for _, path in ipairs(glob.glob(base .. "*.lua") or {}) do
|
2017-02-08 21:14:30 +00:00
|
|
|
load_ctl(path)
|
|
|
|
end
|
2018-07-13 20:45:23 +00:00
|
|
|
for _, path in ipairs(glob.glob(base .. "*/*.lua") or {}) do
|
2017-02-08 21:14:30 +00:00
|
|
|
load_ctl(path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
set_language(renderer, http:getenv("HTTP_ACCEPT_LANGUAGE") or "")
|
|
|
|
|
|
|
|
createtree()
|
|
|
|
|
|
|
|
|
|
|
|
local node = _node(request)
|
|
|
|
|
|
|
|
if not node or not node.target then
|
|
|
|
http:status(404, "Not Found")
|
2018-02-25 18:32:08 +00:00
|
|
|
renderer.render_layout("error/404", {
|
|
|
|
message =
|
|
|
|
"No page is registered at '/" .. table.concat(request, "/") .. "'.\n" ..
|
|
|
|
"If this URL belongs to an extension, make sure it is properly installed.\n",
|
|
|
|
}, 'gluon-web')
|
2017-02-08 21:14:30 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
http:parse_input(node.filehandler)
|
|
|
|
|
|
|
|
local ok, err = pcall(node.target)
|
|
|
|
if not ok then
|
|
|
|
http:status(500, "Internal Server Error")
|
2018-02-25 18:32:08 +00:00
|
|
|
renderer.render_layout("error/500", {
|
|
|
|
message =
|
|
|
|
"Failed to execute dispatcher target for entry '/" .. table.concat(request, "/") .. "'.\n" ..
|
|
|
|
"The called action terminated with an exception:\n" .. tostring(err or "(unknown)"),
|
|
|
|
}, 'gluon-web')
|
2017-02-08 21:14:30 +00:00
|
|
|
end
|
|
|
|
end
|
2018-02-25 05:17:40 +00:00
|
|
|
|
|
|
|
return function(config, http)
|
|
|
|
local request = {}
|
|
|
|
local pathinfo = proto.urldecode(http:getenv("PATH_INFO") or "", true)
|
|
|
|
for node in pathinfo:gmatch("[^/]+") do
|
|
|
|
table.insert(request, node)
|
|
|
|
end
|
|
|
|
|
2019-06-16 18:06:45 +00:00
|
|
|
local ok, err = pcall(dispatch, config, http, request)
|
2018-02-25 05:17:40 +00:00
|
|
|
if not ok then
|
|
|
|
http:status(500, "Internal Server Error")
|
|
|
|
http:prepare_content("text/plain")
|
|
|
|
http:write(err)
|
|
|
|
end
|
|
|
|
end
|