From 5ad0b59666f9033ffb3d99b0aed9a25257d9c3f4 Mon Sep 17 00:00:00 2001 From: Luke Koziarski Date: Thu, 21 May 2026 00:38:31 +0200 Subject: [PATCH] feat: add structured GET /api/v1/connections response --- src/pine/api.clj | 8 +++++++- src/pine/db/connections.clj | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/pine/api.clj b/src/pine/api.clj index ee52fb4..7ed8218 100644 --- a/src/pine/api.clj +++ b/src/pine/api.clj @@ -119,6 +119,12 @@ {:connection-id "" :version version}}))) +(defn get-connections [] + {:result + {:version version + :selected-connection-id @db/connection-id + :connections (connections/list-connections)}}) + (defn test-connection [id] (let [result (db/run-query id {:query "SELECT NOW();"})] {:connection-id id :time result})) @@ -171,7 +177,7 @@ (defroutes app-routes ;; connection management (GET "/api/v1/connection" [] (-> (get-connection) response)) - (GET "/api/v1/connections" [] (-> @connections/pools response)) + (GET "/api/v1/connections" [] (-> (get-connections) response)) (POST "/api/v1/connections" req (let [connection (get-in req [:params])] (-> {:connection-id (connections/add-connection-pool connection)} response))) diff --git a/src/pine/db/connections.clj b/src/pine/db/connections.clj index e70d835..4241525 100644 --- a/src/pine/db/connections.clj +++ b/src/pine/db/connections.clj @@ -39,9 +39,24 @@ (defn make-connection-id [pool] (-> pool .getJdbcUrl (s/split #"/") (nth 2))) +(defn jdbc-url->label [url] + (let [parts (s/split url #"/") + host-port (nth parts 2) + dbname (nth parts 3)] + (str host-port " ยท " dbname))) + +(defn make-connection-label [pool] + (jdbc-url->label (.getJdbcUrl pool))) + (defn get-connection-name [id] (-> id get-connection-pool make-connection-id)) +(defn list-connections [] + (mapv (fn [[id _]] + {:id id + :label (make-connection-label (get-connection-pool id))}) + @pools)) + (defn add-connection-pool [connection] (let [pool (create-pool connection) id (make-connection-id pool)]