webmcp

view doc/autodoc-header.htmlpart @ 402:04172238d79b

Do not require "engine" field to be set for mondelefant.connect{...}; Fixed bugs in mondelefant.connect{...} that could have crashed Lua; Shortened Lua registry key for mondelefant library
author jbe
date Wed Jan 06 02:39:50 2016 +0100 (2016-01-06)
parents a932b94a8490
children 29b1f7a04934
line source
1 <html>
2 <head>
3 <script type="text/javascript">
4 var expandedSections = {};
5 function toggleSection(ident) {
6 var element = document.getElementById('autodoc_details_' + ident);
7 if (element) {
8 if (expandedSections[ident]) {
9 element.style.display = 'none';
10 delete expandedSections[ident];
11 } else {
12 element.style.display = '';
13 expandedSections[ident] = true;
14 }
15 }
16 }
17 var inProgress = false;
18 window.onload = window.onhashchange = function() {
19 if (inProgress) return;
20 for (var ident in expandedSections) toggleSection(ident);
21 var hash = window.location.hash;
22 if (hash) {
23 toggleSection(hash.replace(/^#/, ''));
24 inProgress = true;
25 window.location.hash = hash; // required after collapsing/expanding
26 inProgress = false;
27 }
28 };
29 </script>
30 <style>
31 body {
32 font-family: "Liberation Sans", sans-serif;
33 font-size: 11pt;
34 padding-bottom: 5ex;
35 }
36 .warning {
37 color: #ff0000;
38 }
39 h1, h2 {
40 font-family: "Liberation Serif", Georgia, serif;
41 }
42 h2 {
43 margin-bottom: 0.3ex;
44 }
45 p {
46 margin: 0px;
47 line-height: 130%;
48 }
49 tt, pre {
50 font-size: 10pt;
51 }
52 tt {
53 font-weight: bold;
54 white-space: nowrap;
55 }
56 .autodoc_entry {
57 margin-top: 1ex;
58 margin-bottom: 1ex;
59 }
60 .autodoc_comment_tail {
61 font-style: italic;
62 }
63 .autodoc_entry .short_synopsis {
64 cursor: pointer;
65 }
66 .autodoc_details {
67 padding-left: 1em;
68 padding-right: 1em;
69 border: 1px solid #777;
70 }
71 .autodoc_synopsis {
72 font-weight: bold;
73 }
74 .autodoc_synopsis .autodoc_comment_tail {
75 font-weight: normal;
76 color: #008000;
77 }
78 .autodoc_entry .autodoc_comment {
79 color: #400080;
80 }
81 .autodoc_source {
82 color: #505050;
83 }
84 </style>
85 <title>WebMCP 2.0.4 Documentation</title>
86 </head>
87 <body>
88 <h1>WebMCP 2.0.4 Documentation</h1>
89 <p>
90 WebMCP is a web development framework based on the Lua programming language (read more about Lua <a href="http://www.lua.org/about.html">here</a>).
91 </p>
92 <h2>Requirements</h2>
93 <p>
94 WebMCP has been developed on Linux and FreeBSD. Using it with Mac&nbsp;OS&nbsp;X is untested as of yet; Microsoft Windows is not supported. Beside the operating system, the only mandatory dependencies for WebMCP are the <a href="http://www.lua.org/">programming language Lua</a> version 5.2 or 5.3, the <a href="http://www.public-software-group.org/moonbridge">Moonbridge Network Server for Lua Applications</a> version 1.0.1 or higher, <a href="http://www.postgresql.org/">PostgreSQL</a> version 8.2 or higher, and a C compiler.
95 </p>
96 <h2>Installation</h2>
97 <p>
98 After downloading the tar.gz package, unpack it, enter the unpacked directory and type <tt>make</tt>. If you use Mac OS X or if you experience problems during compilation, you need to edit the <tt>Makefile.options</tt> file prior to compilation. The framework itself will be available in the <tt>framework/</tt> directory, while a demo application is available in the <tt>demo-app/</tt> directory. The <tt>framework.precompiled/</tt> and <tt>demo-app.precompiled/</tt> directories will contain a version with all Lua files being byte-code pre-compiled, which can be used instead. You may copy these directories (with <tt>cp -L</tt> to follow links) to any other place you like. Don't forget to setup a database, and make the <tt>tmp/</tt> directory of the application writable for the web server process. Good luck and have fun!
99 </p>
100 <h2>Configuration, pre-fork and post-fork initializers</h2>
101 <p>
102 The Moonbridge Network Server creates forks (i.e. clones) of the application server process in order to handle concurrent requests. Certain initializations may be performed before forking, other initializations must be performed after forking. The application's configuration files as well as its pre-fork initializers are executed before forking. The application's post-fork initializers are executed after forking. In particular, any libraries that open file or network handles during initialization must not be loaded before the server process is forked. Opening database connections must be performed after forking as well. Execution order is as follows:
103 </p>
104 <ol>
105 <li>
106 Loading all WebMCP libraries except the "multirand" library (multirand opens /dev/urandom and thus must not be loaded prior to forking)
107 </li>
108 <li>
109 Executing the selected configuration file: <tt>config/</tt><i>configuration_name</i><tt>.lua</tt>
110 </li>
111 <li>
112 Executing all pre-fork initializers (both those in the <tt>app/_prefork/</tt> and those in the <tt>app/</tt><i>application_name</i><tt>/_prefork/</tt> directory) until call of <tt>execute.inner()</tt> within each initializer
113 </li>
114 <li>
115 The Moonbridge Network Server forks the process (i.e. cloning the whole Lua machine)<br />
116 <span style="color: red">Note: no file handles or network connections must be opened prior to this point!</span>
117 </li>
118 <li>
119 Loading WebMCP's "multirand" library
120 </li>
121 <li>
122 Executing all post-fork initializers (both those in the <tt>app/_postfork/</tt> and those in the <tt>app/</tt><i>application_name</i><tt>/_postfork/</tt> directory) until call of <tt>execute.inner()</tt> within each initializer
123 </li>
124 <li>
125 For each request:
126 <ul>
127 <li>
128 Execution of all applicable filters until call of <tt>execute.inner()</tt> within each filter
129 </li>
130 <li>
131 Handling of the request by calling the appropriate view or action
132 </li>
133 <li>
134 Resuming execution of all filters in reverse order from that position where <tt>execute.inner()</tt> had been called
135 </li>
136 </ul>
137 </li>
138 <li>
139 Resuming execution of all post-fork initializers in reverse order from that position where <tt>execute.inner()</tt> had been called
140 </li>
141 <li>
142 Resuming execution of all pre-fork initializers in reverse order from that position where <tt>execute.inner()</tt> had been called
143 </li>
144 </ol>
145 </p>
146 <h2>Using the atom library</h2>
147 <p>
148 Lua itself has only very few built-in data types. The atom library gives support for extra data types. Currently the following extra data types are provided:
149 </p>
150 <ul>
151 <li>atom.fraction</li>
152 <li>atom.date</li>
153 <li>atom.time</li>
154 <li>atom.timestamp (date and time combined in one data type)</li>
155 </ul>
156 <p>
157 In addition the following pseudo-types are existent, corresponding to Lua's base types:
158 </p>
159 <ul>
160 <li>atom.boolean</li>
161 <li>atom.string</li>
162 <li>atom.integer</li>
163 <li>atom.number</li>
164 </ul>
165 <p>
166 Both atom.integer and atom.number refer to Lua's base type &ldquo;number&rdquo;.
167 </p>
168 <p>
169 New values of atom data types are created by either calling <tt>atom.<i>type</i>:load(string_representation)</tt> or by calling <tt>atom.<i>type</i>{...}</tt>, e.g. <tt>atom.date{year=1970, month=1, day=1}</tt>. You can dump any atom value as a string by calling <tt>atom.dump(value)</tt> and later reload it with <tt>atom.<i>type</i>:load(string)</tt>.
170 </p>
171 <h2>Using the Object-Relational Mapper &ldquo;mondelefant&rdquo;</h2>
172 <p>
173 The library &ldquo;mondelefant&rdquo; shipping with WebMCP can be used to access PostgreSQL databases. It also serves as an Object-Relational Mapper (ORM). The database connection is usually configured in the config file (e.g. in <tt>config/devel.lua</tt>):
174 </p>
175 <pre>
176 config.db = { engine="postgresql", dbname="webmcp_demo" }
177 config.db_trace = true</pre>
178 <p>
179 In addition to configuring the database, it must be opened within a post-fork initializer (e.g. in <tt>app/_postfork/01_database.lua</tt>):
180 </p>
181 <pre>
182 _G.db = assert(mondelefant.connect(config.db))
183 function mondelefant.class_prototype:get_db_conn() return db end</pre>
184 <p>
185 The parameters for <tt>mondelefant.connect</tt> are directly passed to PostgreSQL's client library libpq. See <a href="http://www.postgresql.org/docs/9.4/static/libpq-connect.html">PostgreSQL's documentation on PQconnect</a> for information about supported parameters.
186 </p>
187 <p>
188 To define a model to be used within a WebMCP application, create a file named with the name of the model and <tt>.lua</tt> as extension in the <tt>model/</tt> directory of your application. The most basic definition of a model (named &ldquo;movie&rdquo; in this example) is:
189 </p>
190 <pre>
191 Movie = mondelefant.new_class()
192 Movie.table = 'movie'</pre>
193 <p>
194 Note: Model classes are always written CamelCase, while the name of the file in <tt>model/</tt> is written lower_case.
195 </p>
196 <p>
197 To select objects from the database, the mondelefant library provides a selector framework:
198 </p>
199 <pre>
200 local s = Movie:new_selector()
201 s:add_where{ 'id = ?', param.get_id() }
202 s:single_object_mode() -- return single object instead of list
203 local movie = s:exec()</pre>
204 <p>
205 A short form of the above query would be:
206 </p>
207 <pre>
208 local movie = Movie:new_selector():add_where{ 'id = ?', param.get_id() }:single_object_mode():exec()</pre>
209 <p>
210 For more examples about how to use the model system, please take a look at the demo application.
211 </p>
212 <h2>The Model-View-Action (MVA) concept</h2>
213 <p>
214 As opposed to other web application frameworks, WebMCP does not use a Model-View-Controller (MVC) concept, but a Model-View-Action (MVA) concept.
215 </p>
216 <h3>Models</h3>
217 <p>
218 The models in MVA are like the models in MVC; they are used to access data stored in a relational database (PostgreSQL) in an object oriented way. They can also be used to provide methods for working with objects representing the database entries.
219 </p>
220 <h3>Views</h3>
221 <p>
222 The views in the MVA concept are different from the views in the MVC concept. As WebMCP has no controllers, the views are responsible for processing the GET/POST parameters from the webbrowser, fetching the data to be displayed, and creating the output by directly writing HTML to slots in a layout or by calling helper functions for the user interface.
223 </p>
224 <h3>Actions</h3>
225 <p>
226 Actions are similar to views, but supposed to change data in the database, hence only callable by HTTP POST requests. They are also responsible for processing the POST parameters from the webbrowser. They can modify the database, but instead of rendering a page to be displayed, they just return a status code. Depending on the status code there will be an internal forward or an HTTP 303 redirect to a view. When calling an action via a POST request, additional POST parameters, which are usually added by hidden form fields, determine the view to be displayed for each status code returned by the action.
227 </p>
228 <h2>Directory structure of a WebMCP application</h2>
229 <ul>
230 <li>
231 Base Directory
232 <ul>
233 <li>
234 <tt>app/</tt>
235 <ul>
236 <li>
237 <tt>_prefork/</tt>
238 <ul>
239 <li>
240 <tt>10_first_prefork_initializer.lua</tt>
241 </li>
242 <li>
243 <tt>30_third_prefork_initializer.lua</tt>
244 </li>
245 </ul>
246 </li>
247 <li>
248 <tt>_postfork/</tt>
249 <ul>
250 <li>
251 <tt>01_first_postfork_initializer.lua</tt>
252 </li>
253 <li>
254 <tt>03_third_postfork_initializer.lua</tt>
255 </li>
256 </ul>
257 </li>
258 <li>
259 <tt>main/</tt>
260 <ul>
261 <li>
262 <tt>_prefork/</tt>
263 <ul>
264 <li>
265 <tt>20_second_prefork_initializer.lua</tt>
266 </li>
267 </ul>
268 </li>
269 <li>
270 <tt>_postfork/</tt>
271 <ul>
272 <li>
273 <tt>02_second_postfork_initializer.lua</tt>
274 </li>
275 </ul>
276 </li>
277 <li>
278 <tt>_filter/</tt>
279 <ul>
280 <li>
281 <tt>10_first_filter.lua</tt>
282 </li>
283 <li>
284 <tt>30_third_filter.lua</tt>
285 </li>
286 <li>&hellip;</li>
287 </ul>
288 </li>
289 <li>
290 <tt>_filter_action/</tt>
291 <ul>
292 <li>
293 <tt>20_second_filter.lua</tt>
294 </li>
295 <li>&hellip;</li>
296 </ul>
297 </li>
298 <li>
299 <tt>_filter_view/</tt>
300 <ul>
301 <li>&hellip;</li>
302 </ul>
303 </li>
304 <li>
305 <tt>_layout/</tt>
306 <ul>
307 <li>&hellip;</li>
308 </ul>
309 </li>
310 <li>
311 <tt>index/</tt>
312 <ul>
313 <li>
314 <tt>_action/</tt>
315 <ul>
316 <li>
317 <i>action_name</i><tt>.lua</tt>
318 </li>
319 <li>
320 <i>another_action_name</i><tt>.lua</tt>
321 </li>
322 <li>&hellip;</li>
323 </ul>
324 </li>
325 <li>
326 <tt>index.lua</tt>
327 </li>
328 <li>
329 <i>other_view_name</i><tt>.lua</tt>
330 </li>
331 <li>&hellip;</li>
332 </ul>
333 </li>
334 <li>
335 <i>other_module_name</i><tt>/</tt>
336 <ul>
337 <li>&hellip;</li>
338 </ul>
339 </li>
340 </ul>
341 </li>
342 <li>
343 <i>other_application_name</i><tt>/</tt>
344 <ul>
345 <li>&hellip;</li>
346 </ul>
347 </li>
348 </ul>
349 </li>
350 <li>
351 <tt>config/</tt>
352 <ul>
353 <li>
354 <tt>development.lua</tt>
355 </li>
356 <li>
357 <tt>production.lua</tt>
358 <li>
359 <li>
360 <i>other_config_name</i><tt>.lua</tt>
361 </li>
362 <li>&hellip;</li>
363 </ul>
364 </li>
365 <li>
366 <tt>db/</tt>
367 <ul>
368 <li>
369 <tt>schema.sql</tt>
370 </li>
371 </ul>
372 </li>
373 <li>
374 <tt>locale/</tt>
375 <ul>
376 <li>
377 <tt>translations.de.lua</tt>
378 </li>
379 <li>
380 <tt>translations.en.lua</tt>
381 </li>
382 <li>
383 <tt>translations.</tt><i>languagecode</i><tt>.lua</tt>
384 </li>
385 <li>&hellip;</li>
386 </ul>
387 </li>
388 <li>
389 <tt>model/</tt>
390 <ul>
391 <li>
392 <i>model_name</i><tt>.lua</tt>
393 </li>
394 <li>
395 <i>another_model_name</i><tt>.lua</tt>
396 </li>
397 <li>&hellip;</li>
398 </ul>
399 </li>
400 <li>
401 <tt>static/</tt>
402 <ul>
403 <li>&hellip; (images, javascript, ...)</li>
404 </ul>
405 </li>
406 <li>
407 <tt>tmp/</tt> (writable by the web process)
408 </li>
409 </ul>
410 </li>
411 </ul>
412 <h2>Starting your application</h2>
413 <p>
414 Ensure that the <tt>moonbridge</tt> binary is within your system's search path and that the <tt>moonbridge_http.lua</tt> file is included in the LUA_PATH or linked into the framework's <tt>lib/</tt> directory (alternatively the MOONBR_LUA_PATH option might be set accordingly at compile-time of the Moonbridge Network Server). To start an application, call the <tt>mcp.lua</tt> executable (found in <tt>framework/bin/mcp.lua</tt>) with the following arguments:
415 </p>
416 <ol>
417 <li>
418 Path of the WebMCP framework directory, e.g. <tt>./framework</tt>
419 </li>
420 <li>
421 Path of your application's directory, e.g. <tt>./demo-app</tt>
422 </li>
423 <li>
424 Name of your applicaiton (usually <tt>main</tt>)
425 </li>
426 <li>
427 Name of configuration (e.g. <tt>devel</tt> to use config/devel.lua)
428 </li>
429 </ol>
430 <h2>Automatically generated reference for the WebMCP environment</h2>
431 <ul>

Impressum / About Us