-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.clj
38 lines (32 loc) · 1.23 KB
/
app.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
(ns freecoin-lib.app
(:require [taoensso.timbre :as log]
[freecoin-lib.core :refer [new-mongo]]
[freecoin-lib.schemas :refer [Config]]
[freecoin-lib.config :as config]
[clj-storage.db.mongo :as mongo]
[freecoin-lib.db.freecoin :as db]
[schema.core :as s]))
;; launching and halting the app
(defonce ^:private app-state (atom {}))
(defn- mongo-conf-to-uri [conf]
(if-let [m conf]
(str "mongodb://" (:host m) ":" (:port m) "/" (:db m))))
(s/defn connect-mongo
"Connect a mongo database for metadata (required)" [conf :- Config]
(-> (:mongo conf)
mongo-conf-to-uri
mongo/get-mongo-db
db/create-freecoin-stores))
;; TODO: else return error
(s/defn start [config :- Config]
(if-let [conf config]
(let [db (-> conf mongo-conf-to-uri config/mongo-uri mongo/get-mongo-db)
stores (db/create-freecoin-stores db) ;; here ttl optional arg
backend (new-mongo stores)]
;; return the base context
{:db db
:config (:freecoin config)
:backend backend})))
;; TODO: handle errors consistently
(defn disconnect-mongo [ctx]
(if-let [db (:wallet-store ctx)] (mongo/disconnect db)))