page.lp 2.2 KB

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