gluon-web: pass base path from CGI script
This commit is contained in:
parent
661e4dee9f
commit
218de7e0ae
@ -1,3 +1,5 @@
|
||||
#!/usr/bin/lua
|
||||
require "gluon.web.cgi"
|
||||
gluon.web.cgi.run()
|
||||
|
||||
require 'gluon.web.cgi' {
|
||||
base_path = '/lib/gluon/web',
|
||||
}
|
||||
|
@ -2,10 +2,9 @@
|
||||
-- Copyright 2017 Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
-- 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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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!")
|
||||
|
@ -2,19 +2,17 @@
|
||||
-- Copyright 2017-2018 Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
-- 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'
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user