webmcp

changeset 206:eb3e236d261d

Code cleanup and work on Moonbridge integration
author jbe
date Fri Jan 09 22:28:39 2015 +0100 (2015-01-09)
parents 48ee8826efbe
children 77c4774e8342
files framework/bin/mcp.lua framework/env/__init.lua framework/env/execute/__init.lua framework/env/execute/_add_filters_by_path.lua framework/env/execute/_create_sorted_execution_list.lua framework/env/execute/action.lua framework/env/execute/chunk.lua framework/env/execute/config.lua framework/env/execute/filtered_action.lua framework/env/execute/filtered_view.lua framework/env/execute/load_chunk.lua framework/env/execute/view.lua
line diff
     1.1 --- a/framework/bin/mcp.lua	Fri Jan 09 21:32:23 2015 +0100
     1.2 +++ b/framework/bin/mcp.lua	Fri Jan 09 22:28:39 2015 +0100
     1.3 @@ -9,27 +9,27 @@
     1.4    WEBMCP_MODE = "interactive"
     1.5  end
     1.6  
     1.7 --- store extra command line arguments
     1.8 -local extraargs = {select(3, ...)}
     1.9 +-- configuration names are provided as 4th, 5th, etc. argument
    1.10 +WEBMCP_CONFIG_NAMES = {select(4, ...)}
    1.11  
    1.12  -- determine framework and bath path from command line arguments
    1.13  -- or print usage synopsis (if applicable)
    1.14  do
    1.15 -  local arg1, arg2 = ...
    1.16 +  local arg1, arg2, arg3 = ...
    1.17    local helpout
    1.18    if
    1.19      arg1 == "-h" or arg1 == "--help" or
    1.20 -    arg2 == "-h" or arg2 == "--help"
    1.21 +    arg2 == "-h" or arg2 == "--help"  -- if first arg is provided by wrapper
    1.22    then
    1.23      helpout = io.stdout
    1.24    elseif
    1.25 -    (WEBMCP_MODE == "listen" and (#extraargs < 2 or #extraargs % 2 ~= 0)) or
    1.26 -    (WEBMCP_MODE == "interactive" and arg2 == nil)
    1.27 +    #config_args < 1 or
    1.28 +    (WEBMCP_MODE == "interactive") ~= (arg3 == "INTERACTIVE")
    1.29    then
    1.30      helpout = io.stderr
    1.31    end
    1.32 -  helpout:write("Usage: moonbridge -- mcp.lua <framework path> <app base path> <app name 1> <config name 1> [<app name 2> <config name 2> ...]\n")
    1.33 -  helpout:write("   or: lua -i mcp.lua <framework path> <app base path> [<config name>]\n")
    1.34 +  helpout:write("Usage: moonbridge -- <framework path>/bin/mcp.lua <framework path> <app base path> <app name> <config name> [<config name> ...]\n")
    1.35 +  helpout:write("   or: lua -i <framework path>/bin/mcp.lua <framework path> <app base path> INTERACTIVE <config name> [<config name> ...]\n")
    1.36    if helpout then
    1.37      if helpout == io.stderr then
    1.38        return 1
    1.39 @@ -42,6 +42,9 @@
    1.40    end
    1.41    WEBMCP_FRAMEWORK_PATH = append_trailing_slash(arg1)
    1.42    WEBMCP_BASE_PATH      = append_trailing_slash(arg2)
    1.43 +  if WEBMCP_MODE == "listen" then
    1.44 +    WEBMCP_APP_NAME = arg3
    1.45 +  end
    1.46  end
    1.47  
    1.48  -- setup search paths for libraries
    1.49 @@ -175,43 +178,43 @@
    1.50    end
    1.51  })
    1.52  
    1.53 +-- execute configurations
    1.54 +for i, config_name in ipairs(WEBMCP_CONFIG_NAMES) do
    1.55 +  execute.config(config_name)
    1.56 +end
    1.57 +
    1.58  -- interactive console mode
    1.59  if WEBMCP_MODE == "interactive" then
    1.60    trace.disable()  -- avoids memory leakage (TODO: needs general solution for moonbridge?)
    1.61 -  local config_name = select(3, ...)
    1.62 -  if config_name then
    1.63 -    execute.config(config_name)
    1.64 -  end
    1.65 -  return
    1.66  end
    1.67  
    1.68  -- invoke moonbridge
    1.69 -local moonbridge_listen = listen
    1.70 -local listeners
    1.71 -function _G.listen(args)
    1.72 -  listeners[#listeners+1] = args
    1.73 -end
    1.74 -for i = 1, #extraargs/2 do
    1.75 -  local config = {}
    1.76 -  local function prepare_globals()
    1.77 -    _G.WEBMCP_APP_NAME = extraargs[2*i-1]
    1.78 -    _G.WEBMCP_CONFIG_NAME = extraargs[2*i]
    1.79 -    _G.config = config
    1.80 +if WEBMCP_MODE == "listen" then
    1.81 +  local moonbridge_listen = listen
    1.82 +  local listeners
    1.83 +  function _G.listen(args)
    1.84 +    listeners[#listeners+1] = args
    1.85    end
    1.86 -  prepare_globals()
    1.87 -  listeners = {}
    1.88 -  execute.config(config_name)
    1.89 -  for i, listener in ipairs(listeners) do
    1.90 -    function listener.prepare()
    1.91 -      prepare_globals()
    1.92 -      request.execute_preparers()
    1.93 +  for i = 1, #extraargs/2 do
    1.94 +    local config = {}
    1.95 +    listeners = {}
    1.96 +    execute.config(config_name)
    1.97 +    for i, listener in ipairs(listeners) do
    1.98 +      listener.prepare = execute.prefork_initializers
    1.99 +      listener.connect = http.generate_handler(
   1.100 +        request.get_http_options(),
   1.101 +        function(req)
   1.102 +          execute.postfork_initializers()
   1.103 +          request.handler(req)
   1.104 +        end
   1.105 +      )
   1.106 +      --listener.finish = ???  -- TODO: requires coroutines and execute.inner() for initializers?
   1.107 +      moonbridge_listen(listener)
   1.108      end
   1.109 -    listener.connect = request.connect
   1.110 -    listener.finish = request.execute_finishers
   1.111    end
   1.112  end
   1.113  
   1.114 ---[[ TODO: following lines to be moved to request.connect(...)
   1.115 +--[[ TODO: following lines to be moved to execute.server(...)
   1.116  
   1.117  local success, error_info = xpcall(
   1.118    function()
     2.1 --- a/framework/env/__init.lua	Fri Jan 09 21:32:23 2015 +0100
     2.2 +++ b/framework/env/__init.lua	Fri Jan 09 22:28:39 2015 +0100
     2.3 @@ -46,4 +46,21 @@
     2.4  atom        = require 'atom'
     2.5  json        = require 'json'
     2.6  require 'mondelefant_atom_connector'
     2.7 +http        = require 'http'
     2.8  
     2.9 +--[[--
    2.10 +app  -- table to store an application state
    2.11 +
    2.12 +'app' is a global table for storing any application state data
    2.13 +--]]--
    2.14 +app = {}
    2.15 +--//--
    2.16 +
    2.17 +--[[--
    2.18 +config  -- table to store application configuration
    2.19 +
    2.20 +'config' is a global table, which can be modified by a config file of an application to modify the behaviour of that application.
    2.21 +--]]--
    2.22 +config = {}
    2.23 +--//--
    2.24 +
     3.1 --- a/framework/env/execute/__init.lua	Fri Jan 09 21:32:23 2015 +0100
     3.2 +++ b/framework/env/execute/__init.lua	Fri Jan 09 22:28:39 2015 +0100
     3.3 @@ -1,1 +1,1 @@
     3.4 -execute._wrap_stack = {}
     3.5 +execute._wrap_stack = {}  -- used by execute.inner() and execute.wrapped(...)
     4.1 --- a/framework/env/execute/_add_filters_by_path.lua	Fri Jan 09 21:32:23 2015 +0100
     4.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.3 @@ -1,25 +0,0 @@
     4.4 -function execute._add_filters_by_path(filter_list, ...)
     4.5 -  local full_path     = encode.file_path(request.get_app_basepath(), "app", ...)
     4.6 -  local relative_path = encode.file_path("", ...)
     4.7 -  local filter_names = extos.listdir(full_path)
     4.8 -  if filter_names then
     4.9 -    table.sort(filter_names)  -- not really neccessary, due to sorting afterwards
    4.10 -    for i, filter_name in ipairs(filter_names) do
    4.11 -      if string.find(filter_name, "%.lua$") then
    4.12 -        if filter_list[filter_name] then
    4.13 -          error('More than one filter is named "' .. filter_name .. '".')
    4.14 -        end
    4.15 -        table.insert(filter_list, filter_name)
    4.16 -        filter_list[filter_name] = function()
    4.17 -          trace.enter_filter{
    4.18 -            path = encode.file_path(relative_path, filter_name)
    4.19 -          }
    4.20 -          execute.file_path{
    4.21 -            file_path = encode.file_path(full_path, filter_name)
    4.22 -          }
    4.23 -          trace.execution_return()
    4.24 -        end
    4.25 -      end
    4.26 -    end
    4.27 -  end
    4.28 -end
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/framework/env/execute/_create_sorted_execution_list.lua	Fri Jan 09 22:28:39 2015 +0100
     5.3 @@ -0,0 +1,31 @@
     5.4 +function execute._create_sorted_execution_list(add_paths, execute_func)
     5.5 +  local entries = {}
     5.6 +  add_paths(function(...)
     5.7 +    local full_path     = encode.file_path(WEBMCP_BASE_PATH, "app", ...)
     5.8 +    local relative_path = encode.file_path("", ...)
     5.9 +    local filenames = extos.listdir(full_path)
    5.10 +    if filenames then
    5.11 +      table.sort(filenames)  -- not really neccessary, due to sorting afterwards
    5.12 +      for i, filename in ipairs(filenames) do
    5.13 +        if string.find(filename, "%.lua$") then
    5.14 +          if entries[filename] then
    5.15 +            error('More than one filter or initializer is named "' .. filter_name .. '".')
    5.16 +          end
    5.17 +          entries[#entries+1] = filename
    5.18 +          entries[filename] = function()
    5.19 +            execute_func(
    5.20 +              encode.file_path(full_path, filename),
    5.21 +              encode.file_path(relative_path, filename)
    5.22 +            )
    5.23 +          end
    5.24 +        end
    5.25 +      end
    5.26 +    end
    5.27 +  end)
    5.28 +  table.sort(entries)
    5.29 +  for idx, filename in ipairs(entries) do
    5.30 +    entries[idx] = enteries[filename]
    5.31 +    entries[filename] = nil
    5.32 +  end
    5.33 +  return entries
    5.34 +end
     6.1 --- a/framework/env/execute/action.lua	Fri Jan 09 21:32:23 2015 +0100
     6.2 +++ b/framework/env/execute/action.lua	Fri Jan 09 22:28:39 2015 +0100
     6.3 @@ -8,7 +8,6 @@
     6.4  }
     6.5  
     6.6  Executes an action without associated filters.
     6.7 -This function is only used by execute.filtered_action{...}, which itself is only used by the webmcp.lua file in the cgi-bin/ directory.
     6.8  
     6.9  --]]--
    6.10  
    6.11 @@ -18,8 +17,7 @@
    6.12    trace.enter_action{ module = module, action = action }
    6.13    local action_status = execute.file_path{
    6.14      file_path = encode.file_path(
    6.15 -      request.get_app_basepath(),
    6.16 -      'app', request.get_app_name(), module, '_action', action .. '.lua'
    6.17 +      WEBMCP_BASE_PATH, 'app', WEBMCP_APP_NAME, module, '_action', action .. '.lua'
    6.18      ),
    6.19      id     = args.id,
    6.20      params = args.params
     7.1 --- a/framework/env/execute/chunk.lua	Fri Jan 09 21:32:23 2015 +0100
     7.2 +++ b/framework/env/execute/chunk.lua	Fri Jan 09 22:28:39 2015 +0100
     7.3 @@ -24,17 +24,14 @@
     7.4  
     7.5  function execute.chunk(args)
     7.6    local file_path = args.file_path
     7.7 -  local app       = args.app
     7.8 +  local app       = args.app or WEBMCP_APP_NAME
     7.9    local module    = args.module
    7.10    local chunk     = args.chunk
    7.11    local id        = args.id
    7.12    local params    = args.params
    7.13  
    7.14 -  app = app or request.get_app_name()
    7.15 -
    7.16    file_path = file_path or encode.file_path(
    7.17 -    request.get_app_basepath(),
    7.18 -    'app', app, module, chunk .. '.lua'
    7.19 +    WEBMCP_BASE_PATH, 'app', app, module, chunk .. '.lua'
    7.20    )
    7.21  
    7.22    local func, load_errmsg = loadfile(file_path)
     8.1 --- a/framework/env/execute/config.lua	Fri Jan 09 21:32:23 2015 +0100
     8.2 +++ b/framework/env/execute/config.lua	Fri Jan 09 22:28:39 2015 +0100
     8.3 @@ -12,7 +12,7 @@
     8.4    trace.enter_config{ name = name }
     8.5    execute.file_path{
     8.6      file_path = encode.file_path(
     8.7 -      request.get_app_basepath(), 'config', name .. '.lua'
     8.8 +      WEBMCP_BASE_PATH, 'config', name .. '.lua'
     8.9      )
    8.10    }
    8.11    trace.execution_return()
     9.1 --- a/framework/env/execute/filtered_action.lua	Fri Jan 09 21:32:23 2015 +0100
     9.2 +++ b/framework/env/execute/filtered_action.lua	Fri Jan 09 22:28:39 2015 +0100
     9.3 @@ -8,29 +8,27 @@
     9.4  }
     9.5  
     9.6  Executes an action with associated filters.
     9.7 -This function is only used by by the webmcp.lua file in the cgi-bin/ directory.
     9.8  
     9.9  --]]--
    9.10  
    9.11  function execute.filtered_action(args)
    9.12 -  local filters = {}
    9.13 -  local function add_by_path(...)
    9.14 -    execute._add_filters_by_path(filters, ...)
    9.15 -  end
    9.16 -  add_by_path("_filter")
    9.17 -  add_by_path("_filter_action")
    9.18 -  add_by_path(request.get_app_name(), "_filter")
    9.19 -  add_by_path(request.get_app_name(), "_filter_action")
    9.20 -  add_by_path(request.get_app_name(), args.module, "_filter")
    9.21 -  add_by_path(request.get_app_name(), args.module, "_filter_action")
    9.22 -  table.sort(filters)
    9.23 -  for idx, filter_name in ipairs(filters) do
    9.24 -    filters[idx] = filters[filter_name]
    9.25 -    filters[filter_name] = nil
    9.26 -  end
    9.27    local result
    9.28    execute.multi_wrapped(
    9.29 -    filters,
    9.30 +    execute._create_sorted_execution_list(
    9.31 +      function(add_by_path)
    9.32 +        add_by_path("_filter")
    9.33 +        add_by_path("_filter_action")
    9.34 +        add_by_path(WEBMCP_APP_NAME, "_filter")
    9.35 +        add_by_path(WEBMCP_APP_NAME, "_filter_action")
    9.36 +        add_by_path(WEBMCP_APP_NAME, args.module, "_filter")
    9.37 +        add_by_path(WEBMCP_APP_NAME, args.module, "_filter_action")
    9.38 +      end,
    9.39 +      function(full_path, relative_path)
    9.40 +        trace.enter_filter{ path = relative_path }
    9.41 +        execute.file_path{ file_path = full_path }
    9.42 +        trace.execution_return()
    9.43 +      end
    9.44 +    ),
    9.45      function()
    9.46        result = execute.action(args)
    9.47      end
    10.1 --- a/framework/env/execute/filtered_view.lua	Fri Jan 09 21:32:23 2015 +0100
    10.2 +++ b/framework/env/execute/filtered_view.lua	Fri Jan 09 22:28:39 2015 +0100
    10.3 @@ -5,28 +5,26 @@
    10.4  }
    10.5  
    10.6  Executes a view with associated filters.
    10.7 -This function is only used by by the webmcp.lua file in the cgi-bin/ directory.
    10.8  
    10.9  --]]--
   10.10  
   10.11  function execute.filtered_view(args)
   10.12 -  local filters = {}
   10.13 -  local function add_by_path(...)
   10.14 -    execute._add_filters_by_path(filters, ...)
   10.15 -  end
   10.16 -  add_by_path("_filter")
   10.17 -  add_by_path("_filter_view")
   10.18 -  add_by_path(request.get_app_name(), "_filter")
   10.19 -  add_by_path(request.get_app_name(), "_filter_view")
   10.20 -  add_by_path(request.get_app_name(), args.module, "_filter")
   10.21 -  add_by_path(request.get_app_name(), args.module, "_filter_view")
   10.22 -  table.sort(filters)
   10.23 -  for idx, filter_name in ipairs(filters) do
   10.24 -    filters[idx] = filters[filter_name]
   10.25 -    filters[filter_name] = nil
   10.26 -  end
   10.27    execute.multi_wrapped(
   10.28 -    filters,
   10.29 +    execute._create_sorted_execution_list(
   10.30 +      function(add_by_path)
   10.31 +        add_by_path("_filter")
   10.32 +        add_by_path("_filter_view")
   10.33 +        add_by_path(WEBMCP_APP_NAME, "_filter")
   10.34 +        add_by_path(WEBMCP_APP_NAME, "_filter_view")
   10.35 +        add_by_path(WEBMCP_APP_NAME, args.module, "_filter")
   10.36 +        add_by_path(WEBMCP_APP_NAME, args.module, "_filter_view")
   10.37 +      end,
   10.38 +      function(full_path, relative_path)
   10.39 +        trace.enter_filter{ path = relative_path }
   10.40 +        execute.file_path{ file_path = full_path }
   10.41 +        trace.execution_return()
   10.42 +      end
   10.43 +    ),
   10.44      function()
   10.45        execute.view(args)
   10.46      end
    11.1 --- a/framework/env/execute/load_chunk.lua	Fri Jan 09 21:32:23 2015 +0100
    11.2 +++ b/framework/env/execute/load_chunk.lua	Fri Jan 09 22:28:39 2015 +0100
    11.3 @@ -11,8 +11,7 @@
    11.4  
    11.5  NOTE: execute.load_chunk{...} is DEPRECATED and replaced by execute.chunk{...}. Both functions differ in interpretation of argument "chunk" regarding the filename extenstion '.lua'.
    11.6    
    11.7 -This function loads and executes a lua file specified by a given path or constructs 
    11.8 -a path to load from the module and chunk name.
    11.9 +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.
   11.10  
   11.11  --]]--
   11.12  
    12.1 --- a/framework/env/execute/view.lua	Fri Jan 09 21:32:23 2015 +0100
    12.2 +++ b/framework/env/execute/view.lua	Fri Jan 09 22:28:39 2015 +0100
    12.3 @@ -16,8 +16,7 @@
    12.4    trace.enter_view{ module = module, view = view }
    12.5    execute.file_path{
    12.6      file_path = encode.file_path(
    12.7 -      request.get_app_basepath(),
    12.8 -      'app', request.get_app_name(), module, view .. '.lua'
    12.9 +      WEBMCP_BASE_PATH, 'app', request.get_app_name(), module, view .. '.lua'
   12.10      ),
   12.11      id     = args.id,
   12.12      params = args.params

Impressum / About Us