page.lp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?
  2. -- Lua server pages have full control over the output, including HTTP
  3. -- headers they send to the client. Send HTTP headers:
  4. print('HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n')
  5. ?><html><body>
  6. <p>This is an example Lua server page served by
  7. <a href="http://code.google.com/p/mongoose">Mongoose web server</a>.
  8. Mongoose has Lua, Sqlite, and other functionality built in the binary.
  9. This example page stores the request in the Sqlite database, and shows
  10. all requests done previously.</p>
  11. <p> Today is <? print(os.date("%A")) ?>
  12. <p> HTTP headers: <br>
  13. <?
  14. for name, value in pairs(request_info.http_headers) do
  15. print(name, ' : ', value, '<br>')
  16. end
  17. ?>
  18. <pre>
  19. <?
  20. -- Open database
  21. local db = sqlite3.open('requests.db')
  22. -- Setup a trace callback, to show SQL statements we'll be executing.
  23. -- db:trace(function(data, sql) print('Executing: ' .. sql .. '\n') end, nil)
  24. -- Create a table if it is not created already
  25. db:exec([[
  26. CREATE TABLE IF NOT EXISTS requests (
  27. id INTEGER PRIMARY KEY AUTOINCREMENT,
  28. timestamp NOT NULL,
  29. method NOT NULL,
  30. uri NOT NULL,
  31. user_agent
  32. );
  33. ]])
  34. -- Add entry about this request
  35. local stmt = db:prepare(
  36. 'INSERT INTO requests VALUES(NULL, datetime("now"), ?, ?, ?);');
  37. stmt:bind_values(request_info.request_method, request_info.uri,
  38. request_info.http_headers['User-Agent'])
  39. stmt:step()
  40. stmt:finalize()
  41. -- Show all previous records
  42. print('Previous requests:\n')
  43. stmt = db:prepare('SELECT * FROM requests ORDER BY id DESC;')
  44. while stmt:step() == sqlite3.ROW do
  45. local v = stmt:get_values()
  46. print(v[1] .. ' ' .. v[2] .. ' ' .. v[3] .. ' '
  47. .. v[4] .. ' ' .. v[5] .. '\n')
  48. end
  49. -- Close database
  50. db:close()
  51. ?>
  52. </pre></body></html>