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
This commit is contained in:
MDS
2025-09-28 10:31:10 +02:00
parent 0727d0169e
commit db4df1e085
2 changed files with 29 additions and 6 deletions

20
run.sh Executable file
View File

@@ -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

View File

@@ -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)))))