page.lp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. <pre>
  13. <?
  14. -- Open database
  15. local db = sqlite3.open('requests.db')
  16. -- Setup a trace callback, to show SQL statements we'll be executing.
  17. -- db:trace(function(data, sql) print('Executing: ' .. sql .. '\n') end, nil)
  18. -- Create a table if it is not created already
  19. db:exec([[
  20. CREATE TABLE IF NOT EXISTS requests (
  21. id INTEGER PRIMARY KEY AUTOINCREMENT,
  22. timestamp NOT NULL,
  23. method NOT NULL,
  24. uri NOT NULL,
  25. user_agent
  26. );
  27. ]])
  28. -- Add entry about this request
  29. local stmt = db:prepare(
  30. 'INSERT INTO requests VALUES(NULL, datetime("now"), ?, ?, ?);');
  31. stmt:bind_values(request_info.request_method, request_info.uri,
  32. request_info.http_headers['User-Agent'])
  33. stmt:step()
  34. stmt:finalize()
  35. -- Show all previous records
  36. print('Previous requests:\n')
  37. stmt = db:prepare('SELECT * FROM requests ORDER BY id DESC;')
  38. while stmt:step() == sqlite3.ROW do
  39. local v = stmt:get_values()
  40. print(v[1] .. ' ' .. v[2] .. ' ' .. v[3] .. ' '
  41. .. v[4] .. ' ' .. v[5] .. '\n')
  42. end
  43. -- Close database
  44. db:close()
  45. ?>
  46. </pre></body></html>