# HG changeset patch # User jbe # Date 1420838919 -3600 # Node ID eb3e236d261d4342722204e1147027289b269596 # Parent 48ee8826efbece068f67ffe8563abe874afebf83 Code cleanup and work on Moonbridge integration diff -r 48ee8826efbe -r eb3e236d261d framework/bin/mcp.lua --- a/framework/bin/mcp.lua Fri Jan 09 21:32:23 2015 +0100 +++ b/framework/bin/mcp.lua Fri Jan 09 22:28:39 2015 +0100 @@ -9,27 +9,27 @@ WEBMCP_MODE = "interactive" end --- store extra command line arguments -local extraargs = {select(3, ...)} +-- configuration names are provided as 4th, 5th, etc. argument +WEBMCP_CONFIG_NAMES = {select(4, ...)} -- determine framework and bath path from command line arguments -- or print usage synopsis (if applicable) do - local arg1, arg2 = ... + local arg1, arg2, arg3 = ... local helpout if arg1 == "-h" or arg1 == "--help" or - arg2 == "-h" or arg2 == "--help" + arg2 == "-h" or arg2 == "--help" -- if first arg is provided by wrapper then helpout = io.stdout elseif - (WEBMCP_MODE == "listen" and (#extraargs < 2 or #extraargs % 2 ~= 0)) or - (WEBMCP_MODE == "interactive" and arg2 == nil) + #config_args < 1 or + (WEBMCP_MODE == "interactive") ~= (arg3 == "INTERACTIVE") then helpout = io.stderr end - helpout:write("Usage: moonbridge -- mcp.lua [ ...]\n") - helpout:write(" or: lua -i mcp.lua []\n") + helpout:write("Usage: moonbridge -- /bin/mcp.lua [ ...]\n") + helpout:write(" or: lua -i /bin/mcp.lua INTERACTIVE [ ...]\n") if helpout then if helpout == io.stderr then return 1 @@ -42,6 +42,9 @@ end WEBMCP_FRAMEWORK_PATH = append_trailing_slash(arg1) WEBMCP_BASE_PATH = append_trailing_slash(arg2) + if WEBMCP_MODE == "listen" then + WEBMCP_APP_NAME = arg3 + end end -- setup search paths for libraries @@ -175,43 +178,43 @@ end }) +-- execute configurations +for i, config_name in ipairs(WEBMCP_CONFIG_NAMES) do + execute.config(config_name) +end + -- interactive console mode if WEBMCP_MODE == "interactive" then trace.disable() -- avoids memory leakage (TODO: needs general solution for moonbridge?) - local config_name = select(3, ...) - if config_name then - execute.config(config_name) - end - return end -- invoke moonbridge -local moonbridge_listen = listen -local listeners -function _G.listen(args) - listeners[#listeners+1] = args -end -for i = 1, #extraargs/2 do - local config = {} - local function prepare_globals() - _G.WEBMCP_APP_NAME = extraargs[2*i-1] - _G.WEBMCP_CONFIG_NAME = extraargs[2*i] - _G.config = config +if WEBMCP_MODE == "listen" then + local moonbridge_listen = listen + local listeners + function _G.listen(args) + listeners[#listeners+1] = args end - prepare_globals() - listeners = {} - execute.config(config_name) - for i, listener in ipairs(listeners) do - function listener.prepare() - prepare_globals() - request.execute_preparers() + for i = 1, #extraargs/2 do + local config = {} + listeners = {} + execute.config(config_name) + for i, listener in ipairs(listeners) do + listener.prepare = execute.prefork_initializers + listener.connect = http.generate_handler( + request.get_http_options(), + function(req) + execute.postfork_initializers() + request.handler(req) + end + ) + --listener.finish = ??? -- TODO: requires coroutines and execute.inner() for initializers? + moonbridge_listen(listener) end - listener.connect = request.connect - listener.finish = request.execute_finishers end end ---[[ TODO: following lines to be moved to request.connect(...) +--[[ TODO: following lines to be moved to execute.server(...) local success, error_info = xpcall( function() diff -r 48ee8826efbe -r eb3e236d261d framework/env/__init.lua --- a/framework/env/__init.lua Fri Jan 09 21:32:23 2015 +0100 +++ b/framework/env/__init.lua Fri Jan 09 22:28:39 2015 +0100 @@ -46,4 +46,21 @@ atom = require 'atom' json = require 'json' require 'mondelefant_atom_connector' +http = require 'http' +--[[-- +app -- table to store an application state + +'app' is a global table for storing any application state data +--]]-- +app = {} +--//-- + +--[[-- +config -- table to store application configuration + +'config' is a global table, which can be modified by a config file of an application to modify the behaviour of that application. +--]]-- +config = {} +--//-- + diff -r 48ee8826efbe -r eb3e236d261d framework/env/execute/__init.lua --- a/framework/env/execute/__init.lua Fri Jan 09 21:32:23 2015 +0100 +++ b/framework/env/execute/__init.lua Fri Jan 09 22:28:39 2015 +0100 @@ -1,1 +1,1 @@ -execute._wrap_stack = {} +execute._wrap_stack = {} -- used by execute.inner() and execute.wrapped(...) diff -r 48ee8826efbe -r eb3e236d261d framework/env/execute/_add_filters_by_path.lua --- a/framework/env/execute/_add_filters_by_path.lua Fri Jan 09 21:32:23 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,25 +0,0 @@ -function execute._add_filters_by_path(filter_list, ...) - local full_path = encode.file_path(request.get_app_basepath(), "app", ...) - local relative_path = encode.file_path("", ...) - local filter_names = extos.listdir(full_path) - if filter_names then - table.sort(filter_names) -- not really neccessary, due to sorting afterwards - for i, filter_name in ipairs(filter_names) do - if string.find(filter_name, "%.lua$") then - if filter_list[filter_name] then - error('More than one filter is named "' .. filter_name .. '".') - end - table.insert(filter_list, filter_name) - filter_list[filter_name] = function() - trace.enter_filter{ - path = encode.file_path(relative_path, filter_name) - } - execute.file_path{ - file_path = encode.file_path(full_path, filter_name) - } - trace.execution_return() - end - end - end - end -end diff -r 48ee8826efbe -r eb3e236d261d framework/env/execute/_create_sorted_execution_list.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/framework/env/execute/_create_sorted_execution_list.lua Fri Jan 09 22:28:39 2015 +0100 @@ -0,0 +1,31 @@ +function execute._create_sorted_execution_list(add_paths, execute_func) + local entries = {} + add_paths(function(...) + local full_path = encode.file_path(WEBMCP_BASE_PATH, "app", ...) + local relative_path = encode.file_path("", ...) + local filenames = extos.listdir(full_path) + if filenames then + table.sort(filenames) -- not really neccessary, due to sorting afterwards + for i, filename in ipairs(filenames) do + if string.find(filename, "%.lua$") then + if entries[filename] then + error('More than one filter or initializer is named "' .. filter_name .. '".') + end + entries[#entries+1] = filename + entries[filename] = function() + execute_func( + encode.file_path(full_path, filename), + encode.file_path(relative_path, filename) + ) + end + end + end + end + end) + table.sort(entries) + for idx, filename in ipairs(entries) do + entries[idx] = enteries[filename] + entries[filename] = nil + end + return entries +end diff -r 48ee8826efbe -r eb3e236d261d framework/env/execute/action.lua --- a/framework/env/execute/action.lua Fri Jan 09 21:32:23 2015 +0100 +++ b/framework/env/execute/action.lua Fri Jan 09 22:28:39 2015 +0100 @@ -8,7 +8,6 @@ } Executes an action without associated filters. -This function is only used by execute.filtered_action{...}, which itself is only used by the webmcp.lua file in the cgi-bin/ directory. --]]-- @@ -18,8 +17,7 @@ trace.enter_action{ module = module, action = action } local action_status = execute.file_path{ file_path = encode.file_path( - request.get_app_basepath(), - 'app', request.get_app_name(), module, '_action', action .. '.lua' + WEBMCP_BASE_PATH, 'app', WEBMCP_APP_NAME, module, '_action', action .. '.lua' ), id = args.id, params = args.params diff -r 48ee8826efbe -r eb3e236d261d framework/env/execute/chunk.lua --- a/framework/env/execute/chunk.lua Fri Jan 09 21:32:23 2015 +0100 +++ b/framework/env/execute/chunk.lua Fri Jan 09 22:28:39 2015 +0100 @@ -24,17 +24,14 @@ function execute.chunk(args) local file_path = args.file_path - local app = args.app + local app = args.app or WEBMCP_APP_NAME local module = args.module local chunk = args.chunk local id = args.id local params = args.params - app = app or request.get_app_name() - file_path = file_path or encode.file_path( - request.get_app_basepath(), - 'app', app, module, chunk .. '.lua' + WEBMCP_BASE_PATH, 'app', app, module, chunk .. '.lua' ) local func, load_errmsg = loadfile(file_path) diff -r 48ee8826efbe -r eb3e236d261d framework/env/execute/config.lua --- a/framework/env/execute/config.lua Fri Jan 09 21:32:23 2015 +0100 +++ b/framework/env/execute/config.lua Fri Jan 09 22:28:39 2015 +0100 @@ -12,7 +12,7 @@ trace.enter_config{ name = name } execute.file_path{ file_path = encode.file_path( - request.get_app_basepath(), 'config', name .. '.lua' + WEBMCP_BASE_PATH, 'config', name .. '.lua' ) } trace.execution_return() diff -r 48ee8826efbe -r eb3e236d261d framework/env/execute/filtered_action.lua --- a/framework/env/execute/filtered_action.lua Fri Jan 09 21:32:23 2015 +0100 +++ b/framework/env/execute/filtered_action.lua Fri Jan 09 22:28:39 2015 +0100 @@ -8,29 +8,27 @@ } Executes an action with associated filters. -This function is only used by by the webmcp.lua file in the cgi-bin/ directory. --]]-- function execute.filtered_action(args) - local filters = {} - local function add_by_path(...) - execute._add_filters_by_path(filters, ...) - end - add_by_path("_filter") - add_by_path("_filter_action") - add_by_path(request.get_app_name(), "_filter") - add_by_path(request.get_app_name(), "_filter_action") - add_by_path(request.get_app_name(), args.module, "_filter") - add_by_path(request.get_app_name(), args.module, "_filter_action") - table.sort(filters) - for idx, filter_name in ipairs(filters) do - filters[idx] = filters[filter_name] - filters[filter_name] = nil - end local result execute.multi_wrapped( - filters, + execute._create_sorted_execution_list( + function(add_by_path) + add_by_path("_filter") + add_by_path("_filter_action") + add_by_path(WEBMCP_APP_NAME, "_filter") + add_by_path(WEBMCP_APP_NAME, "_filter_action") + add_by_path(WEBMCP_APP_NAME, args.module, "_filter") + add_by_path(WEBMCP_APP_NAME, args.module, "_filter_action") + end, + function(full_path, relative_path) + trace.enter_filter{ path = relative_path } + execute.file_path{ file_path = full_path } + trace.execution_return() + end + ), function() result = execute.action(args) end diff -r 48ee8826efbe -r eb3e236d261d framework/env/execute/filtered_view.lua --- a/framework/env/execute/filtered_view.lua Fri Jan 09 21:32:23 2015 +0100 +++ b/framework/env/execute/filtered_view.lua Fri Jan 09 22:28:39 2015 +0100 @@ -5,28 +5,26 @@ } Executes a view with associated filters. -This function is only used by by the webmcp.lua file in the cgi-bin/ directory. --]]-- function execute.filtered_view(args) - local filters = {} - local function add_by_path(...) - execute._add_filters_by_path(filters, ...) - end - add_by_path("_filter") - add_by_path("_filter_view") - add_by_path(request.get_app_name(), "_filter") - add_by_path(request.get_app_name(), "_filter_view") - add_by_path(request.get_app_name(), args.module, "_filter") - add_by_path(request.get_app_name(), args.module, "_filter_view") - table.sort(filters) - for idx, filter_name in ipairs(filters) do - filters[idx] = filters[filter_name] - filters[filter_name] = nil - end execute.multi_wrapped( - filters, + execute._create_sorted_execution_list( + function(add_by_path) + add_by_path("_filter") + add_by_path("_filter_view") + add_by_path(WEBMCP_APP_NAME, "_filter") + add_by_path(WEBMCP_APP_NAME, "_filter_view") + add_by_path(WEBMCP_APP_NAME, args.module, "_filter") + add_by_path(WEBMCP_APP_NAME, args.module, "_filter_view") + end, + function(full_path, relative_path) + trace.enter_filter{ path = relative_path } + execute.file_path{ file_path = full_path } + trace.execution_return() + end + ), function() execute.view(args) end diff -r 48ee8826efbe -r eb3e236d261d framework/env/execute/load_chunk.lua --- a/framework/env/execute/load_chunk.lua Fri Jan 09 21:32:23 2015 +0100 +++ b/framework/env/execute/load_chunk.lua Fri Jan 09 22:28:39 2015 +0100 @@ -11,8 +11,7 @@ NOTE: execute.load_chunk{...} is DEPRECATED and replaced by execute.chunk{...}. Both functions differ in interpretation of argument "chunk" regarding the filename extenstion '.lua'. -This function loads and executes a lua file specified by a given path or constructs -a path to load from the module and chunk name. +This function loads and executes a lua file specified by a given path or constructs a path to load from the module and chunk name. --]]-- diff -r 48ee8826efbe -r eb3e236d261d framework/env/execute/view.lua --- a/framework/env/execute/view.lua Fri Jan 09 21:32:23 2015 +0100 +++ b/framework/env/execute/view.lua Fri Jan 09 22:28:39 2015 +0100 @@ -16,8 +16,7 @@ trace.enter_view{ module = module, view = view } execute.file_path{ file_path = encode.file_path( - request.get_app_basepath(), - 'app', request.get_app_name(), module, view .. '.lua' + WEBMCP_BASE_PATH, 'app', request.get_app_name(), module, view .. '.lua' ), id = args.id, params = args.params