civet.lua 911 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. socket = require "socket"
  2. local civet = {}
  3. -- default params
  4. civet.port=12345
  5. civet.max_retry=100
  6. civet.start_delay=0.1
  7. function civet.start(docroot)
  8. -- TODO: use a property
  9. docroot = docroot or 'ci/test/01_basic/docroot'
  10. assert(io.popen('./civetweb'
  11. .. " -listening_ports " .. civet.port
  12. .. " -document_root " .. docroot
  13. .. " > /dev/null 2>&1 &"
  14. ))
  15. -- wait until the server answers
  16. for i=1,civet.max_retry do
  17. local s = socket.connect('127.0.0.1', civet.port)
  18. if s then
  19. s:close()
  20. break
  21. end
  22. socket.select(nil, nil, civet.start_delay) -- sleep
  23. end
  24. end
  25. function civet.stop()
  26. os.execute('killall civetweb')
  27. -- wait until the server port closes
  28. for i=1,civet.max_retry do
  29. local s = socket.connect('127.0.0.1', civet.port)
  30. if not s then
  31. break
  32. end
  33. s:close()
  34. socket.select(nil, nil, civet.start_delay) -- sleep
  35. end
  36. end
  37. return civet