Skip to content

Commit

Permalink
Handle missing endpoint or service gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
julienvincent committed Dec 5, 2024
1 parent 4c99e57 commit 0815a05
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
27 changes: 17 additions & 10 deletions src/k16/kl/api/proxy.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns k16.kl.api.proxy
(:require
[k16.kl.log :as log]
[clj-yaml.core :as yaml]
[k16.kl.api.fs :as api.fs]))

Expand All @@ -19,18 +20,24 @@

endpoint-name (or (:endpoint route)
(:default-endpoint service))
endpoint (get-in service [:endpoints endpoint-name])

traefik-service-name (str (name service-name) "-" (name endpoint-name))]
endpoint (get-in service [:endpoints endpoint-name])]

(if (and service endpoint)
(-> acc
(assoc-in [:http :routers (name route-name)]
{:rule (route->traefik-rule route)
:service traefik-service-name})
(assoc-in [:http :services traefik-service-name :loadbalancer :servers]
[{:url (:url endpoint)}]))
acc)))
(let [traefik-service-name (str (name service-name)
"-"
(name endpoint-name))]
(-> acc
(assoc-in [:http :routers (name route-name)]
{:rule (route->traefik-rule route)
:service traefik-service-name})
(assoc-in [:http :services traefik-service-name :loadbalancer :servers]
[{:url (:url endpoint)}])))
(do (when-not service
(log/warn (str "Unknown service " service-name)))
(when (and endpoint-name
(not endpoint))
(log/warn (str "Unknown endpoint " endpoint-name)))
acc))))
{})))

(defn write-proxy-config! [{:keys [module-name module]}]
Expand Down
8 changes: 6 additions & 2 deletions src/k16/kl/log.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns k16.kl.log
(:require
[jansi-clj.core :as color]))
(:require
[jansi-clj.core :as color]))

(def ^:private lock (Object.))

Expand All @@ -12,6 +12,10 @@
(locking lock
(println (color/render (str "@|red " (color/render msg) "|@")))))

(defn warn [msg]
(locking lock
(println (color/render (str "@|yellow " (color/render msg) "|@")))))

(defn debug [msg]
(locking lock
(println (color/render (str "@|white " (color/render msg) "|@")))))

0 comments on commit 0815a05

Please sign in to comment.