webmcp

view framework/bin/mcp.lua @ 218:15c9de7832cc

Removed unnecessary extra slash in autoloader in mcp.lua
author jbe
date Sun Feb 22 13:03:34 2015 +0100 (2015-02-22)
parents 7f9c9c4434a1
children db106eb273ea
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)
84 autoloader_category[self] = category
85 autoloader_path[self] = path
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 local function compose_path_string(base, path, key)
105 if #path == 0 then
106 return base .. key
107 else
108 return base .. table.concat(path, "/") .. "/" .. key
109 end
110 end
111 function autoloader_mt.__index(self, key)
112 local category, base_path, merge_base_path, file_key
113 local merge = false
114 if
115 string.find(key, "^[a-z_][A-Za-z0-9_]*$") and
116 not string.find(key, "^__")
117 then
118 category = "env"
119 base_path = WEBMCP_FRAMEWORK_PATH .. "env/"
120 merge = true
121 merge_base_path = WEBMCP_BASE_PATH .. "env/"
122 file_key = key
123 elseif string.find(key, "^[A-Z][A-Za-z0-9]*$") then
124 category = "model"
125 base_path = WEBMCP_BASE_PATH .. "model/"
126 local first = true
127 file_key = string.gsub(key, "[A-Z]",
128 function(c)
129 if first then
130 first = false
131 return string.lower(c)
132 else
133 return "_" .. string.lower(c)
134 end
135 end
136 )
137 else
138 return
139 end
140 local required_category = autoloader_category[self]
141 if required_category and required_category ~= category then return end
142 local path = autoloader_path[self]
143 local path_string = compose_path_string(base_path, path, file_key)
144 local merge_path_string
145 if merge then
146 merge_path_string = compose_path_string(
147 merge_base_path, path, file_key
148 )
149 end
150 local function try_dir(dirname)
151 local dir = io.open(dirname)
152 if dir then
153 io.close(dir)
154 local obj = {}
155 local sub_path = {}
156 for i = 1, #path do sub_path[i] = path[i] end
157 sub_path[#path+1] = file_key
158 install_autoloader(obj, category, sub_path)
159 rawset(self, key, obj)
160 try_exec(path_string .. "/__init.lua")
161 if merge then try_exec(merge_path_string .. "/__init.lua") end
162 return true
163 else
164 return false
165 end
166 end
167 if merge and try_exec(merge_path_string .. ".lua") then
168 elseif merge and try_dir(merge_path_string .. "/") then
169 elseif try_exec(path_string .. ".lua") then
170 elseif try_dir(path_string .. "/") then
171 else end
172 return rawget(self, key)
173 end
174 install_autoloader(_G, nil, {})
175 try_exec(WEBMCP_FRAMEWORK_PATH .. "env/__init.lua")
176 try_exec(WEBMCP_BASE_PATH .. "env/__init.lua")
177 end
179 -- replace Moonbridge listen function
180 local moonbridge_listen = listen
181 local listeners
182 function listen(args)
183 listeners[#listeners+1] = args
184 end
186 -- prohibit (unintended) definition of new global variables
187 _ENV = setmetatable({}, {
188 __index = _G,
189 __newindex = function()
190 error("Setting of global variable prohibited")
191 end
192 })
194 -- execute configurations
195 for i, config_name in ipairs(WEBMCP_CONFIG_NAMES) do
196 execute.config(config_name)
197 execute.prefork_initializers()
198 end
200 -- interactive console mode
201 if WEBMCP_MODE == "interactive" then
202 execute.postfork_initializers()
203 trace.disable() -- avoids memory leakage (TODO: needs general solution for moonbridge?)
204 end
206 -- invoke moonbridge
207 if WEBMCP_MODE == "listen" then
208 local http = require("moonbridge_http")
209 for i, listener in ipairs(listeners) do
210 listener.prepare = execute.postfork_initializers
211 listener.connect = http.generate_handler(
212 request.handler,
213 request.get_http_options()
214 )
215 listener.finish = execute.finalizers
216 moonbridge_listen(listener)
217 end
218 end

Impressum / About Us