Compare commits
2 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
75d33abe8a | ||
|
|
25667d883d |
5 changed files with 47 additions and 18 deletions
11
project.clj
11
project.clj
|
|
@ -15,4 +15,13 @@
|
|||
:profiles {:dev {:source-paths ["dev" "test/app"]
|
||||
:dependencies [[yesql "0.5.1"]
|
||||
[org.clojure/tools.nrepl "0.2.11"]
|
||||
[com.datomic/datomic-free "0.9.5327" :exclusions [joda-time]]]}})
|
||||
[org.clojure/tools.cli "0.3.3"]
|
||||
[com.datomic/datomic-free "0.9.5327" :exclusions [joda-time]]]}
|
||||
|
||||
;; "test" is in sources here to just "demo" the uberjar without poluting mount "src"
|
||||
:uberjar {:source-paths ["test/app"]
|
||||
:dependencies [[org.clojure/tools.cli "0.3.3"]
|
||||
[org.clojure/tools.nrepl "0.2.11"]
|
||||
[com.datomic/datomic-free "0.9.5327" :exclusions [joda-time]]]
|
||||
:main app
|
||||
:aot :all}})
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@
|
|||
|
||||
;; (defonce ^:private session-id (System/currentTimeMillis))
|
||||
(defonce ^:private mount-state 42)
|
||||
(defonce ^:private -args (atom :no-args)) ;; mostly for command line args and external files
|
||||
(defonce ^:private state-seq (atom 0))
|
||||
(defonce ^:private state-order (atom {}))
|
||||
|
||||
|
|
@ -57,6 +58,9 @@
|
|||
(throw (RuntimeException. (str "could not stop [" name "] due to") t)))))
|
||||
(alter-meta! var assoc :started? false)))
|
||||
|
||||
;;TODO args might need more thinking
|
||||
(defn args [] @-args)
|
||||
|
||||
(defn mount-state? [var]
|
||||
(= (-> var meta :mount-state)
|
||||
mount-state))
|
||||
|
|
@ -82,7 +86,7 @@
|
|||
doall))
|
||||
|
||||
(defn start [& states]
|
||||
(let [states (or states (find-all-states))]
|
||||
(let [states (or (seq states) (find-all-states))]
|
||||
(bring states up <)
|
||||
:started))
|
||||
|
||||
|
|
@ -90,3 +94,9 @@
|
|||
(let [states (or states (find-all-states))]
|
||||
(bring states down >)
|
||||
:stopped))
|
||||
|
||||
(defn start-with-args [xs & states]
|
||||
(reset! -args xs)
|
||||
(if (first states)
|
||||
(start states)
|
||||
(start)))
|
||||
|
|
|
|||
|
|
@ -1,10 +1,12 @@
|
|||
(ns app
|
||||
(:require [datomic.api :as d]
|
||||
[clojure.tools.nrepl.server :refer [start-server stop-server]]
|
||||
[clojure.tools.cli :refer [parse-opts]]
|
||||
[mount :refer [defstate]]
|
||||
[app.utils.datomic :refer [touch]]
|
||||
[app.config :refer [app-config]]
|
||||
[app.nyse :as nyse]))
|
||||
[app.nyse :as nyse])
|
||||
(:gen-class))
|
||||
|
||||
;; example on creating a network REPL
|
||||
(defn- start-nrepl [{:keys [host port]}]
|
||||
|
|
@ -60,6 +62,15 @@
|
|||
(defn create-nyse-schema []
|
||||
(create-schema nyse/conn))
|
||||
|
||||
;; example of an app entry point
|
||||
;; "any" regular function to pass arguments
|
||||
(defn parse-args [args]
|
||||
(let [opts [["-d" "--datomic-uri [datomic url]" "Datomic URL"
|
||||
:default "datomic:mem://mount"]
|
||||
["-h" "--help"]]]
|
||||
(-> (parse-opts args opts)
|
||||
:options)))
|
||||
|
||||
;; example of an app entry point with arguments
|
||||
(defn -main [& args]
|
||||
(mount/start))
|
||||
(mount/start-with-args
|
||||
(parse-args args)))
|
||||
|
|
|
|||
|
|
@ -5,6 +5,8 @@
|
|||
|
||||
(defn load-config [path]
|
||||
(info "loading config from" path)
|
||||
(if (:help (mount/args))
|
||||
(info "\n\nthis is a sample mount app to demo how to pass and read runtime arguments\n"))
|
||||
(-> path
|
||||
slurp
|
||||
edn/read-string))
|
||||
|
|
|
|||
|
|
@ -4,18 +4,15 @@
|
|||
[clojure.tools.logging :refer [info]]
|
||||
[app.config :refer [app-config]]))
|
||||
|
||||
(defn- new-connection [conf]
|
||||
(info "conf: " conf)
|
||||
(let [uri (get-in conf [:datomic :uri])]
|
||||
(info "creating a connection to datomic:" uri)
|
||||
(d/create-database uri)
|
||||
(d/connect uri)))
|
||||
(defn- new-connection [{:keys [datomic-uri]}]
|
||||
(info "creating a connection to datomic:" datomic-uri)
|
||||
(d/create-database datomic-uri)
|
||||
(d/connect datomic-uri))
|
||||
|
||||
(defn disconnect [conf conn]
|
||||
(let [uri (get-in conf [:datomic :uri])]
|
||||
(info "disconnecting from " uri)
|
||||
(.release conn) ;; usually it's not released, here just to illustrate the access to connection on (stop)
|
||||
(d/delete-database uri)))
|
||||
(defn disconnect [{:keys [datomic-uri]} conn]
|
||||
(info "disconnecting from " datomic-uri)
|
||||
(.release conn) ;; usually it's not released, here just to illustrate the access to connection on (stop)
|
||||
(d/delete-database datomic-uri))
|
||||
|
||||
(defstate conn :start (new-connection app-config)
|
||||
:stop (disconnect app-config conn))
|
||||
(defstate conn :start (new-connection (mount/args))
|
||||
:stop (disconnect (mount/args) conn))
|
||||
|
|
|
|||
Loading…
Reference in a new issue