HTTP/1.0 200 OK Content-Type: text/html
This is another example of a Lua server page, served by CivetWeb web server.
The following features are available:
Today is mg.write(os.date("%A")) ?>
URI is mg.write(mg.request_info.uri) ?>
URI is =mg.request_info.uri?>
Database example:
-- Open database local db = sqlite3.open('requests.db') -- Note that the data base is located in the current working directory -- of the process if no other path is given here. -- Setup a trace callback, to show SQL statements we'll be executing. -- db:trace(function(data, sql) mg.write('Executing: ', sql: '\n') end, nil) -- Create a table if it is not created already db:exec([[ CREATE TABLE IF NOT EXISTS requests ( id INTEGER PRIMARY KEY AUTOINCREMENT, timestamp NOT NULL, method NOT NULL, uri NOT NULL, addr, civetwebversion, luaversion, aux ); ]]) -- Add colums to table created with older version db:exec("ALTER TABLE requests ADD COLUMN civetwebversion;") db:exec("ALTER TABLE requests ADD COLUMN luaversion;") db:exec("ALTER TABLE requests ADD COLUMN aux;") -- Add entry about this request local stmt = db:prepare( 'INSERT INTO requests VALUES(NULL, datetime("now"), ?, ?, ?, ?, ?, ?);'); stmt:bind_values(mg.request_info.request_method, mg.request_info.uri, mg.request_info.remote_port, mg.version, _VERSION, "" ) -- Show all previous records mg.write('Previous requests:\n') stmt = db:prepare('SELECT * FROM requests ORDER BY id DESC;') while stmt:step() == sqlite3.ROW do local v = stmt:get_values() mg.write(v[1] .. ' ' .. v[2] .. ' ' .. v[3] .. ' ' .. v[4] .. ' ' .. v[5] .. '\n') end -- Close database db:close() ?>