page.lp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. HTTP/1.0 200 OK
  2. Content-Type: text/html
  3. <html><body>
  4. <p>This is an example Lua server page served by
  5. <a href="http://code.google.com/p/civetweb">Civetweb web server</a>.
  6. Civetweb has Lua, Sqlite, and other functionality built in the binary.
  7. This example page stores the request in the Sqlite database, and shows
  8. all requests done previously.</p>
  9. <p> Today is <? mg.write(os.date("%A")) ?>
  10. <pre>
  11. <?
  12. -- for k,v in pairs(_G) do mg.write(k, '\n') end
  13. -- Open database
  14. local db = sqlite3.open('requests.db')
  15. -- Setup a trace callback, to show SQL statements we'll be executing.
  16. -- db:trace(function(data, sql) mg.write('Executing: ', sql: '\n') end, nil)
  17. -- Create a table if it is not created already
  18. db:exec([[
  19. CREATE TABLE IF NOT EXISTS requests (
  20. id INTEGER PRIMARY KEY AUTOINCREMENT,
  21. timestamp NOT NULL,
  22. method NOT NULL,
  23. uri NOT NULL,
  24. addr
  25. );
  26. ]])
  27. -- Add entry about this request
  28. local stmt = db:prepare(
  29. 'INSERT INTO requests VALUES(NULL, datetime("now"), ?, ?, ?);');
  30. stmt:bind_values(mg.request_info.request_method,
  31. mg.request_info.uri,
  32. mg.request_info.remote_port)
  33. stmt:step()
  34. stmt:finalize()
  35. -- Show all previous records
  36. mg.write('Previous requests:\n')
  37. stmt = db:prepare('SELECT * FROM requests ORDER BY id DESC;')
  38. while stmt:step() == sqlite3.ROW do
  39. local v = stmt:get_values()
  40. mg.write(v[1] .. ' ' .. v[2] .. ' ' .. v[3] .. ' '
  41. .. v[4] .. ' ' .. v[5] .. '\n')
  42. end
  43. -- Close database
  44. db:close()
  45. ?>
  46. </pre></body></html>