testproject.sh 1.16 KB
Newer Older
zauberstuhl's avatar
zauberstuhl committed
1 2 3 4 5 6 7 8 9 10 11
#!/bin/bash

[ "$#" -ne 3 ] && {
  echo "$0 [DATABASE] [DATABASEHOST] [PORT]"
  exit 1
}

DATABASE=$1
DATABASEHOST=$2
PORT=$3

12
tmpLog=$(mktemp)
zauberstuhl's avatar
zauberstuhl committed
13 14 15 16 17 18 19 20
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;'"
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

21
# listen and serve
22 23 24 25
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=$!
26

27 28 29 30 31 32 33 34 35
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)
36
  remote=$(echo $req |cut -d' ' -f2 |cut -d@ -f2)
zauberstuhl's avatar
zauberstuhl committed
37
  if [[ $req =~ '/add' ]]; then
38
    if [[ "$remote" == "/add" ]]; then
39 40
      eval $psqlAdd
    else
zauberstuhl's avatar
zauberstuhl committed
41
      #curl http://$remote/add
42
    fi
zauberstuhl's avatar
zauberstuhl committed
43 44
  fi
  if [[ $req =~ '/remove' ]]; then
45
    if [[ "$remote" == "/remove" ]]; then
46 47 48 49
      eval $psqlRemove
    else
      curl http://$remote/remove
    fi
zauberstuhl's avatar
zauberstuhl committed
50 51
  fi
done