webmcp
diff framework/bin/mcp.lua @ 206:eb3e236d261d
Code cleanup and work on Moonbridge integration
author | jbe |
---|---|
date | Fri Jan 09 22:28:39 2015 +0100 (2015-01-09) |
parents | b059efd81649 |
children | 77c4774e8342 |
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()