civet.lua 899 B

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