From db4df1e0851cd27a24f05e63d09906266ba84a5f Mon Sep 17 00:00:00 2001 From: Marco Dalla Stella Date: Sun, 28 Sep 2025 10:31:10 +0200 Subject: [PATCH] Add run script and improve error handling - Add run.sh script to automate IP list update and nginx reload - Improve core.clj error handling with try/catch block - Remove redundant string import in core.clj - Add logging with timestamps to run script - Exit with proper status codes in both success and failure cases --- run.sh | 20 ++++++++++++++++++++ src/bunny2realip/core.clj | 15 +++++++++------ 2 files changed, 29 insertions(+), 6 deletions(-) create mode 100755 run.sh diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..9cd80c7 --- /dev/null +++ b/run.sh @@ -0,0 +1,20 @@ +#!/bin/bash +# Function to log messages with a timestamp +log_message() { + echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" +} + +log_message("Update Bunny IPs list...") +bb -m bunny2realip.core + +if [ $? -eq 0]; then + log_message("Reload nginx configuration...") + systemctl reload nginx + if [ $? -eq 0]; then + log_message("Update successfully!") + else + log_message("nginx: Something went wrong!") + fi +else + log_message("bunny2realip: Something went wrong!") +fi diff --git a/src/bunny2realip/core.clj b/src/bunny2realip/core.clj index d69155d..cef4528 100644 --- a/src/bunny2realip/core.clj +++ b/src/bunny2realip/core.clj @@ -1,8 +1,7 @@ (ns bunny2realip.core (:require [babashka.http-client :as http] [cheshire.core :as json] - [clojure.string :as s] - [clojure.string :as str])) + [clojure.string :as s])) (defonce ip-lists ["https://bunnycdn.com/api/system/edgeserverlist/" "https://bunnycdn.com/api/system/cdnserverlist/"]) @@ -22,7 +21,11 @@ (defn -main [& args] (let [ips (set (mapcat get-list ip-lists)) realips (map format-ips (sort ips))] - (as-> realips $ - (concat $ ["real_ip_header X-Real-IP;" "real_ip_recursive on;"]) - (str/join "\n" $) - (spit realip-dest $)))) + (try + (as-> realips $ + (concat $ ["real_ip_header X-Real-IP;" "real_ip_recursive on;"]) + (s/join "\n" $) + (spit realip-dest $)) + (System/exit 0) + (catch Exception _ + (System/exit -1)))))