2013-10-18 23:57:59 +08:00
|
|
|
# Mongoose Lua Server Pages
|
|
|
|
|
2014-01-06 21:08:39 +08:00
|
|
|
Pre-built Windows and Mac mongoose binaries support Lua Server Pages
|
|
|
|
functionality.
|
|
|
|
That means it is possible to write PHP-like scripts with mongoose
|
2013-10-18 23:57:59 +08:00
|
|
|
using Lua programming language instead of PHP. Lua is known
|
2014-03-10 03:26:58 +08:00
|
|
|
for it's speed and small size. Mongoose uses Lua version 5.2.3, the
|
2013-10-18 23:57:59 +08:00
|
|
|
documentation for it can be found at
|
|
|
|
[Lua 5.2 reference manual](http://www.lua.org/manual/5.2/).
|
|
|
|
|
2014-03-10 03:26:58 +08:00
|
|
|
To create a Lua Page, make a file that is called `ANY_NAME.lp`. For example,
|
|
|
|
`my_page.lp`. It is important to have a file
|
2014-01-06 21:08:39 +08:00
|
|
|
name that ends up with `.lp`, cause this is the way mongoose recognises
|
|
|
|
Lua Page file. The contents of the file, just like
|
|
|
|
with PHP, is HTML with embedded Lua code. Lua code must be enclosed within
|
2014-03-10 03:26:58 +08:00
|
|
|
`<? ?>` blocks, and can appear anywhere on the page.
|
2013-10-18 23:57:59 +08:00
|
|
|
|
2014-01-06 21:08:39 +08:00
|
|
|
Mongoose does not send HTTP headers for Lua pages. Therefore,
|
|
|
|
every Lua Page must begin with HTTP status line and headers, like this:
|
|
|
|
|
|
|
|
<? mg.write('HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n') ?>
|
|
|
|
<html><body>
|
|
|
|
<span>Today is:</span> <? mg.write(os.date("%A")) ?>
|
|
|
|
</body></html>
|
2013-10-18 23:57:59 +08:00
|
|
|
|
|
|
|
Note that this example uses function `mg.write()`, which prints data to the
|
|
|
|
web page. Using function `mg.write()` is the way to generate web content from
|
|
|
|
inside Lua code. In addition to `mg.write()`, all standard library functions
|
2014-01-06 21:08:39 +08:00
|
|
|
are accessible from the Lua code (please check reference manual for details).
|
2014-03-10 03:26:58 +08:00
|
|
|
Information about the request is available via the `mg.request_info` object.
|
2014-01-06 21:08:39 +08:00
|
|
|
I contains request method, all headers, etcetera. Please refer to
|
2013-10-18 23:57:59 +08:00
|
|
|
`struct mg_request_info` definition in
|
|
|
|
[mongoose.h](https://github.com/cesanta/mongoose/blob/master/mongoose.h)
|
2014-03-10 03:26:58 +08:00
|
|
|
to see what is available via the `mg.request_info` object.
|
|
|
|
Check out [prime_numbers.lp](https://github.com/cesanta/mongoose/blob/master/examples/lua/prime_numbers.lp) for some example.
|
2013-10-18 23:57:59 +08:00
|
|
|
|
2014-03-10 03:26:58 +08:00
|
|
|
Mongoose exports the following to the Lua Server Page:
|
2013-10-18 23:57:59 +08:00
|
|
|
|
|
|
|
mg.write(str) -- writes string to the client
|
|
|
|
mg.onerror(msg) -- error handler, can be overridden
|
|
|
|
mg.request_info -- a table with request information
|
|
|
|
|
2014-01-06 21:08:39 +08:00
|
|
|
Using Lua scripting it is easy to emulate SSI functionality. For example,
|
|
|
|
to include the content of another file, one can write:
|
2013-10-18 23:57:59 +08:00
|
|
|
|
2014-01-06 21:08:39 +08:00
|
|
|
<? mg.write(io.open('MY_FILE.TXT'):read('*all')) ?>
|
2013-10-18 23:57:59 +08:00
|
|
|
|
2014-01-06 21:08:39 +08:00
|
|
|
To serve a Lua Page, mongoose creates Lua context. That context is used for
|
2013-10-18 23:57:59 +08:00
|
|
|
all Lua blocks within the page. That means, all Lua blocks on the same page
|
|
|
|
share the same context. If one block defines a variable, for example, that
|
2014-03-10 03:26:58 +08:00
|
|
|
variable is visible in all following blocks.
|