webmcp

view doc/autodoc-header.htmlpart @ 466:2751b6b81c23

Minor efficiency enhancement in <db_object>:try_save() method
author jbe
date Mon Nov 07 19:32:51 2016 +0100 (2016-11-07)
parents 0221836a9db5
children ac5f7a40b8c4
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.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 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 <p>
147 As a minimum configuration, the used configuration file or pre-fork initializer should at least contain a <tt>listen{...}</tt> call, e.g.:
148 </p>
149 <pre>
150 listen{
151 { proto = "tcp", host = "::", port = 8080 },
152 { proto = "tcp", host = "0.0.0.0", port = 8080 }
153 }
154 execute.inner() -- if done in pre-fork initializer</pre>
155 <h2>Using the atom library</h2>
156 <p>
157 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:
158 </p>
159 <ul>
160 <li>atom.fraction</li>
161 <li>atom.date</li>
162 <li>atom.time</li>
163 <li>atom.timestamp (date and time combined in one data type)</li>
164 </ul>
165 <p>
166 In addition the following pseudo-types are existent, corresponding to Lua's base types:
167 </p>
168 <ul>
169 <li>atom.boolean</li>
170 <li>atom.string</li>
171 <li>atom.integer</li>
172 <li>atom.number</li>
173 </ul>
174 <p>
175 Both atom.integer and atom.number refer to Lua's base type &ldquo;number&rdquo;.
176 </p>
177 <p>
178 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>.
179 </p>
180 <h2>Using the Object-Relational Mapper &ldquo;mondelefant&rdquo;</h2>
181 <p>
182 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>):
183 </p>
184 <pre>
185 config.db = { engine="postgresql", dbname="webmcp_demo" }</pre>
186 <p>
187 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>):
188 </p>
189 <pre>
190 _G.db = assert(mondelefant.connect(config.db))
191 function mondelefant.class_prototype:get_db_conn() return db end
192 execute.inner()</pre>
193 <p>
194 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.
195 </p>
196 <p>
197 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:
198 </p>
199 <pre>
200 Movie = mondelefant.new_class()
201 Movie.table = 'movie'</pre>
202 <p>
203 Note: Model classes are always written CamelCase, while the name of the file in <tt>model/</tt> is written lower_case.
204 </p>
205 <p>
206 To select objects from the database, the mondelefant library provides a selector framework:
207 </p>
208 <pre>
209 local s = Movie:new_selector()
210 s:add_where{ 'id = ?', param.get_id() }
211 s:single_object_mode() -- return single object instead of list
212 local movie = s:exec()</pre>
213 <p>
214 A short form of the above query would be:
215 </p>
216 <pre>
217 local movie = Movie:new_selector():add_where{ 'id = ?', param.get_id() }:single_object_mode():exec()</pre>
218 <p>
219 For more examples about how to use the model system, please take a look at the demo application.
220 </p>
221 <h2>The Model-View-Action (MVA) concept</h2>
222 <p>
223 As opposed to other web application frameworks, WebMCP does not use a Model-View-Controller (MVC) concept, but a Model-View-Action (MVA) concept.
224 </p>
225 <h3>Models</h3>
226 <p>
227 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.
228 </p>
229 <h3>Views</h3>
230 <p>
231 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.
232 </p>
233 <h3>Actions</h3>
234 <p>
235 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.
236 </p>
237 <h2>Directory structure of a WebMCP application</h2>
238 <ul>
239 <li>
240 Base Directory
241 <ul>
242 <li>
243 <tt>app/</tt>
244 <ul>
245 <li>
246 <tt>_prefork/</tt>
247 <ul>
248 <li>
249 <tt>10_first_prefork_initializer.lua</tt>
250 </li>
251 <li>
252 <tt>30_third_prefork_initializer.lua</tt>
253 </li>
254 </ul>
255 </li>
256 <li>
257 <tt>_postfork/</tt>
258 <ul>
259 <li>
260 <tt>01_first_postfork_initializer.lua</tt>
261 </li>
262 <li>
263 <tt>03_third_postfork_initializer.lua</tt>
264 </li>
265 </ul>
266 </li>
267 <li>
268 <tt>main/</tt>
269 <ul>
270 <li>
271 <tt>_prefork/</tt>
272 <ul>
273 <li>
274 <tt>20_second_prefork_initializer.lua</tt>
275 </li>
276 </ul>
277 </li>
278 <li>
279 <tt>_postfork/</tt>
280 <ul>
281 <li>
282 <tt>02_second_postfork_initializer.lua</tt>
283 </li>
284 </ul>
285 </li>
286 <li>
287 <tt>_filter/</tt>
288 <ul>
289 <li>
290 <tt>10_first_filter.lua</tt>
291 </li>
292 <li>
293 <tt>30_third_filter.lua</tt>
294 </li>
295 <li>&hellip;</li>
296 </ul>
297 </li>
298 <li>
299 <tt>_filter_action/</tt>
300 <ul>
301 <li>
302 <tt>20_second_filter.lua</tt>
303 </li>
304 <li>&hellip;</li>
305 </ul>
306 </li>
307 <li>
308 <tt>_filter_view/</tt>
309 <ul>
310 <li>&hellip;</li>
311 </ul>
312 </li>
313 <li>
314 <tt>_layout/</tt>
315 <ul>
316 <li>&hellip;</li>
317 </ul>
318 </li>
319 <li>
320 <tt>index/</tt>
321 <ul>
322 <li>
323 <tt>_action/</tt>
324 <ul>
325 <li>
326 <i>action_name</i><tt>.lua</tt>
327 </li>
328 <li>
329 <i>another_action_name</i><tt>.lua</tt>
330 </li>
331 <li>&hellip;</li>
332 </ul>
333 </li>
334 <li>
335 <tt>index.lua</tt>
336 </li>
337 <li>
338 <i>other_view_name</i><tt>.lua</tt>
339 </li>
340 <li>&hellip;</li>
341 </ul>
342 </li>
343 <li>
344 <i>other_module_name</i><tt>/</tt>
345 <ul>
346 <li>&hellip;</li>
347 </ul>
348 </li>
349 </ul>
350 </li>
351 <li>
352 <i>other_application_name</i><tt>/</tt>
353 <ul>
354 <li>&hellip;</li>
355 </ul>
356 </li>
357 </ul>
358 </li>
359 <li>
360 <tt>config/</tt>
361 <ul>
362 <li>
363 <tt>development.lua</tt>
364 </li>
365 <li>
366 <tt>production.lua</tt>
367 <li>
368 <li>
369 <i>other_config_name</i><tt>.lua</tt>
370 </li>
371 <li>&hellip;</li>
372 </ul>
373 </li>
374 <li>
375 <tt>db/</tt>
376 <ul>
377 <li>
378 <tt>schema.sql</tt>
379 </li>
380 </ul>
381 </li>
382 <li>
383 <tt>locale/</tt>
384 <ul>
385 <li>
386 <tt>translations.de.lua</tt>
387 </li>
388 <li>
389 <tt>translations.en.lua</tt>
390 </li>
391 <li>
392 <tt>translations.</tt><i>languagecode</i><tt>.lua</tt>
393 </li>
394 <li>&hellip;</li>
395 </ul>
396 </li>
397 <li>
398 <tt>model/</tt>
399 <ul>
400 <li>
401 <i>model_name</i><tt>.lua</tt>
402 </li>
403 <li>
404 <i>another_model_name</i><tt>.lua</tt>
405 </li>
406 <li>&hellip;</li>
407 </ul>
408 </li>
409 <li>
410 <tt>static/</tt>
411 <ul>
412 <li>&hellip; (images, javascript, ...)</li>
413 </ul>
414 </li>
415 <li>
416 <tt>tmp/</tt> (writable by the web process)
417 </li>
418 </ul>
419 </li>
420 </ul>
421 <h2>Starting your application</h2>
422 <p>
423 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:
424 </p>
425 <ol>
426 <li>
427 Path of the WebMCP framework directory, e.g. <tt>./framework</tt>
428 </li>
429 <li>
430 Path of your application's directory, e.g. <tt>./demo-app</tt>
431 </li>
432 <li>
433 Name of your applicaiton (usually <tt>main</tt>)
434 </li>
435 <li>
436 Name of configuration (e.g. <tt>devel</tt> to use config/devel.lua)
437 </li>
438 </ol>
439 <h2>Automatically generated reference for the WebMCP environment</h2>
440 <ul>

Impressum / About Us