mirror of
https://github.com/lammertb/libhttp.git
synced 2025-08-17 22:21:06 +03:00
Document Lua Server Pages and Lua scripts
This commit is contained in:
@@ -338,38 +338,52 @@ websockets may also be served from a different directory. By default,
|
|||||||
the document_root is used as websocket_root as well.
|
the document_root is used as websocket_root as well.
|
||||||
|
|
||||||
|
|
||||||
# Lua Server Pages
|
# Lua Scripts and Lua Server Pages
|
||||||
Pre-built Windows and Mac civetweb binaries have built-in Lua Server Pages
|
Pre-built Windows and Mac civetweb binaries have built-in Lua scripting
|
||||||
support. That means it is possible to write PHP-like scripts with civetweb,
|
support as well as support for Lua Server Pages.
|
||||||
using Lua programming language instead of PHP. Lua is known
|
|
||||||
for it's speed and small size. Civetweb uses Lua version 5.2.2, the
|
|
||||||
documentation for it can be found at
|
|
||||||
[Lua 5.2 reference manual](http://www.lua.org/manual/5.2/).
|
|
||||||
|
|
||||||
To create a Lua Page, make sure a file has `.lp` extension. For example,
|
Lua scripts (default extension: *.lua) use plain Lua syntax.
|
||||||
let's say it is going to be `my_page.lp`. The contents of the file, just like
|
The body of the script file is not sent directly to the client,
|
||||||
with PHP, is HTML with embedded Lua code. Lua code must be enclosed in
|
the Lua script must send header and content of the web page by calling
|
||||||
`<? ?>` blocks, and can appear anywhere on the page. For example, to
|
the function mg.write(text).
|
||||||
print current weekday name, one can write:
|
|
||||||
|
Lua Server Pages (default extensions: *.lsp, *.lp) are html pages containing
|
||||||
|
script elements similar to PHP, using the Lua programming language instead of
|
||||||
|
PHP. Lua script elements must be enclosed in `<? ?>` blocks, and can appear
|
||||||
|
anywhere on the page. For example, to print current weekday name, one can
|
||||||
|
write:
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
<span>Today is:</span>
|
<span>Today is:</span>
|
||||||
<? mg.write(os.date("%A")) ?>
|
<? mg.write(os.date("%A")) ?>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
Lua is known for it's speed and small size. Civetweb uses Lua version 5.2.2,
|
||||||
|
the documentation for it can be found at
|
||||||
|
[Lua 5.2 reference manual](http://www.lua.org/manual/5.2/).
|
||||||
|
|
||||||
|
|
||||||
Note that this example uses function `mg.write()`, which prints data to the
|
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
|
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
|
inside Lua code. In addition to `mg.write()`, all standard library functions
|
||||||
are accessible from the Lua code (please check reference manual for details),
|
are accessible from the Lua code (please check reference manual for details),
|
||||||
and also information about the request is available in `mg.request_info` object,
|
and also information about the request is available in `mg.request_info`
|
||||||
like request method, all headers, etcetera. Please refer to
|
object, like request method, all headers, etcetera.
|
||||||
`struct mg_request_info` definition in
|
|
||||||
|
[page2.lua](https://github.com/sunsetbrew/civetweb/blob/master/test/page2.lua)
|
||||||
|
is an example for a plain Lua script.
|
||||||
|
|
||||||
|
[page2.lp](https://github.com/sunsetbrew/civetweb/blob/master/test/page2.lp)
|
||||||
|
is an example for a Lua Server Page.
|
||||||
|
|
||||||
|
Both examples show the content of the `mg.request_info` object as the page
|
||||||
|
content. Please refer to `struct mg_request_info` definition in
|
||||||
[civetweb.h](https://github.com/sunsetbrew/civetweb/blob/master/include/civetweb.h)
|
[civetweb.h](https://github.com/sunsetbrew/civetweb/blob/master/include/civetweb.h)
|
||||||
to see what kind of information is present in `mg.request_info` object. Also,
|
to see additional information on the elements of the `mg.request_info` object.
|
||||||
[page.lp](https://github.com/sunsetbrew/civetweb/blob/master/test/page.lp) and
|
|
||||||
[prime_numbers.lp](https://github.com/sunsetbrew/civetweb/blob/master/examples/docroot/prime_numbers.lp)
|
Civetweb also provides SQlite3 access through Lua. An example is given in
|
||||||
contains some example code that uses `request_info` and other functions (form
|
[page.lp](https://github.com/sunsetbrew/civetweb/blob/master/test/page.lp).
|
||||||
submitting for example).
|
|
||||||
|
|
||||||
Civetweb exports the following to the Lua server page:
|
Civetweb exports the following to the Lua server page:
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user