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