webmcp

view framework/bin/mcp.lua @ 226:17baf126ea88

Fixed typo in execute._create_sorted_execution_list(...)
author jbe
date Fri Feb 27 22:31:07 2015 +0100 (2015-02-27)
parents cd7fce06440d
children bf690b4be420
line source
1 #!/usr/bin/env moonbridge
3 WEBMCP_VERSION = "2.0.0_devel"
5 -- check if interactive mode
6 if listen then -- defined by moonbridge
7 WEBMCP_MODE = "listen"
8 else
9 WEBMCP_MODE = "interactive"
10 end
12 -- configuration names are provided as 4th, 5th, etc. argument
13 WEBMCP_CONFIG_NAMES = {select(4, ...)}
15 -- determine framework and bath path from command line arguments
16 -- or print usage synopsis (if applicable)
17 do
18 local arg1, arg2, arg3 = ...
19 local helpout
20 if
21 arg1 == "-h" or arg1 == "--help" or
22 arg2 == "-h" or arg2 == "--help" -- if first arg is provided by wrapper
23 then
24 helpout = io.stdout
25 elseif
26 #WEBMCP_CONFIG_NAMES < 1 or
27 (WEBMCP_MODE == "interactive") ~= (arg3 == "INTERACTIVE")
28 then
29 helpout = io.stderr
30 end
31 if helpout then
32 helpout:write("Usage: moonbridge -- <framework path>/bin/mcp.lua <framework path> <app base path> <app name> <config name> [<config name> ...]\n")
33 helpout:write(" or: lua -i <framework path>/bin/mcp.lua <framework path> <app base path> INTERACTIVE <config name> [<config name> ...]\n")
34 if helpout == io.stderr then
35 return 1
36 else
37 return 0
38 end
39 end
40 local function append_trailing_slash(str)
41 return string.gsub(str, "([^/])$", function(last) return last .. "/" end)
42 end
43 WEBMCP_FRAMEWORK_PATH = append_trailing_slash(arg1)
44 WEBMCP_BASE_PATH = append_trailing_slash(arg2)
45 if WEBMCP_MODE == "listen" then
46 WEBMCP_APP_NAME = arg3
47 end
48 end
50 -- setup search paths for libraries
51 do
52 if string.match(package.path, "^[^;]") then
53 package.path = ";" .. package.path
54 end
55 package.path = WEBMCP_FRAMEWORK_PATH .. "lib/?.lua" .. package.path
56 -- find out which file name extension shared libraries have
57 local slib_exts = {}
58 for ext in string.gmatch(package.cpath, "%?%.([A-Za-z0-9_-]+)") do
59 if not slib_exts[ext] then
60 slib_exts[#slib_exts+1] = ext
61 slib_exts[ext] = true
62 end
63 end
64 local paths = {}
65 for i, ext in ipairs(slib_exts) do
66 paths[#paths+1] = WEBMCP_FRAMEWORK_PATH .. "accelerator/?." .. ext
67 end
68 for i, ext in ipairs(slib_exts) do
69 paths[#paths+1] = WEBMCP_FRAMEWORK_PATH .. "lib/?." .. ext
70 end
71 paths[#paths+1] = package.cpath
72 package.cpath = table.concat(paths, ";")
73 end
75 -- autoloader system for WebMCP environment "$WEBMCP_FRAMEWORK_PATH/env/",
76 -- application environment extensions "$WEBMCP_BASE_PATH/env/"
77 -- and models "$WEBMCP_BASE_PATH/model/"
78 do
79 local weakkey_mt = { __mode = "k" }
80 local autoloader_category = setmetatable({}, weakkey_mt)
81 local autoloader_path = setmetatable({}, weakkey_mt)
82 local autoloader_mt = {}
83 local function install_autoloader(self, category, path_fragment)
84 autoloader_category[self] = category
85 autoloader_path[self] = path_fragment
86 setmetatable(self, autoloader_mt)
87 end
88 local function try_exec(filename)
89 local file = io.open(filename, "r")
90 if file then
91 local filedata = file:read("*a")
92 io.close(file)
93 local func, errmsg = load(filedata, "=" .. filename)
94 if func then
95 func()
96 return true
97 else
98 error(errmsg, 0)
99 end
100 else
101 return false
102 end
103 end
104 function autoloader_mt.__index(self, key)
105 local category, base_path, merge_base_path, file_key
106 local merge = false
107 if
108 string.find(key, "^[a-z_][A-Za-z0-9_]*$") and
109 not string.find(key, "^__")
110 then
111 category = "env"
112 base_path = WEBMCP_FRAMEWORK_PATH .. "env/"
113 merge = true
114 merge_base_path = WEBMCP_BASE_PATH .. "env/"
115 file_key = key
116 elseif string.find(key, "^[A-Z][A-Za-z0-9]*$") then
117 category = "model"
118 base_path = WEBMCP_BASE_PATH .. "model/"
119 local first = true
120 file_key = string.gsub(key, "[A-Z]",
121 function(c)
122 if first then
123 first = false
124 return string.lower(c)
125 else
126 return "_" .. string.lower(c)
127 end
128 end
129 )
130 else
131 return
132 end
133 local required_category = autoloader_category[self]
134 if required_category and required_category ~= category then return end
135 local path_fragment = autoloader_path[self]
136 local path = base_path .. path_fragment .. file_key
137 local merge_path
138 if merge then
139 merge_path = merge_base_path .. path_fragment .. file_key
140 end
141 local function try_dir(dirname)
142 local dir = io.open(dirname)
143 if dir then
144 io.close(dir)
145 local obj = {}
146 install_autoloader(obj, category, path_fragment .. file_key .. "/")
147 rawset(self, key, obj)
148 try_exec(path .. "/__init.lua")
149 if merge then try_exec(merge_path .. "/__init.lua") end
150 return true
151 else
152 return false
153 end
154 end
155 if merge and try_exec(merge_path .. ".lua") then
156 elseif merge and try_dir(merge_path .. "/") then
157 elseif try_exec(path .. ".lua") then
158 elseif try_dir(path .. "/") then
159 else end
160 return rawget(self, key)
161 end
162 install_autoloader(_G, nil, "")
163 try_exec(WEBMCP_FRAMEWORK_PATH .. "env/__init.lua")
164 try_exec(WEBMCP_BASE_PATH .. "env/__init.lua")
165 end
167 -- replace Moonbridge listen function
168 local moonbridge_listen = listen
169 local listeners = {}
170 function listen(args)
171 listeners[#listeners+1] = args
172 end
174 -- prohibit (unintended) definition of new global variables
175 _ENV = setmetatable({}, {
176 __index = _G,
177 __newindex = function()
178 error("Setting of global variable prohibited")
179 end
180 })
182 -- execute configurations and pre-fork initializers
183 for i, config_name in ipairs(WEBMCP_CONFIG_NAMES) do
184 execute.config(config_name)
185 end
186 execute.prefork_initializers()
188 -- interactive console mode
189 if WEBMCP_MODE == "interactive" then
190 execute.postfork_initializers()
191 trace.disable() -- avoids memory leakage (TODO: needs general solution for moonbridge?)
192 end
194 -- invoke moonbridge
195 if WEBMCP_MODE == "listen" then
196 local http = require("moonbridge_http")
197 for i, listener in ipairs(listeners) do
198 listener.prepare = execute.postfork_initializers
199 listener.connect = http.generate_handler(
200 request.handler,
201 request.get_http_options()
202 )
203 listener.finish = execute.finalizers
204 moonbridge_listen(listener)
205 end
206 end

Impressum / About Us