First draft of the pub/sub notifier

This commit is contained in:
kexkey
2019-05-17 18:29:06 -04:00
committed by kexkey
parent 2c3b28bc84
commit ed71a2ed8f
13 changed files with 231 additions and 19 deletions

View File

@@ -0,0 +1,38 @@
#!/bin/sh
. ./trace.sh
. ./web.sh
. ./response.sh
main() {
trace "Entering main()..."
local msg
local cmd
local response
local response_topic
while read msg; do
trace "[main] msg=${msg}"
cmd=$(echo ${msg} | jq ".cmd" | tr -d '"')
trace "[main] cmd=${cmd}"
response_topic=$(echo ${msg} | jq '."response-topic"' | tr -d '"')
trace "[main] response_topic=${response_topic}"
case "${cmd}" in
web)
response=$(web "${msg}")
publish_response "${response}" "${response_topic}" ${?}
trace "[main] PR"
;;
esac
trace "[main] case finished"
done
}
export TRACING=1
main
exit $?

View File

@@ -0,0 +1,22 @@
#!/bin/sh
. ./trace.sh
publish_response() {
trace "Entering publish_response()..."
local response=${1}
local response_topic=${2}
local returncode=${3}
trace "[publish_response] response=${response}"
trace "[publish_response] response_topic=${response_topic}"
trace "[publish_response] returncode=${returncode}"
trace "[publish_response] mosquitto_pub -h broker -t \"${response_topic}\" -m \"${response}\""
mosquitto_pub -h broker -t "${response_topic}" -m "${response}"
returncode=$?
trace_rc ${returncode}
return ${returncode}
}

View File

@@ -0,0 +1,5 @@
#!/bin/sh
. ./trace.sh
mosquitto_sub -h broker -t notifier | ./requesthandler.sh

View File

@@ -0,0 +1,15 @@
#!/bin/sh
trace()
{
if [ -n "${TRACING}" ]; then
echo "$(date -Is) $$ ${1}" 1>&2
fi
}
trace_rc()
{
if [ -n "${TRACING}" ]; then
echo "$(date -Is) $$ Last return code: ${1}" 1>&2
fi
}

View File

@@ -0,0 +1,76 @@
#!/bin/sh
. ./trace.sh
web() {
trace "Entering web()..."
local msg=${1}
local url
local body
local returncode
local http_code
local result
trace "[web] msg=${msg}"
url=$(echo ${msg} | jq ".url")
trace "[web] url=${url}"
body=$(echo ${msg} | jq -e ".body")
# jq -e will have a return code of 1 if the supplied tag is null.
if [ "$?" -eq "0" ]; then
# body tag not null, so it's a POST
trace "[web] body=${body}"
else
body=
trace "[web] no body, GET request"
fi
http_code=$(curl_it "${url}" "${body}")
returncode=$?
trace_rc ${returncode}
if [ "${returncode}" -eq "0" ]; then
# {"result":"success", "response":"<html></html>"}
result="success"
else
# {"result":"error", "response":"<html></html>"}
result="error"
fi
echo "{\"result\":\"${result}\",\"http_code\":\"${http_code}\"}"
return ${returncode}
}
curl_it() {
trace "Entering curl_it()..."
local url=$(echo "${1}" | tr -d '"')
local data=${2}
local returncode
if [ -n "${data}" ]; then
trace "[curl_it] curl -o /dev/null -w \"%{http_code}\" -H \"Content-Type: application/json\" -H \"X-Forwarded-Proto: https\" -d ${data} ${url}"
rc=$(curl -o /dev/null -w "%{http_code}" -H "Content-Type: application/json" -H "X-Forwarded-Proto: https" -d ${data} ${url})
returncode=$?
else
trace "[curl_it] curl -o /dev/null -w \"%{http_code}\" ${url}"
rc=$(curl -o /dev/null -w "%{http_code}" ${url})
returncode=$?
fi
trace "[curl_it] HTTP return code=${rc}"
trace_rc ${returncode}
echo "${rc}"
if [ "${returncode}" -eq "0" ]; then
if [ "${rc}" -lt "400" ]; then
return 0
else
return ${rc}
fi
else
return ${returncode}
fi
}