webmcp

view doc/autodoc-header.htmlpart @ 509:6c819040ef6f

Further improvements to WebMCP documentation
author jbe
date Mon Aug 21 01:33:17 2017 +0200 (2017-08-21)
parents ac5f7a40b8c4
children 696d7e5f2bcb
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-bottom: 1ex;
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.1.0 Documentation</title>
86 </head>
87 <body>
88 <h1>WebMCP 2.1.0 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 user who executes WebMCP. Good luck and have fun!
99 </p>
100 <h2>Configuration, initializers, and request handling</h2>
101 <p>
102 WebMCP uses the <a href="http://www.public-software-group.org/moonbridge">Moonbridge Network Server</a> to handle HTTP requests. The Moonbridge Network Server listens to a TCP port and passes control to WebMCP by calling <a href="#request.handler"><tt>request.handler(...)</tt></a> for each request. However, before each request is processed, WebMCP will initialize the environment. This initialization includes tasks such as
103 </p>
104 <ul>
105 <li>loading libraries,</li>
106 <li>configuring the database connection or the application,</li>
107 <li>connecting to the database,</li>
108 <li>etc.</li>
109 </ul>
110 <p>
111 For each request, it is also possible to execute filters. Filters can be used to
112 </p>
113 <ul>
114 <li>
115 restrict access for certain requests, e.g. by performing redirects or raising errors
116 </li>
117 <li>
118 perform tasks that are common for different requests, e.g.
119 <ul>
120 <li>execution certain database requests,</li>
121 <li>prepare a menu on the website,</li>
122 <li>&hellip;</li>
123 </ul>
124 </li>
125 </ul>
126 <p>
127 Filters and initializers are created by adding files in the application's directory structure. The filename determins the execution order of otherwise equally ranked initializers and/or filters. It is a common idiom to start the filename of a filter or initializer with a two digit number to be easily able to change the execution order when desired. Filters and initializers are executed both before and after a request. Each file must contain a command <a href="#execute.inner"><tt>execute.inner()</tt></a>. The part before that command is executed before the request, and the part after that command is executed after the request.
128 </p>
129 <p>
130 The Moonbridge server creates forks (i.e. clones) of the application server process (i.e. the whole Lua engine including all libraries and variables) in order to handle concurrent requests. Certain initializations may be performed before forking, other initializations must be performed after forking. For this purpose, WebMCP allows an application to provide so-called "pre-fork" and "post-fork" initializers. 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. WebMCP follows the following execution order (directory structure is explained further down):
131 </p>
132 <ol>
133 <li>
134 Loading all WebMCP libraries except the "multirand" library (multirand opens /dev/urandom and thus must not be loaded prior to forking)
135 </li>
136 <li>
137 Executing the selected configuration file: <tt>config/</tt><i>configuration_name</i><tt>.lua</tt>
138 </li>
139 <li>
140 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
141 </li>
142 <li>
143 The Moonbridge Network Server forks the process (i.e. cloning the whole Lua machine)<br />
144 <span style="color: red">Note: no file handles or network connections must be opened prior to this point!</span>
145 </li>
146 <li>
147 Loading WebMCP's "multirand" library
148 </li>
149 <li>
150 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
151 </li>
152 <li>
153 For each request:
154 <ul>
155 <li>
156 Execution of all applicable filters until call of <tt>execute.inner()</tt> within each filter
157 </li>
158 <li>
159 Handling of the request by calling the appropriate view or action
160 </li>
161 <li>
162 Resuming execution of all filters in reverse order from that position where <tt>execute.inner()</tt> had been called
163 </li>
164 </ul>
165 </li>
166 <li>
167 Resuming execution of all post-fork initializers in reverse order from that position where <tt>execute.inner()</tt> had been called
168 </li>
169 <li>
170 Resuming execution of all pre-fork initializers in reverse order from that position where <tt>execute.inner()</tt> had been called
171 </li>
172 </ol>
173 </p>
174 <p>
175 As a minimum configuration, the used configuration file or pre-fork initializer should at least contain a <a href="#listen"><tt>listen{...}</tt></a> call, e.g.:
176 </p>
177 <pre>
178 listen{
179 { proto = "tcp", host = "::", port = 8080 },
180 { proto = "tcp", host = "0.0.0.0", port = 8080 }
181 }
182 execute.inner() -- only use this line if done in pre-fork initializer</pre>
183 <h2>Using the atom library</h2>
184 <p>
185 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:
186 </p>
187 <ul>
188 <li><a href="#atom.fraction:new">atom.fraction</a></li>
189 <li><a href="#atom.date:new">atom.date</a></li>
190 <li><a href="#atom.time:new">atom.time</a></li>
191 <li><a href="#atom.timestamp:new">atom.timestamp (date and time combined in one data type)</a></li>
192 </ul>
193 <p>
194 In addition the following pseudo-types are existent, corresponding to Lua's base types:
195 </p>
196 <ul>
197 <li>atom.boolean</li>
198 <li>atom.string</li>
199 <li>atom.integer</li>
200 <li>atom.number</li>
201 </ul>
202 <p>
203 Both atom.integer and atom.number refer to Lua's base type &ldquo;number&rdquo;.
204 </p>
205 <p>
206 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 <a href="#atom.dump"><tt>atom.dump(value)</tt></a> and later reload it with <tt>atom.<i>type</i>:load(string)</tt>.
207 </p>
208 <h2>Using the Object-Relational Mapper &ldquo;mondelefant&rdquo;</h2>
209 <p>
210 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>):
211 </p>
212 <pre>
213 config.db = { engine="postgresql", dbname="webmcp_demo" }</pre>
214 <p>
215 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>):
216 </p>
217 <pre>
218 _G.db = assert(<a href="#mondelefant.connect">mondelefant.connect</a>(config.db))
219 function mondelefant.class_prototype:<a href="#db_class:get_db_conn">get_db_conn</a>() return db end
220 execute.inner()</pre>
221 <p>
222 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.
223 </p>
224 <p>
225 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:
226 </p>
227 <pre>
228 Movie = <a href="#mondelefant.new_class">mondelefant.new_class</a>()
229 Movie.table = 'movie'</pre>
230 <p>
231 Note: Model classes are always written CamelCase, while the name of the file in <tt>model/</tt> is written lower_case.
232 </p>
233 <p>
234 To select objects from the database, the mondelefant library provides a selector framework:
235 </p>
236 <pre>
237 local s = Movie:<a href="#db_class:new_selector">new_selector</a>()
238 s:<a href="#db_selector:add_where">add_where</a>{ 'id = ?', param.get_id() }
239 s:<a href="#db_selector:single_object_mode">single_object_mode</a>() -- return single object instead of list
240 local movie = s:<a href="#db_selector:exec">exec</a>()</pre>
241 <p>
242 A short form of the above query would be:
243 </p>
244 <pre>
245 local movie = Movie:new_selector():add_where{ 'id = ?', param.get_id() }:single_object_mode():exec()</pre>
246 <p>
247 For more examples about how to use the model system, please take a look at the demo application.
248 </p>
249 <h2>The Model-View-Action (MVA) concept</h2>
250 <p>
251 As opposed to other web application frameworks, WebMCP does not use a Model-View-Controller (MVC) concept, but a Model-View-Action (MVA) concept.
252 </p>
253 <h3>Models</h3>
254 <p>
255 The models in MVA are like the models in MVC; they are used to access data, which is stored in a relational database (PostgreSQL), in an object oriented way. Methods provided by the corresponding classes be used to alter stored objects or execute any other associated program code. Models are usually defined in a file with a lowercase filename ending with "<tt>.lua</tt>" in the <tt>models/</tt> directory of the application. The corresponding model name (i.e. class name) must be written in CamelCase, e.g. "<tt>models/my_model.lua</tt>" should define a model class named "<tt>MyModel</tt>". The simplest model is created by calling <a href="#mondelefant.new_class"><tt>mondelefant.new_class()</tt></a> and subsequently setting the <a href="#db_class.table"><tt>table</tt></a> attribute of the returned class.
256 </p>
257 <pre>
258 -- filename: model/customer_receipt.lua
259 CustomerReceipt = mondelefant.new_class()
260 CustomerReceipt.table = "custreceipt"</pre>
261 <p>
262 Methods such as <a href="#db_class:add_reference"><tt>:add_reference(...)</tt></a> can be used to further modify or extend the class.
263 </p>
264 <h3>Views</h3>
265 <p>
266 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 of a layout (see <a href="#slot.select"><tt>slot.select(...)</tt></a>, <a href="#slot.put"><tt>slot.put(...)</tt></a>, and <a href="#slot.put_into"><tt>slot.put_into(...)</tt></a> or by calling helper functions for the user interface (those functions beginning with "<tt>ui.</tt>"). Views are stored in files with the file path "<tt>app/</tt><i>application_name</i><tt>/</tt><i>module_name</i><tt>/</tt><i>view_name</i><tt>.lua</tt>". When their corresponding URL, e.g. "<tt>http://</tt><i>hostname</i><tt>:</tt><i>port</i><tt>/</tt><i>module_name</i><tt>/</tt><i>view_name</i><tt>.html</tt>", is requested, the code in that file gets executed (after calling appropriate filters). After the execution of the view has finished (and after all filters have finished their execution too), the slot data will be inserted into placeholder sections in the selected layout file. The layout file defaults to <tt>app/</tt><i>application_name</i><tt>/_layout/default.html</tt>" but may be changed using the <a href="#slot.set_layout"><tt>slot.set_layout(...)</tt></a> function.
267 </p>
268 <h3>Actions</h3>
269 <p>
270 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 string (via Lua's <tt>return</tt> statement, where <tt>true</tt> can also be used instead of "<tt>ok</tt>", and <tt>false</tt> instead of "<tt>error</tt>"). Depending on the status string 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 string returned by the action. See the <tt>routing</tt> parameter to the <a href="#ui.form"><tt>ui.form{...}</tt></a> function for further details.
271 </p>
272 <h2>Directory structure of a WebMCP application</h2>
273 <ul>
274 <li>
275 Base Directory
276 <ul>
277 <li>
278 <tt>app/</tt>
279 <ul>
280 <li>
281 <tt>_prefork/</tt>
282 <ul>
283 <li>
284 <tt>10_first_prefork_initializer.lua</tt>
285 </li>
286 <li>
287 <tt>30_third_prefork_initializer.lua</tt>
288 </li>
289 </ul>
290 </li>
291 <li>
292 <tt>_postfork/</tt>
293 <ul>
294 <li>
295 <tt>01_first_postfork_initializer.lua</tt>
296 </li>
297 <li>
298 <tt>03_third_postfork_initializer.lua</tt>
299 </li>
300 </ul>
301 </li>
302 <li>
303 <tt>main/</tt>
304 <ul>
305 <li>
306 <tt>_prefork/</tt>
307 <ul>
308 <li>
309 <tt>20_second_prefork_initializer.lua</tt>
310 </li>
311 </ul>
312 </li>
313 <li>
314 <tt>_postfork/</tt>
315 <ul>
316 <li>
317 <tt>02_second_postfork_initializer.lua</tt>
318 </li>
319 </ul>
320 </li>
321 <li>
322 <tt>_filter/</tt>
323 <ul>
324 <li>
325 <tt>10_first_filter.lua</tt>
326 </li>
327 <li>
328 <tt>30_third_filter.lua</tt>
329 </li>
330 <li>&hellip;</li>
331 </ul>
332 </li>
333 <li>
334 <tt>_filter_action/</tt>
335 <ul>
336 <li>
337 <tt>20_second_filter.lua</tt>
338 </li>
339 <li>&hellip;</li>
340 </ul>
341 </li>
342 <li>
343 <tt>_filter_view/</tt>
344 <ul>
345 <li>&hellip;</li>
346 </ul>
347 </li>
348 <li>
349 <tt>_layout/</tt>
350 <ul>
351 <li>&hellip;</li>
352 </ul>
353 </li>
354 <li>
355 <tt>index/</tt>
356 <ul>
357 <li>
358 <tt>_action/</tt>
359 <ul>
360 <li>
361 <i>action_name</i><tt>.lua</tt>
362 </li>
363 <li>
364 <i>another_action_name</i><tt>.lua</tt>
365 </li>
366 <li>&hellip;</li>
367 </ul>
368 </li>
369 <li>
370 <tt>index.lua</tt>
371 </li>
372 <li>
373 <i>other_view_name</i><tt>.lua</tt>
374 </li>
375 <li>&hellip;</li>
376 </ul>
377 </li>
378 <li>
379 <i>other_module_name</i><tt>/</tt>
380 <ul>
381 <li>&hellip;</li>
382 </ul>
383 </li>
384 </ul>
385 </li>
386 <li>
387 <i>other_application_name</i><tt>/</tt>
388 <ul>
389 <li>&hellip;</li>
390 </ul>
391 </li>
392 </ul>
393 </li>
394 <li>
395 <tt>config/</tt>
396 <ul>
397 <li>
398 <tt>development.lua</tt>
399 </li>
400 <li>
401 <tt>production.lua</tt>
402 <li>
403 <li>
404 <i>other_config_name</i><tt>.lua</tt>
405 </li>
406 <li>&hellip;</li>
407 </ul>
408 </li>
409 <li>
410 <tt>db/</tt>
411 <ul>
412 <li>
413 <tt>schema.sql</tt>
414 </li>
415 </ul>
416 </li>
417 <li>
418 <tt>locale/</tt>
419 <ul>
420 <li>
421 <tt>translations.de.lua</tt>
422 </li>
423 <li>
424 <tt>translations.en.lua</tt>
425 </li>
426 <li>
427 <tt>translations.</tt><i>languagecode</i><tt>.lua</tt>
428 </li>
429 <li>&hellip;</li>
430 </ul>
431 </li>
432 <li>
433 <tt>model/</tt>
434 <ul>
435 <li>
436 <i>model_name</i><tt>.lua</tt>
437 </li>
438 <li>
439 <i>another_model_name</i><tt>.lua</tt>
440 </li>
441 <li>&hellip;</li>
442 </ul>
443 </li>
444 <li>
445 <tt>static/</tt>
446 <ul>
447 <li>&hellip; (images, javascript, ...)</li>
448 </ul>
449 </li>
450 <li>
451 <tt>tmp/</tt> (writable by the web process)
452 </li>
453 </ul>
454 </li>
455 </ul>
456 <h2>Starting your application</h2>
457 <p>
458 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:
459 </p>
460 <ol>
461 <li>
462 Path of the WebMCP framework directory, e.g. <tt>./framework</tt>
463 </li>
464 <li>
465 Path of your application's directory, e.g. <tt>./demo-app</tt>
466 </li>
467 <li>
468 Name of your applicaiton (usually <tt>main</tt>)
469 </li>
470 <li>
471 Name of configuration (e.g. <tt>devel</tt> to use config/devel.lua)
472 </li>
473 </ol>
474 <h2>Automatically generated reference for the WebMCP environment</h2>
475 <ul>

Impressum / About Us