gluon-web: pass base path from CGI script

This commit is contained in:
Matthias Schiffer 2018-02-25 06:17:40 +01:00
parent 661e4dee9f
commit 218de7e0ae
No known key found for this signature in database
GPG Key ID: 16EF3F64CB201D9C
7 changed files with 77 additions and 95 deletions

View File

@ -1,3 +1,5 @@
#!/usr/bin/lua #!/usr/bin/lua
require "gluon.web.cgi"
gluon.web.cgi.run() require 'gluon.web.cgi' {
base_path = '/lib/gluon/web',
}

View File

@ -2,10 +2,9 @@
-- Copyright 2017 Matthias Schiffer <mschiffer@universe-factory.net> -- Copyright 2017 Matthias Schiffer <mschiffer@universe-factory.net>
-- Licensed to the public under the Apache License 2.0. -- Licensed to the public under the Apache License 2.0.
module("gluon.web.cgi", package.seeall) local nixio = require 'nixio'
local nixio = require("nixio") local http = require 'gluon.web.http'
require("gluon.web.http") local dispatcher = require 'gluon.web.dispatcher'
require("gluon.web.dispatcher")
-- Limited source to avoid endless blocking -- Limited source to avoid endless blocking
local function limitsource(handle, limit) local function limitsource(handle, limit)
@ -27,12 +26,10 @@ local function limitsource(handle, limit)
end end
end end
function run() return function(config)
local http = gluon.web.http.Http( dispatcher(config, http.Http(
nixio.getenv(), nixio.getenv(),
limitsource(io.stdin, tonumber(nixio.getenv("CONTENT_LENGTH"))), limitsource(io.stdin, tonumber(nixio.getenv("CONTENT_LENGTH"))),
io.stdout io.stdout
) ))
gluon.web.dispatcher.httpdispatch(http)
end end

View File

@ -9,33 +9,11 @@ local tpl = require "gluon.web.template"
local util = require "gluon.web.util" local util = require "gluon.web.util"
local proto = require "gluon.web.http.protocol" local proto = require "gluon.web.http.protocol"
module("gluon.web.dispatcher", package.seeall)
local function build_url(http, path)
function build_url(http, path)
return (http:getenv("SCRIPT_NAME") or "") .. "/" .. table.concat(path, "/") return (http:getenv("SCRIPT_NAME") or "") .. "/" .. table.concat(path, "/")
end end
function redirect(http, ...)
http:redirect(build_url(http, {...}))
end
function httpdispatch(http)
local request = {}
local pathinfo = proto.urldecode(http:getenv("PATH_INFO") or "", true)
for node in pathinfo:gmatch("[^/]+") do
table.insert(request, node)
end
ok, err = pcall(dispatch, http, request)
if not ok then
http:status(500, "Internal Server Error")
http:prepare_content("text/plain")
http:write(err)
end
end
local function set_language(renderer, accept) local function set_language(renderer, accept)
local langs = {} local langs = {}
@ -69,8 +47,7 @@ local function set_language(renderer, accept)
renderer.set_language(langs) renderer.set_language(langs)
end end
local function dispatch(config, http, request)
function dispatch(http, request)
local tree = {nodes={}} local tree = {nodes={}}
local nodes = {[''] = tree} local nodes = {[''] = tree}
@ -102,7 +79,7 @@ function dispatch(http, request)
return string.format(' %s="%s"', key, util.pcdata(tostring(val))) return string.format(' %s="%s"', key, util.pcdata(tostring(val)))
end end
local renderer = tpl.renderer(setmetatable({ local renderer = tpl(config, setmetatable({
http = http, http = http,
request = request, request = request,
node = function(path) return _node({path}) end, node = function(path) return _node({path}) end,
@ -118,7 +95,7 @@ function dispatch(http, request)
local function createtree() local function createtree()
local base = util.libpath() .. "/controller/" local base = config.base_path .. "/controller/"
local function load_ctl(path) local function load_ctl(path)
local ctl = assert(loadfile(path)) local ctl = assert(loadfile(path))
@ -172,7 +149,7 @@ function dispatch(http, request)
local hidenav = false local hidenav = false
local model = require "gluon.web.model" local model = require "gluon.web.model"
local maps = model.load(name, renderer, pkg) local maps = model.load(config, name, renderer, pkg)
for _, map in ipairs(maps) do for _, map in ipairs(maps) do
map:parse(http) map:parse(http)
@ -248,3 +225,18 @@ function dispatch(http, request)
}) })
end end
end end
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
ok, err = pcall(dispatch, config, http, request)
if not ok then
http:status(500, "Internal Server Error")
http:prepare_content("text/plain")
http:write(err)
end
end

View File

@ -2,11 +2,11 @@
-- Licensed to the public under the Apache License 2.0. -- Licensed to the public under the Apache License 2.0.
local tparser = require "gluon.web.template.parser" local tparser = require "gluon.web.template.parser"
local util = require "gluon.web.util"
local fs = require "nixio.fs" local fs = require "nixio.fs"
local i18ndir = util.libpath() .. "/i18n" return function(config)
local i18ndir = config.base_path .. "/i18n"
local function i18n_file(lang, pkg) local function i18n_file(lang, pkg)
@ -29,13 +29,13 @@ local function load_catalog(lang, pkg)
end end
module "gluon.web.i18n" local i18n = {}
function supported(lang) function i18n.supported(lang)
return lang == 'en' or fs.access(i18n_file(lang, 'gluon-web')) return lang == 'en' or fs.access(i18n_file(lang, 'gluon-web'))
end end
function load(lang, pkg) function i18n.load(lang, pkg)
local _translate = load_catalog(lang, pkg) local _translate = load_catalog(lang, pkg)
local function translate(key) local function translate(key)
@ -52,3 +52,6 @@ function load(lang, pkg)
translatef = translatef, translatef = translatef,
} }
end end
return i18n
end

View File

@ -8,7 +8,6 @@ local util = require "gluon.web.util"
local fs = require "nixio.fs" local fs = require "nixio.fs"
local datatypes = require "gluon.web.model.datatypes" local datatypes = require "gluon.web.model.datatypes"
local dispatcher = require "gluon.web.dispatcher"
local class = util.class local class = util.class
local instanceof = util.instanceof local instanceof = util.instanceof
@ -17,8 +16,8 @@ FORM_VALID = 1
FORM_INVALID = -1 FORM_INVALID = -1
-- Loads a model from given file, creating an environment and returns it -- Loads a model from given file, creating an environment and returns it
function load(name, renderer, pkg) function load(config, name, renderer, pkg)
local modeldir = util.libpath() .. "/model/" local modeldir = config.base_path .. "/model/"
if not fs.access(modeldir..name..".lua") then if not fs.access(modeldir..name..".lua") then
error("Model '" .. name .. "' not found!") error("Model '" .. name .. "' not found!")

View File

@ -2,19 +2,17 @@
-- Copyright 2017-2018 Matthias Schiffer <mschiffer@universe-factory.net> -- Copyright 2017-2018 Matthias Schiffer <mschiffer@universe-factory.net>
-- Licensed to the public under the Apache License 2.0. -- Licensed to the public under the Apache License 2.0.
local tparser = require "gluon.web.template.parser" local tparser = require 'gluon.web.template.parser'
local i18n = require "gluon.web.i18n"
local util = require "gluon.web.util"
local tostring, ipairs, setmetatable, setfenv = tostring, ipairs, setmetatable, setfenv local tostring, ipairs, setmetatable, setfenv = tostring, ipairs, setmetatable, setfenv
local pcall, assert = pcall, assert local pcall, assert = pcall, assert
module "gluon.web.template" return function(config, env)
local i18n = require('gluon.web.i18n')(config)
local viewdir = util.libpath() .. "/view/" local viewdir = config.base_path .. '/view/'
function renderer(env)
local ctx = {} local ctx = {}
local language = 'en' local language = 'en'

View File

@ -86,12 +86,3 @@ function exec(command)
return data return data
end end
function uniqueid(bytes)
local rand = fs.readfile("/dev/urandom", bytes)
return nixio.bin.hexlify(rand)
end
function libpath()
return '/lib/gluon/web'
end