LuaSqlite.md 2.83 KB
Newer Older
Sergey Lyubka's avatar
Sergey Lyubka committed
1 2
# Mongoose Lua Server Pages

Sergey Lyubka's avatar
Sergey Lyubka committed
3 4 5
Pre-built Windows and Mac mongoose binaries support Lua Server Pages
functionality.
That means it is possible to write PHP-like scripts with mongoose
Sergey Lyubka's avatar
Sergey Lyubka committed
6 7 8 9 10
using Lua programming language instead of PHP. Lua is known
for it's speed and small size. Mongoose uses Lua version 5.2.1, the
documentation for it can be found at
[Lua 5.2 reference manual](http://www.lua.org/manual/5.2/).

Sergey Lyubka's avatar
Sergey Lyubka committed
11 12 13 14 15 16
To create a Lua Page, make a file that is called `<something>.lp`. For example,
let's say it is going to be `my_page.lp`. It is important to have a file
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
`<?  ?>` blocks, and can appear anywhere on the page.
Sergey Lyubka's avatar
Sergey Lyubka committed
17

Sergey Lyubka's avatar
Sergey Lyubka committed
18 19 20 21 22 23 24
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>
Sergey Lyubka's avatar
Sergey Lyubka committed
25 26 27 28

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
Sergey Lyubka's avatar
Sergey Lyubka committed
29 30 31
are accessible from the Lua code (please check reference manual for details).
Information about the request is available in `mg.request_info` object.
I contains request method, all headers, etcetera. Please refer to
Sergey Lyubka's avatar
Sergey Lyubka committed
32 33
`struct mg_request_info` definition in
[mongoose.h](https://github.com/cesanta/mongoose/blob/master/mongoose.h)
Sergey Lyubka's avatar
Sergey Lyubka committed
34
to see what is available in `mg.request_info`. Also,
Sergey Lyubka's avatar
Sergey Lyubka committed
35 36
[page.lp](https://github.com/cesanta/mongoose/blob/master/test/page.lp) and
[prime_numbers.lp](https://github.com/cesanta/mongoose/blob/master/examples/lua/prime_numbers.lp)
Sergey Lyubka's avatar
Sergey Lyubka committed
37 38
contain some example code that uses `request_info` and other functions,
like form submission.
Sergey Lyubka's avatar
Sergey Lyubka committed
39 40 41 42 43 44

Mongoose exports the following to the Lua server page:

    mg.write(str)     -- writes string to the client
    mg.onerror(msg)   -- error handler, can be overridden
    mg.request_info   -- a table with request information
Sergey Lyubka's avatar
Sergey Lyubka committed
45
    sqlite3           -- Sqlite3 interface
Sergey Lyubka's avatar
Sergey Lyubka committed
46

Sergey Lyubka's avatar
Sergey Lyubka committed
47 48 49
slite3 functions are documented at [lua.sqlite.org](http://lua.sqlite.org/),
and usage example is at
[page.lp](https://github.com/cesanta/mongoose/blob/master/test/page.lp).
Sergey Lyubka's avatar
Sergey Lyubka committed
50

Sergey Lyubka's avatar
Sergey Lyubka committed
51 52
Using Lua scripting it is easy to emulate SSI functionality. For example,
to include the content of another file, one can write:
Sergey Lyubka's avatar
Sergey Lyubka committed
53

Sergey Lyubka's avatar
Sergey Lyubka committed
54
    <? mg.write(io.open('MY_FILE.TXT'):read('*all')) ?>
Sergey Lyubka's avatar
Sergey Lyubka committed
55

Sergey Lyubka's avatar
Sergey Lyubka committed
56
To serve a Lua Page, mongoose creates Lua context. That context is used for
Sergey Lyubka's avatar
Sergey Lyubka committed
57 58 59
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
variable is visible in the block that follows.