webmcp

view doc/autodoc-header.htmlpart @ 388:29eeba26ff9f

Changed version number to 2.0.4
author jbe
date Wed Dec 02 09:37:47 2015 +0100 (2015-12-02)
parents cd9329b3bd78
children a7a335e2db93
line source
1 <html>
2 <head>
3 <script type="text/javascript">
4 window.onload = function() {
5 if (window.location.hash) {
6 var encoded_hash = window.location.hash.replace(/^#/, '').replace(/_/g, '_USC_').replace(/\./g, '_DOT_').replace(/:/g, '_COL_');
7 var element = document.getElementById('autodoc_details_' + encoded_hash);
8 if (element) element.style.display = '';
9 }
10 }
11 </script>
12 <style>
13 body {
14 font-family: "Liberation Sans", sans-serif;
15 font-size: 11pt;
16 padding-bottom: 5ex;
17 }
18 .warning {
19 color: #ff0000;
20 }
21 h1, h2 {
22 font-family: "Liberation Serif", Georgia, serif;
23 }
24 h2 {
25 margin-bottom: 0.3ex;
26 }
27 p {
28 margin: 0px;
29 line-height: 130%;
30 }
31 tt, pre {
32 font-size: 10pt;
33 }
34 tt {
35 font-weight: bold;
36 white-space: nowrap;
37 }
38 .autodoc_entry {
39 margin-top: 1ex;
40 margin-bottom: 1ex;
41 }
42 .autodoc_comment_tail {
43 font-style: italic;
44 }
45 .autodoc_entry .short_synopsis {
46 cursor: pointer;
47 }
48 .autodoc_details {
49 padding-left: 1em;
50 padding-right: 1em;
51 border: 1px solid #777;
52 }
53 .autodoc_synopsis {
54 font-weight: bold;
55 }
56 .autodoc_synopsis .autodoc_comment_tail {
57 font-weight: normal;
58 color: #008000;
59 }
60 .autodoc_entry .autodoc_comment {
61 color: #400080;
62 }
63 .autodoc_source {
64 color: #505050;
65 }
66 </style>
67 <title>WebMCP 2.0.4 Documentation</title>
68 </head>
69 <body>
70 <h1>WebMCP 2.0.4 Documentation</h1>
71 <p>
72 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>).
73 </p>
74 <h2>Requirements</h2>
75 <p>
76 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.
77 </p>
78 <h2>Installation</h2>
79 <p>
80 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!
81 </p>
82 <h2>Configuration, pre-fork and post-fork initializers</h2>
83 <p>
84 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:
85 </p>
86 <ol>
87 <li>
88 Loading all WebMCP libraries except the "multirand" library (multirand opens /dev/urandom and thus must not be loaded prior to forking)
89 </li>
90 <li>
91 Executing the selected configuration file: <tt>config/</tt><i>configuration_name</i><tt>.lua</tt>
92 </li>
93 <li>
94 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
95 </li>
96 <li>
97 The Moonbridge Network Server forks the process (i.e. cloning the whole Lua machine)<br />
98 <span style="color: red">Note: no file handles or network connections must be opened prior to this point!</span>
99 </li>
100 <li>
101 Loading WebMCP's "multirand" library
102 </li>
103 <li>
104 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
105 </li>
106 <li>
107 For each request:
108 <ul>
109 <li>
110 Execution of all applicable filters until call of <tt>execute.inner()</tt> within each filter
111 </li>
112 <li>
113 Handling of the request by calling the appropriate view or action
114 </li>
115 <li>
116 Resuming execution of all filters in reverse order from that position where <tt>execute.inner()</tt> had been called
117 </li>
118 </ul>
119 </li>
120 <li>
121 Resuming execution of all post-fork initializers in reverse order from that position where <tt>execute.inner()</tt> had been called
122 </li>
123 <li>
124 Resuming execution of all pre-fork initializers in reverse order from that position where <tt>execute.inner()</tt> had been called
125 </li>
126 </ol>
127 </p>
128 <h2>Using the atom library</h2>
129 <p>
130 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:
131 </p>
132 <ul>
133 <li>atom.fraction</li>
134 <li>atom.date</li>
135 <li>atom.time</li>
136 <li>atom.timestamp (date and time combined in one data type)</li>
137 </ul>
138 <p>
139 In addition the following pseudo-types are existent, corresponding to Lua's base types:
140 </p>
141 <ul>
142 <li>atom.boolean</li>
143 <li>atom.string</li>
144 <li>atom.integer</li>
145 <li>atom.number</li>
146 </ul>
147 <p>
148 Both atom.integer and atom.number refer to Lua's base type &ldquo;number&rdquo;.
149 </p>
150 <p>
151 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>.
152 </p>
153 <h2>Using the Object-Relational Mapper &ldquo;mondelefant&rdquo;</h2>
154 <p>
155 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>):
156 </p>
157 <pre>
158 config.db = { engine="postgresql", dbname="webmcp_demo" }
159 config.db_trace = true</pre>
160 <p>
161 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>):
162 </p>
163 <pre>
164 _G.db = assert(mondelefant.connect(config.db))
165 function mondelefant.class_prototype:get_db_conn() return db end</pre>
166 <p>
167 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.
168 </p>
169 <p>
170 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:
171 </p>
172 <pre>
173 Movie = mondelefant.new_class()
174 Movie.table = 'movie'</pre>
175 <p>
176 Note: Model classes are always written CamelCase, while the name of the file in <tt>model/</tt> is written lower_case.
177 </p>
178 <p>
179 To select objects from the database, the mondelefant library provides a selector framework:
180 </p>
181 <pre>
182 local s = Movie:new_selector()
183 s:add_where{ 'id = ?', param.get_id() }
184 s:single_object_mode() -- return single object instead of list
185 local movie = s:exec()</pre>
186 <p>
187 A short form of the above query would be:
188 </p>
189 <pre>
190 local movie = Movie:new_selector():add_where{ 'id = ?', param.get_id() }:single_object_mode():exec()</pre>
191 <p>
192 For more examples about how to use the model system, please take a look at the demo application.
193 </p>
194 <h2>The Model-View-Action (MVA) concept</h2>
195 <p>
196 As opposed to other web application frameworks, WebMCP does not use a Model-View-Controller (MVC) concept, but a Model-View-Action (MVA) concept.
197 </p>
198 <h3>Models</h3>
199 <p>
200 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.
201 </p>
202 <h3>Views</h3>
203 <p>
204 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.
205 </p>
206 <h3>Actions</h3>
207 <p>
208 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.
209 </p>
210 <h2>Directory structure of a WebMCP application</h2>
211 <ul>
212 <li>
213 Base Directory
214 <ul>
215 <li>
216 <tt>app/</tt>
217 <ul>
218 <li>
219 <tt>_prefork/</tt>
220 <ul>
221 <li>
222 <tt>10_first_prefork_initializer.lua</tt>
223 </li>
224 <li>
225 <tt>30_third_prefork_initializer.lua</tt>
226 </li>
227 </ul>
228 </li>
229 <li>
230 <tt>_postfork/</tt>
231 <ul>
232 <li>
233 <tt>01_first_postfork_initializer.lua</tt>
234 </li>
235 <li>
236 <tt>03_third_postfork_initializer.lua</tt>
237 </li>
238 </ul>
239 </li>
240 <li>
241 <tt>main/</tt>
242 <ul>
243 <li>
244 <tt>_prefork/</tt>
245 <ul>
246 <li>
247 <tt>20_second_prefork_initializer.lua</tt>
248 </li>
249 </ul>
250 </li>
251 <li>
252 <tt>_postfork/</tt>
253 <ul>
254 <li>
255 <tt>02_second_postfork_initializer.lua</tt>
256 </li>
257 </ul>
258 </li>
259 <li>
260 <tt>_filter/</tt>
261 <ul>
262 <li>
263 <tt>10_first_filter.lua</tt>
264 </li>
265 <li>
266 <tt>30_third_filter.lua</tt>
267 </li>
268 <li>&hellip;</li>
269 </ul>
270 </li>
271 <li>
272 <tt>_filter_action/</tt>
273 <ul>
274 <li>
275 <tt>20_second_filter.lua</tt>
276 </li>
277 <li>&hellip;</li>
278 </ul>
279 </li>
280 <li>
281 <tt>_filter_view/</tt>
282 <ul>
283 <li>&hellip;</li>
284 </ul>
285 </li>
286 <li>
287 <tt>_layout/</tt>
288 <ul>
289 <li>&hellip;</li>
290 </ul>
291 </li>
292 <li>
293 <tt>index/</tt>
294 <ul>
295 <li>
296 <tt>_action/</tt>
297 <ul>
298 <li>
299 <i>action_name</i><tt>.lua</tt>
300 </li>
301 <li>
302 <i>another_action_name</i><tt>.lua</tt>
303 </li>
304 <li>&hellip;</li>
305 </ul>
306 </li>
307 <li>
308 <tt>index.lua</tt>
309 </li>
310 <li>
311 <i>other_view_name</i><tt>.lua</tt>
312 </li>
313 <li>&hellip;</li>
314 </ul>
315 </li>
316 <li>
317 <i>other_module_name</i><tt>/</tt>
318 <ul>
319 <li>&hellip;</li>
320 </ul>
321 </li>
322 </ul>
323 </li>
324 <li>
325 <i>other_application_name</i><tt>/</tt>
326 <ul>
327 <li>&hellip;</li>
328 </ul>
329 </li>
330 </ul>
331 </li>
332 <li>
333 <tt>config/</tt>
334 <ul>
335 <li>
336 <tt>development.lua</tt>
337 </li>
338 <li>
339 <tt>production.lua</tt>
340 <li>
341 <li>
342 <i>other_config_name</i><tt>.lua</tt>
343 </li>
344 <li>&hellip;</li>
345 </ul>
346 </li>
347 <li>
348 <tt>db/</tt>
349 <ul>
350 <li>
351 <tt>schema.sql</tt>
352 </li>
353 </ul>
354 </li>
355 <li>
356 <tt>locale/</tt>
357 <ul>
358 <li>
359 <tt>translations.de.lua</tt>
360 </li>
361 <li>
362 <tt>translations.en.lua</tt>
363 </li>
364 <li>
365 <tt>translations.</tt><i>languagecode</i><tt>.lua</tt>
366 </li>
367 <li>&hellip;</li>
368 </ul>
369 </li>
370 <li>
371 <tt>model/</tt>
372 <ul>
373 <li>
374 <i>model_name</i><tt>.lua</tt>
375 </li>
376 <li>
377 <i>another_model_name</i><tt>.lua</tt>
378 </li>
379 <li>&hellip;</li>
380 </ul>
381 </li>
382 <li>
383 <tt>static/</tt>
384 <ul>
385 <li>&hellip; (images, javascript, ...)</li>
386 </ul>
387 </li>
388 <li>
389 <tt>tmp/</tt> (writable by the web process)
390 </li>
391 </ul>
392 </li>
393 </ul>
394 <h2>Starting your application</h2>
395 <p>
396 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:
397 </p>
398 <ol>
399 <li>
400 Path of the WebMCP framework directory, e.g. <tt>./framework</tt>
401 </li>
402 <li>
403 Path of your application's directory, e.g. <tt>./demo-app</tt>
404 </li>
405 <li>
406 Name of your applicaiton (usually <tt>main</tt>)
407 </li>
408 <li>
409 Name of configuration (e.g. <tt>devel</tt> to use config/devel.lua)
410 </li>
411 </ol>
412 <h2>Automatically generated reference for the WebMCP environment</h2>
413 <ul>

Impressum / About Us