webmcp

view framework/cgi-bin/webmcp.lua @ 49:07ddd0da7479

Version number changed to 1.1.2
author jbe
date Mon Nov 08 01:53:55 2010 +0100 (2010-11-08)
parents 3198806c7904
children 382f042d99a3
line source
1 #!/usr/bin/env lua
3 _WEBMCP_VERSION = "1.1.2"
5 -- include "../lib/" in search path for libraries
6 if not WEBMCP_PATH then
7 WEBMCP_PATH = "../"
8 end
10 do
11 package.path = WEBMCP_PATH .. 'lib/?.lua;' .. package.path
12 -- find out which file name extension shared libraries have
13 local slib_exts = {}
14 for ext in string.gmatch(package.cpath, "%?%.([A-Za-z0-9_-]+)") do
15 slib_exts[ext] = true
16 end
17 local paths = {}
18 for ext in pairs(slib_exts) do
19 paths[#paths+1] = WEBMCP_PATH .. "accelerator/?." .. ext
20 end
21 for ext in pairs(slib_exts) do
22 paths[#paths+1] = WEBMCP_PATH .. "lib/?." .. ext
23 end
24 paths[#paths+1] = package.cpath
25 package.cpath = table.concat(paths, ";")
26 end
28 -- load os extensions for lua
29 -- (should happen as soon as possible due to run time measurement)
30 require 'extos'
32 -- load nihil library
33 require 'nihil'
35 -- load random generator library
36 require 'multirand'
38 -- load rocketcgi library and map it to cgi
39 do
40 local option = os.getenv("WEBMCP_INTERACTIVE")
41 if option and option ~= "" and option ~= "0" then
42 cgi = nil
43 else
44 require 'rocketcgi'
45 cgi = rocketcgi
46 end
47 end
49 -- load database access library with object relational mapper
50 require 'mondelefant'
51 mondelefant.connection_prototype.error_objects = true
53 -- load type system "atom"
54 require 'atom'
56 -- load mondelefant atom connector
57 require 'mondelefant_atom_connector'
59 --[[--
60 cloned_table = -- newly generated table
61 table.new(
62 table_or_nil -- keys of a given table will be copied to the new table
63 )
65 If a table is given, then a cloned table is returned.
66 If nil is given, then a new empty table is returned.
68 --]]--
69 function table.new(tbl)
70 new_tbl = {}
71 if tbl then
72 for key, value in pairs(tbl) do
73 new_tbl[key] = value
74 end
75 end
76 return new_tbl
77 end
78 --//--
80 --[[--
81 at_exit(
82 func -- function to be called before the process is ending
83 )
85 Registers a function to be called before the CGI process is exiting.
86 --]]--
87 do
88 local exit_handlers = {}
89 function at_exit(func)
90 table.insert(exit_handlers, func)
91 end
92 function exit(code)
93 for i = #exit_handlers, 1, -1 do
94 exit_handlers[i]()
95 end
96 os.exit(code)
97 end
98 end
99 --//--
101 --[[--
102 app -- table to store an application state
104 'app' is a global table for storing any application state data
105 --]]--
106 app = {}
107 --//--
109 --[[--
110 config -- table to store application configuration
112 'config' is a global table, which can be modified by a config file of an application to modify the behaviour of that application.
113 --]]--
114 config = {}
115 --//--
117 -- autoloader system for WebMCP environment "../env/",
118 -- application environment extensions "$WEBMCP_APP_BASE/env/"
119 -- and models "$WEBMCP_APP_BASE/model/"
120 do
121 local app_base = os.getenv("WEBMCP_APP_BASEPATH")
122 if not app_base then
123 error(
124 "Failed to initialize autoloader " ..
125 "due to unset WEBMCP_APP_BASEPATH environment variable."
126 )
127 end
128 local weakkey_mt = { __mode = "k" }
129 local autoloader_category = setmetatable({}, weakkey_mt)
130 local autoloader_path = setmetatable({}, weakkey_mt)
131 local autoloader_mt = {}
132 local function install_autoloader(self, category, path)
133 autoloader_category[self] = category
134 autoloader_path[self] = path
135 setmetatable(self, autoloader_mt)
136 end
137 local function try_exec(filename)
138 local file = io.open(filename, "r")
139 if file then
140 local filedata = file:read("*a")
141 io.close(file)
142 local func, errmsg = loadstring(filedata, "=" .. filename)
143 if func then
144 func()
145 return true
146 else
147 error(errmsg, 0)
148 end
149 else
150 return false
151 end
152 end
153 local function compose_path_string(base, path, key)
154 return string.gsub(
155 base .. table.concat(path, "/") .. "/" .. key, "/+", "/"
156 )
157 end
158 function autoloader_mt.__index(self, key)
159 local category, base_path, merge_base_path, file_key
160 local merge = false
161 if
162 string.find(key, "^[a-z_][A-Za-z0-9_]*$") and
163 not string.find(key, "^__")
164 then
165 category = "env"
166 base_path = WEBMCP_PATH .. "/env/"
167 merge = true
168 merge_base_path = app_base .. "/env/"
169 file_key = key
170 elseif string.find(key, "^[A-Z][A-Za-z0-9]*$") then
171 category = "model"
172 base_path = app_base .. "/model/"
173 local first = true
174 file_key = string.gsub(key, "[A-Z]",
175 function(c)
176 if first then
177 first = false
178 return string.lower(c)
179 else
180 return "_" .. string.lower(c)
181 end
182 end
183 )
184 else
185 return
186 end
187 local required_category = autoloader_category[self]
188 if required_category and required_category ~= category then return end
189 local path = autoloader_path[self]
190 local path_string = compose_path_string(base_path, path, file_key)
191 local merge_path_string
192 if merge then
193 merge_path_string = compose_path_string(
194 merge_base_path, path, file_key
195 )
196 end
197 local function try_dir(dirname)
198 local dir = io.open(dirname)
199 if dir then
200 io.close(dir)
201 local obj = {}
202 local sub_path = {}
203 for i, v in ipairs(path) do sub_path[i] = v end
204 table.insert(sub_path, file_key)
205 install_autoloader(obj, category, sub_path)
206 rawset(self, key, obj)
207 try_exec(path_string .. "/__init.lua")
208 if merge then try_exec(merge_path_string .. "/__init.lua") end
209 return true
210 else
211 return false
212 end
213 end
214 if merge and try_exec(merge_path_string .. ".lua") then
215 elseif merge and try_dir(merge_path_string .. "/") then
216 elseif try_exec(path_string .. ".lua") then
217 elseif try_dir(path_string .. "/") then
218 else end
219 return rawget(self, key)
220 end
221 install_autoloader(_G, nil, {})
222 try_exec(WEBMCP_PATH .. "env/__init.lua")
223 end
225 -- interactive console mode
226 if not cgi then
227 local config_name = request.get_config_name()
228 if config_name then
229 execute.config(config_name)
230 end
231 return
232 end
234 local success, error_info = xpcall(
235 function()
237 -- execute configuration file
238 do
239 local config_name = request.get_config_name()
240 if config_name then
241 execute.config(config_name)
242 end
243 end
245 -- restore slots if coming from http redirect
246 if cgi.params.tempstore then
247 trace.restore_slots{}
248 local blob = tempstore.pop(cgi.params.tempstore)
249 if blob then slot.restore_all(blob) end
250 end
252 local function file_exists(filename)
253 local file = io.open(filename, "r")
254 if file then
255 io.close(file)
256 return true
257 else
258 return false
259 end
260 end
262 if cgi.params["_webmcp_404"] then
263 request.force_absolute_baseurl()
264 request.set_status("404 Not Found")
265 if request.get_404_route() then
266 request.forward(request.get_404_route())
267 else
268 error("No 404 page set.")
269 end
270 elseif request.get_action() then
271 trace.request{
272 module = request.get_module(),
273 action = request.get_action()
274 }
275 if
276 request.get_404_route() and
277 not file_exists(
278 encode.action_file_path{
279 module = request.get_module(),
280 action = request.get_action()
281 }
282 )
283 then
284 request.set_status("404 Not Found")
285 request.forward(request.get_404_route())
286 else
287 if cgi.method ~= "POST" then
288 request.set_status("405 Method Not Allowed")
289 cgi.add_header("Allow: POST")
290 error("Tried to invoke an action with a GET request.")
291 end
292 local action_status = execute.filtered_action{
293 module = request.get_module(),
294 action = request.get_action(),
295 }
296 if not request.is_rerouted() then
297 local routing_mode, routing_module, routing_view
298 routing_mode = cgi.params["_webmcp_routing." .. action_status .. ".mode"]
299 routing_module = cgi.params["_webmcp_routing." .. action_status .. ".module"]
300 routing_view = cgi.params["_webmcp_routing." .. action_status .. ".view"]
301 if not (routing_mode or routing_module or routing_view) then
302 action_status = "default"
303 routing_mode = cgi.params["_webmcp_routing.default.mode"]
304 routing_module = cgi.params["_webmcp_routing.default.module"]
305 routing_view = cgi.params["_webmcp_routing.default.view"]
306 end
307 assert(routing_module, "Routing information has no module.")
308 assert(routing_view, "Routing information has no view.")
309 if routing_mode == "redirect" then
310 local routing_params = {}
311 for key, value in pairs(cgi.params) do
312 local status, stripped_key = string.match(
313 key, "^_webmcp_routing%.([^%.]*)%.params%.(.*)$"
314 )
315 if status == action_status then
316 routing_params[stripped_key] = value
317 end
318 end
319 request.redirect{
320 module = routing_module,
321 view = routing_view,
322 id = cgi.params["_webmcp_routing." .. action_status .. ".id"],
323 params = routing_params
324 }
325 elseif routing_mode == "forward" then
326 request.forward{ module = routing_module, view = routing_view }
327 else
328 error("Missing or unknown routing mode in request parameters.")
329 end
330 end
331 end
332 else
333 -- no action
334 trace.request{
335 module = request.get_module(),
336 view = request.get_view()
337 }
338 if
339 request.get_404_route() and
340 not file_exists(
341 encode.view_file_path{
342 module = request.get_module(),
343 view = request.get_view()
344 }
345 )
346 then
347 request.set_status("404 Not Found")
348 request.forward(request.get_404_route())
349 end
350 end
352 if not request.get_redirect_data() then
353 request.process_forward()
354 local view = request.get_view()
355 if string.find(view, "^_") then
356 error("Tried to call a private view (prefixed with underscore).")
357 end
358 execute.filtered_view{
359 module = request.get_module(),
360 view = view,
361 }
362 end
364 -- force error due to missing absolute base URL until its too late to display error message
365 --if request.get_redirect_data() then
366 -- request.get_absolute_baseurl()
367 --end
369 end,
371 function(errobj)
372 return {
373 errobj = errobj,
374 stacktrace = string.gsub(
375 debug.traceback('', 2),
376 "^\r?\n?stack traceback:\r?\n?", ""
377 )
378 }
379 end
380 )
382 if not success then trace.error{} end
384 -- laufzeitermittlung
385 trace.exectime{ real = os.monotonic_hires_time(), cpu = os.clock() }
387 slot.select('trace', trace.render) -- render trace information
389 local redirect_data = request.get_redirect_data()
391 -- log error and switch to error layout, unless success
392 if not success then
393 local errobj = error_info.errobj
394 local stacktrace = error_info.stacktrace
395 if not request.get_status() and not request.get_json_request_slots() then
396 request.set_status("500 Internal Server Error")
397 end
398 slot.set_layout('system_error')
399 slot.select('system_error', function()
400 if getmetatable(errobj) == mondelefant.errorobject_metatable then
401 slot.put(
402 "<p>Database error of class <b>",
403 encode.html(errobj.code),
404 "</b> occured:<br/><b>",
405 encode.html(errobj.message),
406 "</b></p>"
407 )
408 else
409 slot.put("<p><b>", encode.html(tostring(errobj)), "</b></p>")
410 end
411 slot.put("<p>Stack trace follows:<br/>")
412 slot.put(encode.html_newlines(encode.html(stacktrace)))
413 slot.put("</p>")
414 end)
415 elseif redirect_data then
416 local redirect_params = {}
417 for key, value in pairs(redirect_data.params) do
418 redirect_params[key] = value
419 end
420 local slot_dump = slot.dump_all()
421 if slot_dump ~= "" then
422 redirect_params.tempstore = tempstore.save(slot_dump)
423 end
424 local json_request_slots = request.get_json_request_slots()
425 if json_request_slots then
426 redirect_params["_webmcp_json_slots[]"] = json_request_slots
427 end
428 cgi.redirect(
429 encode.url{
430 base = request.get_absolute_baseurl(),
431 module = redirect_data.module,
432 view = redirect_data.view,
433 id = redirect_data.id,
434 params = redirect_params
435 }
436 )
437 cgi.send_data()
438 end
440 if not success or not redirect_data then
442 local http_status = request.get_status()
443 if http_status then
444 cgi.set_status(http_status)
445 end
447 local json_request_slots = request.get_json_request_slots()
448 if json_request_slots then
449 cgi.set_content_type('application/json')
450 local data = {}
451 for idx, slot_ident in ipairs(json_request_slots) do
452 data[slot_ident] = slot.get_content(slot_ident)
453 end
454 cgi.send_data(encode.json(data))
455 else
456 cgi.set_content_type(slot.get_content_type())
457 cgi.send_data(slot.render_layout())
458 end
459 end
461 exit()

Impressum / About Us