Files
marco db4df1e085 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
2025-09-28 10:31:10 +02:00

32 lines
831 B
Clojure

(ns bunny2realip.core
(:require [babashka.http-client :as http]
[cheshire.core :as json]
[clojure.string :as s]))
(defonce ip-lists ["https://bunnycdn.com/api/system/edgeserverlist/"
"https://bunnycdn.com/api/system/cdnserverlist/"])
(defonce realip-dest "/etc/nginx/conf.d/bunny.conf")
(defn get-list
[url]
(-> url
http/get
:body
json/parse-string))
(defn format-ips
[ip]
(str "set_real_ip_from " ip ";"))
(defn -main [& args]
(let [ips (set (mapcat get-list ip-lists))
realips (map format-ips (sort ips))]
(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)))))