Commit 6224ecd1 authored by zauberstuhl's avatar zauberstuhl

Replace echo server with go version

parent 9929ea94
# Dummy Project
Related to [feneas.git.feneas.org/federation/testsuite](https://feneas.git.feneas.org/federation/testsuite/#testsuite)
## Dependencies
* postgresql-client
* nmap
* curl
* inotify-tools
module git.feneas.org/feneas/federation/testsuite-testproject
go 1.12
package main
import (
"net/http"
"log"
"os"
"fmt"
)
func main() {
http.HandleFunc("/send", func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s\n", r.Method, r.URL.String())
_, err := http.Get(r.URL.RawQuery)
if err != nil {
fmt.Fprintf(w, "%s\n", err.Error())
} else {
fmt.Fprintf(w, "ok\n")
}
})
http.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s\n", r.Method, r.URL.String())
fmt.Fprintf(w, "pong\n")
})
listening := ":8080"
if len(os.Args) > 1 {
listening = os.Args[1]
}
log.Printf("Listening on %s..\n", listening)
log.Fatal(http.ListenAndServe(listening, nil))
}
#!/bin/bash
[ "$#" -ne 3 ] && {
echo "$0 [DATABASE] [DATABASEHOST] [PORT]"
exit 1
}
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;'"
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
# listen and serve
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=$!
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
eval $psqlAdd
else
curl http://$remote/add
fi
fi
if [[ $req =~ '/remove' ]]; then
if [[ "$remote" == "/remove" ]]; then
eval $psqlRemove
else
curl http://$remote/remove
fi
fi
done
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