# HG changeset patch # User jbe # Date 1503320481 -7200 # Node ID 55a43a67e9ada3d61fd657476c8a85e3e790aa60 # Parent 02d505b6090c90c47fac47650bcafe5698cac6c1 Further improvements to the documentation of the environment system diff -r 02d505b6090c -r 55a43a67e9ad doc/autodoc-header.htmlpart --- a/doc/autodoc-header.htmlpart Mon Aug 21 14:50:27 2017 +0200 +++ b/doc/autodoc-header.htmlpart Mon Aug 21 15:01:21 2017 +0200 @@ -309,11 +309,19 @@ To avoid accidental programming errors, WebMCP forbids setting of global variables by default. This is overridden by using the prefix "_G." (see reference) when setting the variable, e.g. _G.myvar = 7, or by setting the variable in a file with the same name of the global varaible (but suffixed with ".lua") in the env/ directory of the framework or application. Note, however, that the lifetime of global variables is not predictable as it depends on process recycling of the Moonbridge webserver (one fork will handle more than one request) and because there may be multiple forks of the Lua machine which all have their own global variable space (there is usually more than one fork).

+ If an application needs to store request related data, the global table app should be used (e.g. app.myvar = true instead of _G.myvar = true). The app table gets automatically initialized (i.e. emptied) for each request. +

+

Global variables are still useful when providing access to libraries, for example. WebMCP automatically loads certain libraries and functions though an autoloader mechanism. On read-accessing any unknown variable, WebMCP will search the framework's and application's env/ directories for an appropriate file (e.g. "my_func.lua if you invoke "my_func()") or a matching directory (e.g. "my_module/ if you access "my_module.my_func()). In case of an existing directory in env/, an empty table with autoloading capabilities is automatically created as global variable with the name of the directory. The autoloading mechanism allows directories to contain further files which can be used to initialize variables within that table when accessed. Directories can also contain a special file called "__init.lua" that always gets executed when the table is accessed for the first time. The env/ root directory can also contain a file ("env/__init__.lua") which gets executed before any configuration is loaded.

- If an application needs to store request related data, the global table app should be used. This table gets automatically initialized (i.e. emptied) for each request. + A good place to store utility functions is a global table called util. This table will be automatically accessible if you create a env/util/ directory in your WebMCP application. To provide a function util.myfunc(...) simply create a file env/util/myfunc.lua, with the following function definition:

+
+-- filename: env/util/myfunc.lua
+function util.myfunc()
+  slot.put_into("hello", "Hello World!")
+end

Directory structure of a WebMCP application

Summarizing information from the previous section, we get the following directory structure for a WebMCP application: @@ -385,9 +393,9 @@ env/