Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split aero and integrant into two separate parts. #34

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 9 additions & 19 deletions bases/agent/resources/agent/system.edn
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
{:system/env
#profile {:dev :dev
:prod :prod}
{:system/env #profile {:dev :dev
:prod :prod}

:server/http
{:port #long #or [#env AGENT_PORT 8080]
:host #or [#env AGENT_HOST "0.0.0.0"]
:handler #ig/ref :handler/ring}
:http/server
{:port #long #or [#env VENN_AGENT_PORT 8080]
:host #or [#env VENN_AGENT_HOST "0.0.0.0"]
:handler #ref :handler/ring}

:handler/ring
{:router #ig/ref :router/core}
{:router #ref :router/ring}

:agent.routes/internal
{:base-path "/internal"}

:agent.routes/api
{:base-path "/v1"}

:router/routes
{:routes #ig/refset :agent/routes}

:router/core
{:routes #ig/ref :router/routes}}
:router/ring
{:base-path "/v1"}}
119 changes: 55 additions & 64 deletions bases/agent/src/com/vennbilling/agent/core.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns com.vennbilling.agent.core
(:gen-class)
(:require
[aero.core :as aero]
[clojure.java.io :as io]
[com.vennbilling.customer.interface :as customer]
[com.vennbilling.healthcheck.interface :as healthcheck]
Expand All @@ -19,92 +20,82 @@
[ring.logger :as logger]))


(defmethod ig/init-key :server/http
[_ {:keys [db] :as opts}]
(let [handler (atom (delay (:handler opts)))]
{:handler handler}
{:server (run-undertow (fn [req] (@@handler (assoc req :db db))) (dissoc opts :handler))}))


(defmethod ig/halt-key! :server/http
[_ {:keys [server]}]
(.stop server))


(defmethod ig/init-key :handler/ring
[_ {:keys [router]}]
(logger/wrap-with-logger
(ring/ring-handler
router
(ring/create-default-handler))))


(defn internal-routes
[_opts]
[healthcheck/simple-route])


(defn api-routes
[_opts]
(def api-routes
[venn-spec/identify-route
customer/list-route
customer/show-route])


(defn route-data
[opts]
(merge
opts
{:coercion malli/coercion
:muuntaja m/instance
:middleware [;; query-params & form-params
parameters/parameters-middleware
;; content-negotiation
muuntaja/format-negotiate-middleware
;; encoding response body
muuntaja/format-response-middleware
;; exception handling
exception/exception-middleware
;; decoding request body
muuntaja/format-request-middleware
;; coercing response bodys
coercion/coerce-response-middleware
;; coercing request parameters
coercion/coerce-request-middleware]}))


(derive :agent.routes/internal :agent/routes)
(derive :agent.routes/api :agent/routes)

(def router-config
{:coercion malli/coercion
:muuntaja m/instance
:middleware [;; query-params & form-params
parameters/parameters-middleware
;; content-negotiation
muuntaja/format-negotiate-middleware
;; encoding response body
muuntaja/format-response-middleware
;; exception handling
exception/exception-middleware
;; decoding request body
muuntaja/format-request-middleware
;; coercing response bodys
coercion/coerce-response-middleware
;; coercing request parameters
coercion/coerce-request-middleware]})


;; TODO Move this to system component
(defmethod ig/init-key :http/server
[_ opts]
(let [handler (atom (delay (:handler opts)))
undertow-opts (dissoc opts :handler)]
{:server (run-undertow (fn [req] (@@handler req)) undertow-opts)}))


;; TODO Move this to system component
(defmethod ig/halt-key! :http/server
[_ {:keys [server]}]
(.stop server))

(defmethod ig/init-key :agent.routes/api
[_ {:keys [base-path]
:as opts}]
[base-path (dissoc (route-data opts) :base-path) (api-routes opts)])

;; TODO Move this to system component
(defmethod ig/init-key :handler/ring
[_ {:keys [router]}]
(logger/wrap-with-logger
(ring/ring-handler
router
(ring/create-default-handler))))

(defmethod ig/init-key :agent.routes/internal
[_ {:keys [base-path]
:as opts}]
[base-path (dissoc (route-data opts) :base-path) (internal-routes opts)])

;; TODO Move this to system component
(defmethod ig/init-key :router/ring
[_ {:keys [base-path]}]
(ring/router [base-path router-config api-routes]))

(defmethod ig/init-key :router/routes
[_ {:keys [routes]}]
(apply conj [] routes))

(def ^:const banner (slurp (io/resource "agent/banner.txt")))
(def config-file (io/resource "agent/system.edn"))

(defmethod ig/init-key :router/core
[_ {:keys [routes] :as opts}]
(ring/router ["" opts routes]))

;; TODO Move this to system component
(def settings
{:http/server
{:handler (ig/ref :handler/ring)}

(def ^:const banner (slurp (io/resource "agent/banner.txt")))
(def config-file (io/resource "agent/system.edn"))
:handler/ring
{:router (ig/ref :router/ring)}})


(defn -main
[& _]
;; TODO: Pass profile as args
(system/start banner config-file {:profile :prod})
(let [config (aero/read-config config-file {:profile :prod})
agent (merge-with into config settings)]
(system/start banner agent))
(log/info :msg "venn agent started successfully."))
4 changes: 1 addition & 3 deletions components/system/resources/system/default.edn
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
{:system/env
#profile {:dev :dev
:prod :prod}}
{:system/env :dev}
22 changes: 0 additions & 22 deletions components/system/src/com/vennbilling/system/config.clj

This file was deleted.

5 changes: 3 additions & 2 deletions components/system/src/com/vennbilling/system/interface.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
(ns com.vennbilling.system.interface
(:require
[com.vennbilling.system.core :as core]
bryanmikaelian marked this conversation as resolved.
Show resolved Hide resolved
[com.vennbilling.system.lifecycle :as lifecycle]))


(defn start
[banner config-file opts]
(lifecycle/start banner config-file opts))
([banner settings]
(lifecycle/start banner settings)))
28 changes: 6 additions & 22 deletions components/system/src/com/vennbilling/system/lifecycle.clj
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
(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))
(defmethod ig/init-key :system/env, [_ env] env)


(defn- stop
Expand All @@ -23,18 +14,11 @@


(defn start
[banner config-file opts]

[banner settings]
(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)))
(->> settings
(ig/prep)
(ig/init)
(reset! system))
(.addShutdownHook (Runtime/getRuntime) (Thread. stop)))
7 changes: 3 additions & 4 deletions development/src/dev/agent.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
[aero.core :refer [read-config]]
[clojure.java.io :as io]
[clojure.tools.namespace.repl :as repl]
[com.vennbilling.agent.core]
[com.vennbilling.system.interface]
[com.vennbilling.agent.core :refer [settings]]
[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-file (read-config (io/resource "../../../bases/agent/resources/agent/system.edn") {:profile :dev}))

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

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