Skip to content

Commit

Permalink
working reitit router and ring handler
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolap committed May 20, 2023
1 parent 7f8bb0d commit 2c06804
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 32 deletions.
8 changes: 5 additions & 3 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{:paths ["src" "resources"]
:deps {org.clojure/clojure {:mvn/version "1.11.1"}
party.donut/dbxray {:mvn/version "0.0.70"}
io.github.nikolap/dbxray {:git/sha "537cd94ffc451208f64a012b00c1244debae5607"} ;; TODO: update if fork merged
org.clojure/tools.logging {:mvn/version "1.2.4"}
metosin/ring-http-response {:mvn/version "0.9.3"}
com.github.seancorfield/next.jdbc {:mvn/version "1.3.874"}
Expand All @@ -10,8 +10,10 @@
:ns-default build}
:dev {:extra-paths ["test"]
:extra-deps {lambdaisland/kaocha {:mvn/version "1.75.1190"}
migratus/migratus {:mvn/version "1.4.9"}}}
migratus/migratus {:mvn/version "1.4.9"}
metosin/reitit {:mvn/version "0.6.0"}}}
:test {:extra-paths ["test"]
:extra-deps {lambdaisland/kaocha {:mvn/version "1.75.1190"}
migratus/migratus {:mvn/version "1.4.9"}}
migratus/migratus {:mvn/version "1.4.9"}
metosin/reitit {:mvn/version "0.6.0"}}
:main-opts ["-m" "kaocha.runner"]}}}
63 changes: 39 additions & 24 deletions src/route_craft/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -147,30 +147,29 @@
;; TODO: handle failed mapping finds more gracefully
(defmethod generate-method-handler :get
[{:keys [table id-key] :as opts} _]
[(str "/" id-key)
{:get (partial get-by-id-handler opts)
:parameters {:path [:map [id-key (id-type opts)]]}
:responses {200 {:body (table->malli-map table)}}}])
{:handler (partial get-by-id-handler opts)
:parameters {:path [:map [id-key (id-type opts)]]}
:responses {200 {:body (table->malli-map table)}}})

(defmethod generate-method-handler :post
[{:keys [table] :as opts} _]
["" {:post (partial create-handler opts)
:parameters {:body (table->malli-map table true)}
:responses {200 {:body (table->malli-map table)}}}])
{:handler (partial create-handler opts)
:parameters {:body (table->malli-map table true)}
:responses {200 {:body (table->malli-map table)}}})

(defmethod generate-method-handler :put
[{:keys [table id-key] :as opts} _]
["/:id" {:put (partial update-by-id-handler opts)
:parameters {:path [:map [id-key (id-type opts)]]
:body (table->malli-map table true {:force-optional? true})}
:responses {200 {:body (table->malli-map table)}}}])
{:handler (partial update-by-id-handler opts)
:parameters {:path [:map [id-key (id-type opts)]]
:body (table->malli-map table true {:force-optional? true})}
:responses {200 {:body (table->malli-map table)}}})

(defmethod generate-method-handler :delete
[{:keys [id-key] :as opts} _]
(let [malli-id-def [id-key (id-type opts)]]
["/:id" {:delete (partial delete-by-id-handler opts)
:parameters {:path [:map malli-id-def]}
:responses {200 {:body [:map malli-id-def]}}}]))
{:handler (partial delete-by-id-handler opts)
:parameters {:path [:map malli-id-def]}
:responses {200 {:body [:map malli-id-def]}}}))

(defmethod generate-method-handler :default
[_ method]
Expand All @@ -179,16 +178,28 @@

(defn routes-from-dbxray
[{:keys [table-definitions]} {:keys [table-order tables]}]
(sequence
(into []
(comp (map
(fn [table]
(let [table-opts (get table-definitions table)]
(when-not (:ignore? table-opts)
[(str "/" (name table))
(mapv (partial generate-method-handler {:table-name table
:table (get tables table)
:id-key (or (:id-key table-opts) default-id-key)})
(get-in table-definitions [table :methods] default-methods))]))))
(try (let [id-key (or (:id-key table-opts) default-id-key)
generate-method-fn (partial generate-method-handler {:table-name table
:table (get tables table)
:id-key id-key})]
(->> (reduce
(fn [out method]
(case method
:post (assoc-in out ["" method] (generate-method-fn method))
(assoc-in out [(str "/" id-key) method] (generate-method-fn method))))
{}
(get-in table-definitions [table :methods] default-methods))
(vec)
(cons (str "/" (name table)))
(vec)))
(catch Exception e
(log/trace e "Route generation exception")
(log/warn "Failed to generate routes for table" table ". Skipping.")))))))
(filter identity))
table-order))

Expand All @@ -207,7 +218,7 @@
;; handling defaults

;; table-definitions map
;; if a table is not specified, it is assumed that all CRUD is permitted (? maybe dangerous)
;; if a table is not specified, it is assumed that all CRUD is permitted (? maybe dangerous) TODO: change to opt in
{:flyway_schema_history {:ignore? true}
:locales {:methods [:get]}
}
Expand All @@ -219,8 +230,8 @@
]
:as opts}]
(try
(let [db-xray (dbx/xray (jdbc/get-connection db-conn))]
)
(let [db-xray (dbx/xray db-conn)]
(routes-from-dbxray opts db-xray))
(catch Exception e
(log/error e "Failed to create reitit routes"))))

Expand All @@ -241,4 +252,8 @@
(let [migratus-config {:migration-dir "test/resources/migrations"
:db ctx
:store :database}]
(migratus/migrate migratus-config)))
(migratus/migrate migratus-config))

(ring/router
(generate-reitit-crud-routes
{:db-conn (jdbc/get-connection (:datasource ctx))})))
19 changes: 14 additions & 5 deletions test/route_craft/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
(:require
[clojure.test :refer :all]
[migratus.core :as migratus]
[next.jdbc :as jdbc]))
[next.jdbc :as jdbc]
[reitit.core :as reitit]
[reitit.ring :as ring]
[route-craft.core :as route-craft]))

(defn ctx
[]
Expand All @@ -17,7 +20,13 @@
(f)
(migratus/rollback migratus-config)))

(deftest breaking-test
(testing "test gha"
(ctx)
(is (= 1 2))))
(use-fixtures :once test-fixture)

(deftest reitit-ring-integration-test
(testing "reitit ring handler generation integration test"
(let [router (ring/router
(route-craft/generate-reitit-crud-routes
{:db-conn (jdbc/get-connection (:datasource (ctx)))}))]
(is (= true (reitit/router? router)))
(is (= "/attachments/:id" (:template (reitit/match-by-path router "/attachments/1"))))
(is (fn? (ring/ring-handler router))))))

0 comments on commit 2c06804

Please sign in to comment.