Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
mongoose
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ganil-acq
GANILinux
linux-service
library
mongoose
Commits
a58bb71c
Commit
a58bb71c
authored
12 years ago
by
Sergey Lyubka
Browse files
Options
Downloads
Patches
Plain Diff
Added Lua section to the user manual
parent
fc9a9593
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
UserManual.md
+44
-0
44 additions, 0 deletions
UserManual.md
test/page.lp
+9
-0
9 additions, 0 deletions
test/page.lp
with
53 additions
and
0 deletions
UserManual.md
+
44
−
0
View file @
a58bb71c
...
@@ -279,6 +279,50 @@ must be for a file name only, not including directory name. Example:
...
@@ -279,6 +279,50 @@ must be for a file name only, not including directory name. Example:
mongoose -hide_files_patterns secret.txt|even_more_secret.txt
mongoose -hide_files_patterns secret.txt|even_more_secret.txt
# Lua Server Pages
Pre-built Windows and Mac mongoose binaries have built-in Lua Server Pages
support. That means it is possible to write PHP-like scripts with mongoose,
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/
)
.
To create a Lua Page, make sure a file has
`.lp`
extension. For example,
let's say it is going to be
`my_page.lp`
. The contents of the file, just like
with PHP, is HTML with embedded Lua code. Lua code must be enclosed in
`<? ?>`
blocks, and can appear anywhere on the page. For example, to
print current weekday name, one can write:
<p>
<span>Today is:</span>
<? print(os.date("%A")) ?>
</p>
Note that this example uses function
`print()`
, which prints data to the
web page. Using function
`print()`
is the way to generate web content from
inside Lua code. In addition to
`print()`
, all standard library functions
are accessible from the Lua code (please check reference manual for details),
and also information about the request is available in
`request_info`
object,
like request method, all headers, etcetera. Please refer to
`struct mg_request_info`
definition in
[
mongoose.h
](
https://github.com/valenok/mongoose/blob/master/mongoose.h
)
to see what kind of information is present in
`request_info`
object. Also,
[
page.lp
](
https://github.com/valenok/mongoose/blob/master/test/page.lp
)
contains some example code that uses
`request_info`
.
One substantial difference of mongoose's Lua Pages and PHP is that Mongoose
expects Lua page to output HTTP headers. Therefore,
**
at the very beginning of
every Lua Page must be a Lua block that outputs HTTP headers
**
, like this:
<? print('HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n') ?>
<html><body>
... the rest of the web page ...
It is easy to do things like redirects, for example:
<? print('HTTP/1.0 302 Found\r\nLocation: http://google.com\r\n\r\n') ?>
# Common Problems
# Common Problems
-
PHP doesn't work - getting empty page, or 'File not found' error. The
-
PHP doesn't work - getting empty page, or 'File not found' error. The
reason for that is wrong paths to the interpreter. Remember that with PHP,
reason for that is wrong paths to the interpreter. Remember that with PHP,
...
...
This diff is collapsed.
Click to expand it.
test/page.lp
+
9
−
0
View file @
a58bb71c
...
@@ -2,6 +2,7 @@
...
@@ -2,6 +2,7 @@
-- Lua server pages have full control over the output, including HTTP
-- Lua server pages have full control over the output, including HTTP
-- headers they send to the client. Send HTTP headers:
-- headers they send to the client. Send HTTP headers:
print('HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n')
print('HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n')
?>
<html><body>
?>
<html><body>
<p>
This is an example Lua server page served by
<p>
This is an example Lua server page served by
...
@@ -10,6 +11,14 @@ Mongoose has Lua, Sqlite, and other functionality built in the binary.
...
@@ -10,6 +11,14 @@ Mongoose has Lua, Sqlite, and other functionality built in the binary.
This example page stores the request in the Sqlite database, and shows
This example page stores the request in the Sqlite database, and shows
all requests done previously.
</p>
all requests done previously.
</p>
<p>
Today is
<? print(os.date("%A")) ?>
<p>
HTTP headers:
<br>
<?
for name, value in pairs(request_info.http_headers) do
print(name, ' : ', value, '<br>')
end
?>
<pre>
<pre>
<?
<?
-- Open database
-- Open database
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment