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