page.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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><a href="http://www.lua.org/docs.html">' .. _VERSION .. "</a> server pages</li>")
  26. if sqlite3 then
  27. mg.write('<li><a href="http://lua.sqlite.org/index.cgi/doc/tip/doc/lsqlite3.wiki">sqlite3</a> binding</li>')
  28. end
  29. if lfs then
  30. mg.write('<li><a href="https://keplerproject.github.io/luafilesystem/manual.html">lua file system</a></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, errcode, errmsg = sqlite3.open('requests.db')
  38. if db then
  39. -- Note that the data base is located in the current working directory
  40. -- of the process if no other path is given here.
  41. -- Setup a trace callback, to show SQL statements we'll be executing.
  42. -- db:trace(function(data, sql) mg.write('Executing: ', sql: '\n') end, nil)
  43. -- Create a table if it is not created already
  44. db:exec([[
  45. CREATE TABLE IF NOT EXISTS requests (
  46. id INTEGER PRIMARY KEY AUTOINCREMENT,
  47. timestamp NOT NULL,
  48. method NOT NULL,
  49. uri NOT NULL,
  50. addr,
  51. civetwebversion,
  52. luaversion,
  53. aux
  54. );
  55. ]])
  56. -- Add colums to table created with older version
  57. db:exec("ALTER TABLE requests ADD COLUMN civetwebversion;")
  58. db:exec("ALTER TABLE requests ADD COLUMN luaversion;")
  59. db:exec("ALTER TABLE requests ADD COLUMN aux;")
  60. -- Add entry about this request
  61. local stmt = db:prepare(
  62. 'INSERT INTO requests VALUES(NULL, datetime("now"), ?, ?, ?, ?, ?, ?);');
  63. stmt:bind_values(mg.request_info.request_method,
  64. mg.request_info.uri,
  65. mg.request_info.remote_port,
  66. mg.version,
  67. _VERSION,
  68. ""
  69. )
  70. stmt:step()
  71. stmt:finalize()
  72. -- Show all previous records
  73. mg.write('<table>\n')
  74. mg.write("<tr>\n")
  75. mg.write("<th>id</th>\n")
  76. mg.write("<th>timestamp</th>\n")
  77. mg.write("<th>method</th>\n")
  78. mg.write("<th>uri</th>\n")
  79. mg.write("<th>addr</th>\n")
  80. mg.write("<th>civetweb</th>\n")
  81. mg.write("<th>lua</th>\n")
  82. mg.write("<th>aux</th>\n")
  83. mg.write("</tr>\n")
  84. stmt = db:prepare('SELECT * FROM requests ORDER BY id DESC;')
  85. while stmt:step() == sqlite3.ROW do
  86. local v = stmt:get_values()
  87. mg.write("<tr>\n")
  88. local i = 1
  89. while (v[i]) do
  90. mg.write("<td>" .. v[i] .. "</td>\n")
  91. i = i+1
  92. end
  93. mg.write("</tr>\n")
  94. end
  95. mg.write("</table>\n")
  96. -- Close database
  97. db:close()
  98. else
  99. mg.write("DB error:\n")
  100. mg.write("code = " .. tostring(errcode) .. "\n")
  101. mg.write("msg = " .. tostring(msg) .. "\n")
  102. end
  103. mg.write([[
  104. </pre>
  105. </p>
  106. </body>
  107. </html>
  108. ]])