webmcp

view doc/autodoc-header.htmlpart @ 370:35d1b6485145

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

Impressum / About Us