]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | mg.write("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n") |
2 | ||
3 | mg.write([[ | |
4 | <html><body> | |
5 | <p>This is another example of a Lua script, creating a web page served by the | |
11fdf7f2 | 6 | <a href="https://github.com/civetweb/civetweb/">CivetWeb web server</a>. |
7c673cae FG |
7 | </p><p> |
8 | The following features are available: | |
9 | <ul> | |
10 | ]]) | |
11 | ||
12 | mg.write("<li>" .. _VERSION .. " server pages</li>") | |
13 | if sqlite3 then | |
14 | mg.write("<li>sqlite3 binding</li>") | |
15 | end | |
16 | if lfs then | |
17 | mg.write("<li>lua file system</li>") | |
18 | end | |
19 | ||
20 | ||
21 | mg.write("</ul></p>\r\n") | |
22 | mg.write("<p> Today is " .. os.date("%A") .. "</p>\r\n") | |
23 | mg.write("<p> URI is " .. mg.request_info.uri .. "</p>\r\n") | |
24 | ||
25 | mg.write("<p>Database example:\r\n<pre>\r\n") | |
26 | ||
27 | -- Open database | |
28 | local db = sqlite3.open('requests.db') | |
29 | -- Note that the data base is located in the current working directory | |
30 | -- of the process if no other path is given here. | |
31 | ||
32 | -- Setup a trace callback, to show SQL statements we'll be executing. | |
33 | -- db:trace(function(data, sql) mg.write('Executing: ', sql: '\n') end, nil) | |
34 | ||
35 | -- Create a table if it is not created already | |
36 | db:exec([[ | |
37 | CREATE TABLE IF NOT EXISTS requests ( | |
38 | id INTEGER PRIMARY KEY AUTOINCREMENT, | |
39 | timestamp NOT NULL, | |
40 | method NOT NULL, | |
41 | uri NOT NULL, | |
42 | addr | |
43 | ); | |
44 | ]]) | |
45 | ||
46 | -- Add entry about this request | |
47 | local stmt = db:prepare( | |
48 | 'INSERT INTO requests VALUES(NULL, datetime("now"), ?, ?, ?);'); | |
49 | stmt:bind_values(mg.request_info.request_method, | |
50 | mg.request_info.uri, | |
51 | mg.request_info.remote_port) | |
52 | stmt:step() | |
53 | stmt:finalize() | |
54 | ||
55 | -- Show all previous records | |
56 | mg.write('Previous requests:\n') | |
57 | stmt = db:prepare('SELECT * FROM requests ORDER BY id DESC;') | |
58 | while stmt:step() == sqlite3.ROW do | |
59 | local v = stmt:get_values() | |
60 | mg.write(v[1] .. ' ' .. v[2] .. ' ' .. v[3] .. ' ' | |
61 | .. v[4] .. ' ' .. v[5] .. '\n') | |
62 | end | |
63 | ||
64 | -- Close database | |
65 | db:close() | |
66 | ||
67 | mg.write([[ | |
68 | </pre> | |
69 | </p> | |
70 | </body></html> | |
71 | ]]) |