Commit 9929ea94 authored by zauberstuhl's avatar zauberstuhl

Fix ncat cmd and clean-up after SIGTERM

parent 80790127
......@@ -9,6 +9,7 @@ DATABASE=$1
DATABASEHOST=$2
PORT=$3
tmpLog=$(mktemp)
psql="psql -h $DATABASEHOST -U postgres -d $DATABASE"
psqlAdd="$psql -c 'insert into testtable values(1);'"
psqlRemove="$psql -c 'delete from testtable where id = 1;'"
......@@ -17,13 +18,21 @@ psqlCount="$psql -q -c 'select count(*) from testtable;'"
# create db schema
$psql -c 'create table if not exists testtable(id serial primary key);' || exit 1
tmplog=$(mktemp)
# listen and serve
ncat -l $PORT -o $tmplog -k -c 'echo -e "HTTP/1.1 200 OK\n\n$('\
'eval $psqlCount |head -3 |tail -n 1 |tr -d \"[:space:]\")"' &
cmd=$psqlCount' |head -3 |tail -1 |tr -d "[:space:]"'
cmd="echo \"HTTP/1.1 200 OK\n\n\"\$($cmd)"
ncat -l $PORT -o $tmpLog -k -c "$cmd" &
ncatPID=$!
while inotifywait -qq -e close_write $tmplog; do
req=$(grep GET $tmplog |tail -1)
function clean_up() {
kill $ncatPID
exit
}
# clean up child process first
trap clean_up SIGHUP SIGINT SIGTERM
while inotifywait -qq -e modify $tmpLog; do
req=$(grep GET $tmpLog |tail -1)
remote=$(echo $req |cut -d' ' -f2 |cut -d@ -f2)
if [[ $req =~ '/add' ]]; then
if [[ "$remote" == "/add" ]]; then
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment