46 lines
1.2 KiB
Bash
46 lines
1.2 KiB
Bash
|
|
#!/usr/bin/env bash
|
||
|
|
|
||
|
|
# Setup timestamp function
|
||
|
|
timestamp() {
|
||
|
|
date "+%Y-%m-%d %H:%M:%S"
|
||
|
|
}
|
||
|
|
|
||
|
|
# Setup process cleanup
|
||
|
|
cleanup() {
|
||
|
|
# Kill all child processes of this script
|
||
|
|
pkill -P $$
|
||
|
|
exit $1
|
||
|
|
}
|
||
|
|
|
||
|
|
# Function to handle API requests and save response
|
||
|
|
handle_request() {
|
||
|
|
local request_type=$1
|
||
|
|
local api_endpoint=$2
|
||
|
|
local path=$3
|
||
|
|
local fileName=$4
|
||
|
|
|
||
|
|
echo "$(timestamp) [REQUEST] Getting $request_type" >> "$LOG_FILE"
|
||
|
|
response=$(curl -s -w "%{http_code}" -H "Accept: $EVO_API_ACCEPT" \
|
||
|
|
-H "Content-Type: $EVO_API_CONTENT_TYPE" \
|
||
|
|
-H "Authorization: $EVO_API_TOKEN" \
|
||
|
|
-X GET \
|
||
|
|
$api_endpoint)
|
||
|
|
http_code=${response: -3}
|
||
|
|
response_body=${response:0:-3}
|
||
|
|
if [ "$http_code" = "200" ]; then
|
||
|
|
touch "$path/$fileName"
|
||
|
|
echo "$response_body" > "$path/$fileName"
|
||
|
|
fi
|
||
|
|
echo "$(timestamp) [RESPONSE] code=$http_code" >> "$LOG_FILE"
|
||
|
|
|
||
|
|
# Delete old files (files older than configured period)
|
||
|
|
find $path ! -type d -mmin +$EVO_STORE_PERIOD_MINUTES -delete
|
||
|
|
}
|
||
|
|
|
||
|
|
# Setup logging function
|
||
|
|
setup_logging() {
|
||
|
|
LOG_DIR="$ROOT_DIR/logs"
|
||
|
|
mkdir -p "$LOG_DIR"
|
||
|
|
LOG_FILE="$LOG_DIR/$(date +%Y%m%d).log"
|
||
|
|
}
|