page.lua 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. mg.write("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n")
  2. mg.write([[<html>
  3. <head>
  4. <title>Lua SQLite database test</title>
  5. <style>
  6. table, th, td {
  7. border: 1px solid black;
  8. border-collapse: collapse;
  9. border-spacing: 5px;
  10. }
  11. th, td {
  12. padding: 5px;
  13. text-align: left;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <p>This is Lua script example 1, served by the
  19. <a href="https://github.com/civetweb/civetweb">CivetWeb web server</a>,
  20. version ]] .. mg.version .. [[.
  21. </p><p>
  22. The following features are available:
  23. <ul>
  24. ]])
  25. mg.write("<li>" .. _VERSION .. " server pages</li>")
  26. if sqlite3 then
  27. mg.write("<li>sqlite3 binding</li>")
  28. end
  29. if lfs then
  30. mg.write("<li>lua file system</li>")
  31. end
  32. mg.write("</ul></p>\r\n")
  33. mg.write("<p> Today is " .. os.date("%A") .. "</p>\r\n")
  34. mg.write("<p> URI is " .. mg.request_info.uri .. "</p>\r\n")
  35. mg.write("<p>\r\n<pre>\r\n")
  36. -- Open database
  37. local db = sqlite3.open('requests.db')
  38. -- Note that the data base is located in the current working directory
  39. -- of the process if no other path is given here.
  40. -- Setup a trace callback, to show SQL statements we'll be executing.
  41. -- db:trace(function(data, sql) mg.write('Executing: ', sql: '\n') end, nil)
  42. -- Create a table if it is not created already
  43. db:exec([[
  44. CREATE TABLE IF NOT EXISTS requests (
  45. id INTEGER PRIMARY KEY AUTOINCREMENT,
  46. timestamp NOT NULL,
  47. method NOT NULL,
  48. uri NOT NULL,
  49. addr,
  50. civetwebversion,
  51. luaversion,
  52. aux
  53. );
  54. ]])
  55. -- Add colums to table created with older version
  56. db:exec("ALTER TABLE requests ADD COLUMN civetwebversion;")
  57. db:exec("ALTER TABLE requests ADD COLUMN luaversion;")
  58. db:exec("ALTER TABLE requests ADD COLUMN aux;")
  59. -- Add entry about this request
  60. local stmt = db:prepare(
  61. 'INSERT INTO requests VALUES(NULL, datetime("now"), ?, ?, ?, ?, ?, ?);');
  62. stmt:bind_values(mg.request_info.request_method,
  63. mg.request_info.uri,
  64. mg.request_info.remote_port,
  65. mg.version,
  66. _VERSION,
  67. ""
  68. )
  69. stmt:step()
  70. stmt:finalize()
  71. -- Show all previous records
  72. mg.write('<table>\n')
  73. mg.write("<tr>\n")
  74. mg.write("<th>id</th>\n")
  75. mg.write("<th>timestamp</th>\n")
  76. mg.write("<th>method</th>\n")
  77. mg.write("<th>uri</th>\n")
  78. mg.write("<th>addr</th>\n")
  79. mg.write("<th>civetweb</th>\n")
  80. mg.write("<th>lua</th>\n")
  81. mg.write("<th>aux</th>\n")
  82. mg.write("</tr>\n")
  83. stmt = db:prepare('SELECT * FROM requests ORDER BY id DESC;')
  84. while stmt:step() == sqlite3.ROW do
  85. local v = stmt:get_values()
  86. mg.write("<tr>\n")
  87. local i = 1
  88. while (v[i]) do
  89. mg.write("<td>" .. v[i] .. "</td>\n")
  90. i = i+1
  91. end
  92. mg.write("</tr>\n")
  93. end
  94. mg.write("</table>\n")
  95. -- Close database
  96. db:close()
  97. mg.write([[
  98. </pre>
  99. </p>
  100. </body>
  101. </html>
  102. ]])