Skip to content

Commit 0c8b011

Browse files
committed
add general support for ledger-specific operations
Requests made to `/fluree/ledger/<ledger-alias/<op>`, where op corresponds to the operation endpoint suffix (query, update, insert, upsert, transact) are rewritten and rerouted to the top-level `/fluree/<op>` route handlers. This saves us from having to manually handle all the work the middleware does while still allowing the `/<ledger-alias/<op>` route construction instead of the `/<op>/<ledger-alias>` form.
1 parent 652a659 commit 0c8b011

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

src/fluree/server/handler.clj

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,33 @@
208208
:coercion ^:replace history-coercer
209209
:handler #'ledger/history})
210210

211+
(def fallback-handler
212+
(let [swagger-ui-handler (swagger-ui/create-swagger-ui-handler
213+
{:path "/"
214+
:config {:validatorUrl nil
215+
:operationsSorter "alpha"}})
216+
default-handler (ring/create-default-handler)]
217+
(ring/routes swagger-ui-handler default-handler)))
218+
219+
(defn ledger-specific-handler
220+
"Reroutes a ledger-specific request to the appropriate endpoint, adding the
221+
`fluree-ledger` header so that the request is directed to the appropriate ledger."
222+
[{{ledger-path :ledger-path} :path-params
223+
:as req}]
224+
(let [op-delimiter-idx (str/last-index-of ledger-path "/")
225+
226+
;; deconstruct the path to obtain the ledger-alias and the endpoint
227+
alias (subs ledger-path 0 op-delimiter-idx)
228+
endpoint (str "/fluree" (subs ledger-path op-delimiter-idx))
229+
230+
;; construct a new request to the correct endpoint with the fluree-ledger header
231+
new-req (-> req
232+
(update :headers assoc "fluree-ledger" alias)
233+
(assoc :uri endpoint))
234+
;; create a new dynamic handler and re-route the new request.
235+
re-route (ring/ring-handler (:reitit.core/router req) fallback-handler)]
236+
(re-route new-req)))
237+
211238
(defn wrap-assoc-system
212239
[conn consensus watcher subscriptions handler]
213240
(fn [req]
@@ -567,7 +594,6 @@
567594
:post history-endpoint}]
568595

569596
["/ledger/{*ledger-path}" #'ledger-specific-handler]
570-
["/query/{*ledger-alias}" {:post ledger-query-endpoint}]
571597

572598
["/subscribe"
573599
{:get {:summary "Subscribe to ledger updates"
@@ -591,14 +617,6 @@
591617
:parameters {:body AliasRequestBody}
592618
:handler #'remote/published-ledger-addresses}}]]])
593619

594-
(def fallback-handler
595-
(let [swagger-ui-handler (swagger-ui/create-swagger-ui-handler
596-
{:path "/"
597-
:config {:validatorUrl nil
598-
:operationsSorter "alpha"}})
599-
default-handler (ring/create-default-handler)]
600-
(ring/routes swagger-ui-handler default-handler)))
601-
602620
(def swagger-routes
603621
["/swagger.json"
604622
{:get {:no-doc true

test/fluree/server/integration/sparql_test.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666

6767
(deftest sparql-federated-query
6868
(let [ledger-name "test-federated-query"
69-
service (str "http://localhost:" @test-system/api-port "/fluree/query/" ledger-name)
69+
service (str "http://localhost:" @test-system/api-port "/fluree/ledger/" ledger-name "/query")
7070

7171
txn-req {"ledger" ledger-name
7272
"@context" {"ex" "http://example.com/"}

0 commit comments

Comments
 (0)