Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongoose
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
esp
mongoose
Commits
d5bc58c3
Commit
d5bc58c3
authored
Jan 31, 2013
by
Sergey Lyubka
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Using sqlite as an example
parent
51a80e48
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
45 additions
and
33 deletions
+45
-33
page.lp
test/page.lp
+45
-33
No files found.
test/page.lp
View file @
d5bc58c3
<html>
<?
<p>
Prime numbers from 0 to 100, calculated by Lua:
</p>
-- Lua server pages have full control over the output, including HTTP
<?
-- headers they send to the client. Send HTTP headers:
print('HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n')
function is_prime(n)
?>
<html><body>
if n <= 0 then return false end
if n <= 2 then return true end
<p>
This is an example Lua server page served by
if (n % 2 == 0) then return false end
<a
href=
"http://code.google.com/p/mongoose"
>
Mongoose web server
</a>
.
for i = 3, n / 2, 2 do
Mongoose has Lua, Sqlite, and other functionality built in the binary.
if (n % i == 0) then return false end
This example page stores the request in the Sqlite database, and shows
end
all requests done previously.
</p>
return true
end
<pre>
<?
for i = 1, 100 do
-- Open database
if is_prime(i) then print('<span>' .. i .. '</span> ') end
local db = sqlite3.open('requests.db')
end
-- Setup a trace callback, to show SQL statements we'll be executing.
?>
-- db:trace(function(data, sql) print('Executing: ' .. sql .. '\n') end, nil)
<p>
Reading POST data from Lua (click submit):
</p>
-- Create a table if it is not created already
<form
method=
"POST"
><input
type=
"text"
name=
"t1"
/><input
type=
"submit"
></form>
db:exec([[
CREATE TABLE IF NOT EXISTS requests (
<pre>
id INTEGER PRIMARY KEY AUTOINCREMENT,
POST data: [
<? post_data = read() print(post_data) ?>
]
timestamp NOT NULL,
request method: [
<? print(request_info.request_method) ?>
]
method NOT NULL,
IP/port: [
<? print(request_info.remote_ip, ':', request_info.remote_port) ?>
]
uri NOT NULL,
URI: [
<? print(request_info.uri) ?>
]
user_agent
HTTP version [
<? print(request_info.http_version) ?>
]
);
HEADERS:
]])
<?
for name, value in pairs(request_info.http_headers) do
-- Add entry about this request
print(name, ':', value, '\n')
local stmt = db:prepare(
'INSERT INTO requests VALUES(NULL, datetime("now"), ?, ?, ?);');
stmt:bind_values(request_info.request_method, request_info.uri,
request_info.http_headers['User-Agent'])
stmt:step()
stmt:finalize()
-- Show all previous records
print('Previous requests:\n')
stmt = db:prepare('SELECT * FROM requests ORDER BY id DESC;')
while stmt:step() == sqlite3.ROW do
local v = stmt:get_values()
print(v[1] .. ' ' .. v[2] .. ' ' .. v[3] .. ' '
.. v[4] .. ' ' .. v[5] .. '\n')
end
end
?>
</pre>
</html>
-- Close database
db:close()
?>
</pre></body></html>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment