webmcp

view framework/cgi-bin/webmcp.lua @ 65:a54cc7dcabf1

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

Impressum / About Us