page.lp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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="http://code.google.com/p/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. -- Setup a trace callback, to show SQL statements we'll be executing.
  28. -- db:trace(function(data, sql) mg.write('Executing: ', sql: '\n') end, nil)
  29. -- Create a table if it is not created already
  30. db:exec([[
  31. CREATE TABLE IF NOT EXISTS requests (
  32. id INTEGER PRIMARY KEY AUTOINCREMENT,
  33. timestamp NOT NULL,
  34. method NOT NULL,
  35. uri NOT NULL,
  36. addr
  37. );
  38. ]])
  39. -- Add entry about this request
  40. local stmt = db:prepare(
  41. 'INSERT INTO requests VALUES(NULL, datetime("now"), ?, ?, ?);');
  42. stmt:bind_values(mg.request_info.request_method,
  43. mg.request_info.uri,
  44. mg.request_info.remote_port)
  45. stmt:step()
  46. stmt:finalize()
  47. -- Show all previous records
  48. mg.write('Previous requests:\n')
  49. stmt = db:prepare('SELECT * FROM requests ORDER BY id DESC;')
  50. while stmt:step() == sqlite3.ROW do
  51. local v = stmt:get_values()
  52. mg.write(v[1] .. ' ' .. v[2] .. ' ' .. v[3] .. ' '
  53. .. v[4] .. ' ' .. v[5] .. '\n')
  54. end
  55. -- Close database
  56. db:close()
  57. ?>
  58. </pre></p>
  59. </body></html>