view doc/autodoc-header.htmlpart @ 511:bb1f3e7c65d1

Added section on Internationalization/Localization to WebMCP's documentation
author jbe
date Mon Aug 21 04:28:17 2017 +0200 (2017-08-21)
parents 696d7e5f2bcb
children 74463f7af123
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 = "", 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>Layouts</h2>
273 <p>
274 Templates for HTML documents to be returned by views are stored at the path "<tt>app/</tt><i>application_name</i><tt>/_layout/</tt><i>layout_name</i><tt>.html</tt>", relative to the application base path. The default layout name is "<tt>default</tt>". For system errors, the layout name "<tt>system_error</tt>" is used. A sample layout is given as follows:
275 </p>
276 <pre>
277 &lt;!DOCTYPE HTML&gt;
278 &lt;html&gt;
279 &lt;head&gt;
280 &lt;title&gt;&lt!-- WEBMCP SLOTNODIV title --&gt;&lt;/title&gt;
281 &lt;link rel="stylesheet" type="text/css" media="screen" href="__BASEURL__/static/style.css"/&gt;
282 &lt;/head&gt;
283 &lt;body&gt;
284 &lt;!-- WEBMCP SLOT content --&gt;
285 &lt;/body&gt;
286 &lt;/html&gt;</pre>
287 <p>
288 The following elements in a layout file get replaced automatically:
289 </p>
290 <ul>
291 <li><tt>__BASEURL__</tt> gets replaced with the application's base web address.</li>
292 <li><tt>&lt;!-- WEBMCP SLOT </tt><i>slot_name</i><tt> --&gt;</tt> gets, if the slot is not empty, replaced with a <tt>&lt;div&gt;</tt> element (with the <tt>id</tt> attribute set to "<tt>slot_</tt><i>slotname</i>") that contains the slot's content.</li>
293 <li><tt>&lt;!-- WEBMCP SLOTNODIV </tt><i>slot_name</i><tt> --&gt;</tt> gets replaced with the slot's content (without wrapping it in an additional <tt>&lt;div&gt;</tt> element).</li>
294 </ul>
295 <h2>Internationalization/Localization</h2>
296 <p>
297 To translate certain strings in your application, simply use the <a href="#_">underscore function</a>. A language can be selected with <tt><a href="#locale.set">locale.set</a>{lang = "code"}</tt> where <tt>code</tt> is a language code. The translations for strings are expected to be contained in files "<tt>locale/translations.</tt><i>language_code</i><tt>.lua</tt>". These files should return a table that maps strings to their corresponding translation:
298 </p>
299 <pre>
300 return{
301 ["Are you sure?"] = "Bist Du sicher?";
302 ["User '#{name}' created"] = "Benutzer '#{name}' created";
303 }</pre>
304 <p>
305 Such translation files can be automatically created with the <tt>langtool.lua</tt> program, found in the framework's <tt>bin/</tt> directory.
306 </p>
307 <h2>Directory structure of a WebMCP application</h2>
308 Summarizing information from the previous section, we get the following directory structure for a WebMCP application:
309 <ul>
310 <li>
311 Base Directory
312 <ul>
313 <li>
314 <tt>app/</tt>
315 <ul>
316 <li><tt>_prefork/</tt></li>
317 <li><tt>_postfork/</tt></li>
318 <li>
319 <tt>main/</tt> (typically "main" is the only application)
320 <ul>
321 <li><tt>_prefork/</tt></li>
322 <li><tt>_postfork/</tt></li>
323 <li><tt>_filter/</tt></li>
324 <li><tt>_filter_action/</tt></li>
325 <li><tt>_filter_view/</tt></li>
326 <li><tt>_layout/</tt></li>
327 <li>
328 <tt>index/</tt> (module "index" is the default module)
329 <ul>
330 <li>
331 <tt>_action/</tt>
332 <ul>
333 <li><i>action_name</i><tt>.lua</tt></li>
334 <li><i>another_action_name</i><tt>.lua</tt></li>
335 <li>&hellip;</li>
336 </ul>
337 </li>
338 <li><tt>index.lua</tt> (view "index" of module "index" is the default view)</li>
339 <li><i>other_view_name</i><tt>.lua</tt></li>
340 <li>&hellip;</li>
341 </ul>
342 </li>
343 <li>
344 <i>other_module_name</i><tt>/</tt>
345 <ul><li>&hellip;</li></ul>
346 </li>
347 </ul>
348 </li>
349 <li>
350 <i>other_application_name</i><tt>/</tt>
351 <ul>
352 <li>&hellip;</li>
353 </ul>
354 </li>
355 </ul>
356 </li>
357 <li>
358 <tt>config/</tt>
359 <ul>
360 <li><tt>development.lua</tt> (e.g. to be used for development purposes)</li>
361 <li><tt>production.lua</tt> (e.g. to be used for production)</li>
362 <li><i>other_config_name</i><tt>.lua</tt></li>
363 <li>&hellip;</li>
364 </ul>
365 </li>
366 <li>
367 <tt>db/</tt>
368 <ul>
369 <li><tt>schema.sql</tt> (not used by WebMCP, but may be used to initialize a database)</li>
370 </ul>
371 </li>
372 <li>
373 <tt>locale/</tt> (translation files used by the <a href="#_">underscore function</a>")
374 <ul>
375 <li>
376 <tt>translations.de.lua</tt>
377 </li>
378 <li>
379 <tt>translations.en.lua</tt>
380 </li>
381 <li>
382 <tt>translations.</tt><i>languagecode</i><tt>.lua</tt>
383 </li>
384 <li>&hellip;</li>
385 </ul>
386 </li>
387 <li>
388 <tt>model/</tt>
389 <ul>
390 <li>
391 <i>model_name</i><tt>.lua</tt> (code must create a model class in camel case, e.g. "<tt>ModelName</tt>" if the file name is "<tt>model_name.lua</tt>")
392 </li>
393 <li>
394 <i>another_model_name</i><tt>.lua</tt>
395 </li>
396 <li>&hellip;</li>
397 </ul>
398 </li>
399 <li>
400 <tt>static/</tt>
401 <ul>
402 <li>&hellip; (images, javascript, ...)</li>
403 </ul>
404 </li>
405 <li>
406 <tt>tmp/</tt> (needs to be writable by the user executing WebMCP)
407 </li>
408 </ul>
409 </li>
410 </ul>
411 <h2>Starting your application</h2>
412 <p>
413 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:
414 </p>
415 <ol>
416 <li>
417 Path of the WebMCP framework directory, e.g. <tt>./framework</tt>
418 </li>
419 <li>
420 Path of your application's directory, e.g. <tt>./demo-app</tt>
421 </li>
422 <li>
423 Name of your applicaiton (usually <tt>main</tt>)
424 </li>
425 <li>
426 Name of configuration (e.g. <tt>devel</tt> to use config/devel.lua)
427 </li>
428 </ol>
429 <h2>Automatically generated reference for the WebMCP environment</h2>
430 <ul>

Impressum / About Us