mirror of
https://github.com/aljazceru/cyphernode.git
synced 2025-12-17 04:35:14 +01:00
Working version of the async pub/sub notifier for callbacks
This commit is contained in:
@@ -13,6 +13,7 @@ main() {
|
||||
local response_topic
|
||||
|
||||
while read msg; do
|
||||
trace "[main] New msg just arrived!"
|
||||
trace "[main] msg=${msg}"
|
||||
|
||||
cmd=$(echo ${msg} | jq ".cmd" | tr -d '"')
|
||||
@@ -25,14 +26,14 @@ main() {
|
||||
web)
|
||||
response=$(web "${msg}")
|
||||
publish_response "${response}" "${response_topic}" ${?}
|
||||
trace "[main] PR"
|
||||
;;
|
||||
esac
|
||||
trace "[main] case finished"
|
||||
trace "[main] msg processed"
|
||||
done
|
||||
}
|
||||
|
||||
export TRACING=1
|
||||
|
||||
main
|
||||
trace "[requesthandler] exiting"
|
||||
exit $?
|
||||
|
||||
@@ -13,6 +13,7 @@ publish_response() {
|
||||
trace "[publish_response] response_topic=${response_topic}"
|
||||
trace "[publish_response] returncode=${returncode}"
|
||||
|
||||
# response=$(echo "${response}" | base64 | tr -d '\n')
|
||||
trace "[publish_response] mosquitto_pub -h broker -t \"${response_topic}\" -m \"${response}\""
|
||||
mosquitto_pub -h broker -t "${response_topic}" -m "${response}"
|
||||
returncode=$?
|
||||
|
||||
@@ -9,7 +9,7 @@ web() {
|
||||
local url
|
||||
local body
|
||||
local returncode
|
||||
local http_code
|
||||
local response
|
||||
local result
|
||||
|
||||
trace "[web] msg=${msg}"
|
||||
@@ -20,13 +20,14 @@ web() {
|
||||
# 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
|
||||
body=$(echo "${body}" | base64 -d)
|
||||
trace "[web] body=${body}"
|
||||
else
|
||||
body=
|
||||
trace "[web] no body, GET request"
|
||||
fi
|
||||
|
||||
http_code=$(curl_it "${url}" "${body}")
|
||||
response=$(curl_it "${url}" "${body}")
|
||||
returncode=$?
|
||||
trace_rc ${returncode}
|
||||
|
||||
@@ -38,7 +39,8 @@ web() {
|
||||
result="error"
|
||||
fi
|
||||
|
||||
echo "{\"result\":\"${result}\",\"http_code\":\"${http_code}\"}"
|
||||
echo "${response}"
|
||||
# echo "{\"result\":\"${result}\",\"http_code\":\"${http_code}\"}"
|
||||
|
||||
return ${returncode}
|
||||
}
|
||||
@@ -49,20 +51,29 @@ curl_it() {
|
||||
local url=$(echo "${1}" | tr -d '"')
|
||||
local data=${2}
|
||||
local returncode
|
||||
local response
|
||||
local rnd=$(dd if=/dev/urandom bs=5 count=1 | xxd -pc 5)
|
||||
|
||||
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})
|
||||
trace "[curl_it] curl -o webresponse-${rnd} -w \"%{http_code}\" -H \"Content-Type: application/json\" -H \"X-Forwarded-Proto: https\" -d ${data} ${url}"
|
||||
rc=$(curl -o webresponse-${rnd} -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})
|
||||
trace "[curl_it] curl -o webresponse-$$ -w \"%{http_code}\" ${url}"
|
||||
rc=$(curl -o webresponse-${rnd} -w "%{http_code}" ${url})
|
||||
returncode=$?
|
||||
fi
|
||||
trace "[curl_it] HTTP return code=${rc}"
|
||||
trace_rc ${returncode}
|
||||
|
||||
echo "${rc}"
|
||||
if [ "${returncode}" -eq "0" ]; then
|
||||
response=$(cat webresponse-${rnd} | base64 | tr -d '"' ; rm webresponse-${rnd})
|
||||
else
|
||||
response=
|
||||
fi
|
||||
response="{\"curl_code\":${returncode},\"http_code\":${rc},\"body\":\"${response}\"}"
|
||||
|
||||
echo "${response}"
|
||||
|
||||
if [ "${returncode}" -eq "0" ]; then
|
||||
if [ "${rc}" -lt "400" ]; then
|
||||
|
||||
Reference in New Issue
Block a user