Skip to content

Commit

Permalink
Move system-configuration stuff to a component (#33)
Browse files Browse the repository at this point in the history
* Move system-configuration stuff to a component

* Cleanup

* Handle garbage profiles"

* Remove tests since they aren't needed
  • Loading branch information
bryanmikaelian authored Aug 9, 2024
1 parent 8507562 commit 9de3d43
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 45 deletions.
47 changes: 5 additions & 42 deletions bases/agent/src/com/vennbilling/agent/core.clj
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
(ns com.vennbilling.agent.core
(:gen-class)
(:require
[aero.core :refer [read-config]]
[clojure.java.io :as io]
[com.vennbilling.customer.interface :as customer]
[com.vennbilling.healthcheck.interface :as healthcheck]
[com.vennbilling.spec.interface :as venn-spec]
[com.vennbilling.system.interface :as system]
[integrant.core :as ig]
[io.pedestal.log :as log]
[muuntaja.core :as m]
Expand All @@ -19,19 +19,6 @@
[ring.logger :as logger]))


(defmethod aero.core/reader 'ig/ref
[_ _ value]
(ig/ref value))


(defmethod aero.core/reader 'ig/refset
[_ _ value]
(ig/refset value))


(defmethod ig/init-key :system/env [_ env] env)


(defmethod ig/init-key :server/http
[_ {:keys [db] :as opts}]
(let [handler (atom (delay (:handler opts)))]
Expand Down Expand Up @@ -112,36 +99,12 @@
(ring/router ["" opts routes]))


(defonce system (atom nil))
(def ^:const banner (slurp (io/resource "agent/banner.txt")))


(defn- config
[opts]
(read-config (io/resource "agent/system.edn") opts))


(defn stop-app
[]
(log/info :msg "venn agent has shut down successfully")
(some-> (deref system) (ig/halt!)))


(defn start-app
[{:keys [profile] :as opts}]

(System/setProperty "org.jboss.logging.provider" "slf4j")

(println "\n" banner)
(log/info :msg "venn agent started successfully." :profile profile)

(->> (config opts)
(ig/prep)
(ig/init)
(reset! system))
(.addShutdownHook (Runtime/getRuntime) (Thread. stop-app)))
(def config-file (io/resource "agent/system.edn"))


(defn -main
[& _]
(start-app {:profile :prod}))
;; TODO: Pass profile as args
(system/start banner config-file {:profile :prod})
(log/info :msg "venn agent started successfully."))
4 changes: 4 additions & 0 deletions components/system/deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{:paths ["src" "resources"]
:deps {}
:aliases {:test {:extra-paths ["test"]
:extra-deps {}}}}
1 change: 1 addition & 0 deletions components/system/resources/system/.keep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

3 changes: 3 additions & 0 deletions components/system/resources/system/default.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{:system/env
#profile {:dev :dev
:prod :prod}}
22 changes: 22 additions & 0 deletions components/system/src/com/vennbilling/system/config.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(ns com.vennbilling.system.config
(:require
[aero.core :as aero]
[integrant.core :as ig]))


(defmethod aero.core/reader 'ig/ref
[_ _ value]
(ig/ref value))


(defmethod aero.core/reader 'ig/refset
[_ _ value]
(ig/refset value))


(defmethod ig/init-key :system/env [_ env] env)


(defn read-config
[config-file opts]
(aero/read-config config-file opts))
8 changes: 8 additions & 0 deletions components/system/src/com/vennbilling/system/interface.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(ns com.vennbilling.system.interface
(:require
[com.vennbilling.system.lifecycle :as lifecycle]))


(defn start
[banner config-file opts]
(lifecycle/start banner config-file opts))
40 changes: 40 additions & 0 deletions components/system/src/com/vennbilling/system/lifecycle.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
(ns com.vennbilling.system.lifecycle
(:require
[clojure.java.io :as io]
[com.vennbilling.system.config :as c]
[integrant.core :as ig]))


(defonce system (atom nil))

(def ^:private default-config-file (io/resource "system/default.edn"))
(def ^:private default-opts {:profile :dev})


(defn- valid-profile?
[profile]
(contains? {:prod true
:dev :true} profile))


(defn- stop
[]
(some-> (deref system) (ig/halt!)))


(defn start
[banner config-file opts]

(when banner
(println "\n" banner))

(let [f (or config-file default-config-file)
profile (:profile opts)
o (if (valid-profile? profile)
opts
default-opts)]
(->> (c/read-config f o)
(ig/prep)
(ig/init)
(reset! system)))
(.addShutdownHook (Runtime/getRuntime) (Thread. stop)))
1 change: 1 addition & 0 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
:dev {:extra-deps {poly/spec {:local/root "components/spec"}
poly/healthcheck {:local/root "components/healthcheck"}
poly/customer {:local/root "components/customer"}
poly/system {:local/root "components/system"}
poly/agent {:local/root "bases/agent"}

integrant/repl {:mvn/version "0.3.3"}
Expand Down
5 changes: 2 additions & 3 deletions development/src/dev/agent.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@
[clojure.java.io :as io]
[clojure.tools.namespace.repl :as repl]
[com.vennbilling.agent.core]
[com.vennbilling.system.interface]
[integrant.core :as ig]
[integrant.repl :refer [prep go halt reset init]]
[integrant.repl.state]
[io.pedestal.log :as log]))


(def config-file (io/resource "../../../bases/agent/resources/agent/system.edn"))
(def config (read-config config-file {:profile :dev}))

(integrant.repl/set-prep! #(ig/prep config))
(integrant.repl/set-prep! #(ig/prep (read-config config-file {:profile :dev})))
(repl/set-refresh-dirs "../../../bases/agent/src")

(System/setProperty "logback.statusListenerClass" "ch.qos.logback.core.status.OnConsoleStatusListener")


(log/debug :msg "agent running in development mode")


Expand Down
1 change: 1 addition & 0 deletions projects/agent/deps.edn
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{:deps {poly/spec {:local/root "../../components/spec"}
poly/customer {:local/root "../../components/customer"}
poly/healthcheck {:local/root "../../components/healthcheck"}
poly/system {:local/root "../../components/system"}

poly/agent {:local/root "../../bases/agent"}

Expand Down

0 comments on commit 9de3d43

Please sign in to comment.