page.lp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. );
  40. ]])
  41. -- Add entry about this request
  42. local stmt = db:prepare(
  43. 'INSERT INTO requests VALUES(NULL, datetime("now"), ?, ?, ?);');
  44. stmt:bind_values(mg.request_info.request_method,
  45. mg.request_info.uri,
  46. mg.request_info.remote_port)
  47. stmt:step()
  48. stmt:finalize()
  49. -- Show all previous records
  50. mg.write('Previous requests:\n')
  51. stmt = db:prepare('SELECT * FROM requests ORDER BY id DESC;')
  52. while stmt:step() == sqlite3.ROW do
  53. local v = stmt:get_values()
  54. mg.write(v[1] .. ' ' .. v[2] .. ' ' .. v[3] .. ' '
  55. .. v[4] .. ' ' .. v[5] .. '\n')
  56. end
  57. -- Close database
  58. db:close()
  59. ?>
  60. </pre></p>
  61. </body></html>