From 218de7e0ae569b6dbaec74edec10a0135ecb67b2 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 25 Feb 2018 06:17:40 +0100 Subject: [PATCH] gluon-web: pass base path from CGI script --- .../luasrc/lib/gluon/web/www/cgi-bin/gluon | 6 +- .../luasrc/usr/lib/lua/gluon/web/cgi.lua | 15 ++-- .../usr/lib/lua/gluon/web/dispatcher.lua | 48 +++++------ .../luasrc/usr/lib/lua/gluon/web/i18n.lua | 79 ++++++++++--------- .../luasrc/usr/lib/lua/gluon/web/model.lua | 5 +- .../luasrc/usr/lib/lua/gluon/web/template.lua | 10 +-- .../luasrc/usr/lib/lua/gluon/web/util.lua | 9 --- 7 files changed, 77 insertions(+), 95 deletions(-) diff --git a/package/gluon-web/luasrc/lib/gluon/web/www/cgi-bin/gluon b/package/gluon-web/luasrc/lib/gluon/web/www/cgi-bin/gluon index 2388b0ce..9f8c41ab 100755 --- a/package/gluon-web/luasrc/lib/gluon/web/www/cgi-bin/gluon +++ b/package/gluon-web/luasrc/lib/gluon/web/www/cgi-bin/gluon @@ -1,3 +1,5 @@ #!/usr/bin/lua -require "gluon.web.cgi" -gluon.web.cgi.run() + +require 'gluon.web.cgi' { + base_path = '/lib/gluon/web', +} diff --git a/package/gluon-web/luasrc/usr/lib/lua/gluon/web/cgi.lua b/package/gluon-web/luasrc/usr/lib/lua/gluon/web/cgi.lua index c7a1169a..8d8c648a 100644 --- a/package/gluon-web/luasrc/usr/lib/lua/gluon/web/cgi.lua +++ b/package/gluon-web/luasrc/usr/lib/lua/gluon/web/cgi.lua @@ -2,10 +2,9 @@ -- Copyright 2017 Matthias Schiffer -- Licensed to the public under the Apache License 2.0. -module("gluon.web.cgi", package.seeall) -local nixio = require("nixio") -require("gluon.web.http") -require("gluon.web.dispatcher") +local nixio = require 'nixio' +local http = require 'gluon.web.http' +local dispatcher = require 'gluon.web.dispatcher' -- Limited source to avoid endless blocking local function limitsource(handle, limit) @@ -27,12 +26,10 @@ local function limitsource(handle, limit) end end -function run() - local http = gluon.web.http.Http( +return function(config) + dispatcher(config, http.Http( nixio.getenv(), limitsource(io.stdin, tonumber(nixio.getenv("CONTENT_LENGTH"))), io.stdout - ) - - gluon.web.dispatcher.httpdispatch(http) + )) end diff --git a/package/gluon-web/luasrc/usr/lib/lua/gluon/web/dispatcher.lua b/package/gluon-web/luasrc/usr/lib/lua/gluon/web/dispatcher.lua index 2612af99..0879f76b 100644 --- a/package/gluon-web/luasrc/usr/lib/lua/gluon/web/dispatcher.lua +++ b/package/gluon-web/luasrc/usr/lib/lua/gluon/web/dispatcher.lua @@ -9,33 +9,11 @@ local tpl = require "gluon.web.template" local util = require "gluon.web.util" local proto = require "gluon.web.http.protocol" -module("gluon.web.dispatcher", package.seeall) - -function build_url(http, path) +local function build_url(http, path) return (http:getenv("SCRIPT_NAME") or "") .. "/" .. table.concat(path, "/") 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 langs = {} @@ -69,8 +47,7 @@ local function set_language(renderer, accept) renderer.set_language(langs) end - -function dispatch(http, request) +local function dispatch(config, http, request) local tree = {nodes={}} local nodes = {[''] = tree} @@ -102,7 +79,7 @@ function dispatch(http, request) return string.format(' %s="%s"', key, util.pcdata(tostring(val))) end - local renderer = tpl.renderer(setmetatable({ + local renderer = tpl(config, setmetatable({ http = http, request = request, node = function(path) return _node({path}) end, @@ -118,7 +95,7 @@ function dispatch(http, request) local function createtree() - local base = util.libpath() .. "/controller/" + local base = config.base_path .. "/controller/" local function load_ctl(path) local ctl = assert(loadfile(path)) @@ -172,7 +149,7 @@ function dispatch(http, request) local hidenav = false 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 map:parse(http) @@ -248,3 +225,18 @@ function dispatch(http, request) }) 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 diff --git a/package/gluon-web/luasrc/usr/lib/lua/gluon/web/i18n.lua b/package/gluon-web/luasrc/usr/lib/lua/gluon/web/i18n.lua index 151a9c1c..ad42c3a3 100644 --- a/package/gluon-web/luasrc/usr/lib/lua/gluon/web/i18n.lua +++ b/package/gluon-web/luasrc/usr/lib/lua/gluon/web/i18n.lua @@ -2,53 +2,56 @@ -- Licensed to the public under the Apache License 2.0. local tparser = require "gluon.web.template.parser" -local util = require "gluon.web.util" 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) - return string.format('%s/%s.%s.lmo', i18ndir, pkg, lang) -end - -local function no_translation(key) - return nil -end - -local function load_catalog(lang, pkg) - if pkg then - local file = i18n_file(lang, pkg) - local cat = fs.access(file) and tparser.load_catalog(file) - - if cat then return cat end + local function i18n_file(lang, pkg) + return string.format('%s/%s.%s.lmo', i18ndir, pkg, lang) end - return no_translation -end - - -module "gluon.web.i18n" - -function supported(lang) - return lang == 'en' or fs.access(i18n_file(lang, 'gluon-web')) -end - -function load(lang, pkg) - local _translate = load_catalog(lang, pkg) - - local function translate(key) - return _translate(key) or key + local function no_translation(key) + return nil end - local function translatef(key, ...) - return translate(key):format(...) + local function load_catalog(lang, pkg) + if pkg then + local file = i18n_file(lang, pkg) + local cat = fs.access(file) and tparser.load_catalog(file) + + if cat then return cat end + end + + return no_translation end - return { - _translate = _translate, - translate = translate, - translatef = translatef, - } + + local i18n = {} + + function i18n.supported(lang) + return lang == 'en' or fs.access(i18n_file(lang, 'gluon-web')) + end + + function i18n.load(lang, pkg) + local _translate = load_catalog(lang, pkg) + + local function translate(key) + return _translate(key) or key + end + + local function translatef(key, ...) + return translate(key):format(...) + end + + return { + _translate = _translate, + translate = translate, + translatef = translatef, + } + end + + return i18n end diff --git a/package/gluon-web/luasrc/usr/lib/lua/gluon/web/model.lua b/package/gluon-web/luasrc/usr/lib/lua/gluon/web/model.lua index a9885ab3..00f77fe3 100644 --- a/package/gluon-web/luasrc/usr/lib/lua/gluon/web/model.lua +++ b/package/gluon-web/luasrc/usr/lib/lua/gluon/web/model.lua @@ -8,7 +8,6 @@ local util = require "gluon.web.util" local fs = require "nixio.fs" local datatypes = require "gluon.web.model.datatypes" -local dispatcher = require "gluon.web.dispatcher" local class = util.class local instanceof = util.instanceof @@ -17,8 +16,8 @@ FORM_VALID = 1 FORM_INVALID = -1 -- Loads a model from given file, creating an environment and returns it -function load(name, renderer, pkg) - local modeldir = util.libpath() .. "/model/" +function load(config, name, renderer, pkg) + local modeldir = config.base_path .. "/model/" if not fs.access(modeldir..name..".lua") then error("Model '" .. name .. "' not found!") diff --git a/package/gluon-web/luasrc/usr/lib/lua/gluon/web/template.lua b/package/gluon-web/luasrc/usr/lib/lua/gluon/web/template.lua index 002b9152..0aea336d 100644 --- a/package/gluon-web/luasrc/usr/lib/lua/gluon/web/template.lua +++ b/package/gluon-web/luasrc/usr/lib/lua/gluon/web/template.lua @@ -2,19 +2,17 @@ -- Copyright 2017-2018 Matthias Schiffer -- Licensed to the public under the Apache License 2.0. -local tparser = require "gluon.web.template.parser" -local i18n = require "gluon.web.i18n" -local util = require "gluon.web.util" +local tparser = require 'gluon.web.template.parser' local tostring, ipairs, setmetatable, setfenv = tostring, ipairs, setmetatable, setfenv 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 language = 'en' diff --git a/package/gluon-web/luasrc/usr/lib/lua/gluon/web/util.lua b/package/gluon-web/luasrc/usr/lib/lua/gluon/web/util.lua index 7f2f1ccc..3e32ec50 100644 --- a/package/gluon-web/luasrc/usr/lib/lua/gluon/web/util.lua +++ b/package/gluon-web/luasrc/usr/lib/lua/gluon/web/util.lua @@ -86,12 +86,3 @@ function exec(command) return data end - -function uniqueid(bytes) - local rand = fs.readfile("/dev/urandom", bytes) - return nixio.bin.hexlify(rand) -end - -function libpath() - return '/lib/gluon/web' -end