1HTTP/1.0 200 OK
2Content-Type: text/html
3
4<html><body>
5
6
7<p>This is another example of a Lua server page, served by
8<a href="https://github.com/civetweb/civetweb/">CivetWeb web server</a>.
9</p><p>
10The 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      civetwebversion,
46      luaversion,
47      aux
48    );
49  ]])
50
51  -- Add colums to table created with older version
52  db:exec("ALTER TABLE requests ADD COLUMN civetwebversion;")
53  db:exec("ALTER TABLE requests ADD COLUMN luaversion;")
54  db:exec("ALTER TABLE requests ADD COLUMN aux;")
55
56  -- Add entry about this request
57  local stmt = db:prepare(
58    'INSERT INTO requests VALUES(NULL, datetime("now"), ?, ?, ?, ?, ?, ?);');
59  stmt:bind_values(mg.request_info.request_method,
60                   mg.request_info.uri,
61                   mg.request_info.remote_port,
62                   mg.version,
63                   _VERSION,
64                   ""
65                   )
66
67  -- Show all previous records
68  mg.write('Previous requests:\n')
69  stmt = db:prepare('SELECT * FROM requests ORDER BY id DESC;')
70  while stmt:step() == sqlite3.ROW do
71    local v = stmt:get_values()
72    mg.write(v[1] .. ' ' .. v[2] .. ' ' .. v[3] .. ' '
73          .. v[4] .. ' ' .. v[5] .. '\n')
74  end
75
76  -- Close database
77  db:close()
78?>
79</pre></p>
80
81</body></html>
82