Skip to content

Commit

Permalink
refactor: change every component usage to new one
Browse files Browse the repository at this point in the history
  • Loading branch information
J0sueTM committed Jun 3, 2024
1 parent 2cb2d4d commit 5a905bc
Show file tree
Hide file tree
Showing 41 changed files with 535 additions and 752 deletions.
2 changes: 1 addition & 1 deletion src/back/api/controllers/mocks.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[back.api.db.mocks :as db.mocks]
[back.api.logic.mocks :as logic.mocks]
[back.api.ports.producers :as ports.producers]
[components.logs :as logs]))
[com.moclojer.components.logs :as logs]))

(defn create-mock!
[user-id mock {:keys [database publisher]}]
Expand Down
7 changes: 3 additions & 4 deletions src/back/api/db/customers.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
(ns back.api.db.customers
(:require
[components.database :as database]
[honey.sql :as sql]
[honey.sql.helpers :as sql.helpers]))
(:require [com.moclojer.components.database :as database]
[honey.sql :as sql]
[honey.sql.helpers :as sql.helpers]))

(defn insert!
[transaction db]
Expand Down
2 changes: 1 addition & 1 deletion src/back/api/db/mocks.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns back.api.db.mocks
(:require [components.database :as database]
(:require [com.moclojer.components.database :as database]
[honey.sql :as sql]
[honey.sql.helpers :as sql.helpers]))

Expand Down
3 changes: 1 addition & 2 deletions src/back/api/healthcheck.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
(ns back.api.healthcheck
(:require
[components.database :as database]))
(:require [com.moclojer.components.database :as database]))

(def Content
[:map
Expand Down
18 changes: 9 additions & 9 deletions src/back/api/ports/producers.clj
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
(ns back.api.ports.producers
(:require [components.redis-publisher :as redis-publisher]))
(:require [com.moclojer.components.publisher :as publisher]))

(defn publish-mock-event [mock queue-name publisher]
(redis-publisher/publish! publisher queue-name {:event mock}))
(publisher/publish! publisher queue-name {:event mock}))

(defn publish-mock-changed-event [mock-id publisher]
(redis-publisher/publish! publisher "mock.changed" {:event {:mock-id mock-id}}))
(publisher/publish! publisher "mock.changed" {:event {:mock-id mock-id}}))

(defn verify-domain!
[domain retrying? skip-data? publisher]
(redis-publisher/publish! publisher "domain.verify"
{:event {:domain domain
:retrying? retrying?
:skip-data? skip-data?}}))
(publisher/publish! publisher "domain.verify"
{:event {:domain domain
:retrying? retrying?
:skip-data? skip-data?}}))

(defn verify-unified!
[mocks publisher]
(redis-publisher/publish! publisher "unified.verify"
{:event {:mocks mocks}}))
(publisher/publish! publisher "unified.verify"
{:event {:mocks mocks}}))
35 changes: 14 additions & 21 deletions src/back/api/server.clj
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
(ns back.api.server
(:require [back.api.ports.workers :as p.workers]
[back.api.routes :as routes]
[com.stuartsierra.component :as component]
[components.config :as config]
[components.database :as database]
[components.http :as http]
[components.logs :as logs]
[components.migrations :as migrations]
[components.redis-publisher :as redis-publisher]
[components.redis-queue :as redis-queue]
[components.router :as router]
[components.sentry :as sentry]
[components.webserver :as webserver])
[com.moclojer.components.core :as components]
[com.moclojer.components.logs :as logs]
[com.moclojer.components.migrations :as migrations]
[com.stuartsierra.component :as component])
(:gen-class))

(def system-atom (atom nil))

(defn build-system-map []
(component/system-map
:config (config/new-config)
:http (http/new-http)
:sentry (component/using (sentry/new-sentry) [:config])
:router (router/new-router routes/routes)
:database (component/using (database/new-database) [:config])
:publisher (component/using (redis-publisher/new-redis-publisher
:config (components/new-config "back/config.edn")
:http (components/new-http)
:sentry (component/using (components/new-sentry) [:config])
:router (components/new-router routes/routes)
:database (component/using (components/new-database) [:config])
:publisher (component/using (components/new-publisher
[{:qname "domains.verification.dispatch"
:event {}
;; every 2 minutes
Expand All @@ -33,15 +26,15 @@
;; every 5 minutes
:delay 300000}])
[:config :sentry])
:webserver (component/using (webserver/new-webserver)
:webserver (component/using (components/new-webserver)
[:config :http :router :database :publisher :sentry])
:workers (component/using
(redis-queue/new-redis-queue p.workers/workers false)
(components/new-consumer p.workers/workers false)
[:config :database :publisher :sentry])))

(defn start-system! [system-map]
(logs/setup [["*" :info]] :auto :prod)
(migrations/migrate (migrations/configuration-with-db))
(components/setup-logger [["*" :info]] :auto :prod)
(migrations/migrate (migrations/build-complete-db-config "back/config.edn"))
(->> system-map
component/start
(reset! system-atom)))
Expand Down
3 changes: 2 additions & 1 deletion src/cloud_ops/api/controllers/cloud.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
[cloud-ops.api.logic.cloud :as logic.cloud]
[cloud-ops.api.ports.http-out :as http-out]
[cloud-ops.api.ports.producers :as producers]
[components.logs :as logs]))
[com.moclojer.components.logs :as logs]))

(defn get-current-data
"Retrieves current data from both CloudFlare and DigitalOcean.
The naming is agnostic to records and entire specs."
Expand Down
16 changes: 8 additions & 8 deletions src/cloud_ops/api/ports/http_out.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns cloud-ops.api.ports.http-out
(:require [components.http :as hp]
[components.logs :as logs]
(:require [com.moclojer.components.http :as http]
[com.moclojer.components.logs :as logs]
[muuntaja.core :as m]))

;; Since most of the requests here are a critical point of failure,
Expand All @@ -12,8 +12,8 @@
[domain http]
(try
(let [url (str "https://" domain ".moclojer.com")
resp (hp/request http {:method :get
:url url})]
resp (http/request http {:method :get
:url url})]
(:status resp))
(catch Exception e
(logs/log :warn "verifying domain"
Expand All @@ -32,7 +32,7 @@
"Retrieves the current domain records in CloudFlare."
[http req-params]
(let [req (assoc req-params :method :get)
{:keys [body]} (hp/request-or-throw http req 200)
{:keys [body]} (http/request-or-throw http req 200)
{:keys [result]} (m/decode "application/json" body)]
(logs/log :info "retrieved cloudflare records")
result))
Expand All @@ -48,7 +48,7 @@
:ttl 1})
req (merge req-params {:method :post
:body enc-body})
{:keys [body]} (hp/request-or-throw http req 200)
{:keys [body]} (http/request-or-throw http req 200)
decoded (m/decode "application/json" body)]
(logs/log :info "created cloudflare domain"
:ctx {:domain domain})
Expand All @@ -60,7 +60,7 @@
the app. We actually don't need all of that, so we clean it afterwards."
[http req-params]
(let [req (assoc req-params :method :get)
{:keys [body]} (hp/request-or-throw http req 200)
{:keys [body]} (http/request-or-throw http req 200)
decoded (m/decode "application/json" body)]
(logs/log :info "retrieved digital ocean spec")
(get-in decoded [:app :spec])))
Expand All @@ -76,7 +76,7 @@
(let [enc-spec (m/encode "application/json" {:spec new-spec})
req (merge req-params {:method :put
:body enc-spec})
{:keys [body]} (hp/request-or-throw http req 200)
{:keys [body]} (http/request-or-throw http req 200)
decoded (m/decode "application/json" body)]
(logs/log :info "updated digital ocean spec")
decoded))
22 changes: 11 additions & 11 deletions src/cloud_ops/api/ports/producers.clj
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
(ns cloud-ops.api.ports.producers
(:require [components.redis-publisher :as redis-publisher]))
(:require [com.moclojer.components.publisher :as publisher]))

(defn create-domain!
[domain retrying? publisher]
(redis-publisher/publish! publisher "domain.create"
{:event {:domain domain
:retrying? retrying?}}))
(publisher/publish! publisher "domain.create"
{:event {:domain domain
:retrying? retrying?}}))

(defn verify-domain!
[domain retrying? skip-data? publisher]
(redis-publisher/publish! publisher "domain.verify"
{:event {:domain domain
:retrying? retrying?
:skip-data? skip-data?}}))
(publisher/publish! publisher "domain.verify"
{:event {:domain domain
:retrying? retrying?
:skip-data? skip-data?}}))

(defn set-publication-status!
[domain new-status publisher]
(redis-publisher/publish! publisher "mock.publication"
{:event {:domain domain
:new-status new-status}}))
(publisher/publish! publisher "mock.publication"
{:event {:domain domain
:new-status new-status}}))
2 changes: 1 addition & 1 deletion src/cloud_ops/api/ports/workers.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns cloud-ops.api.ports.workers
(:require [cloud-ops.api.controllers.cloud :as controllers.cloud]
[components.logs :as logs]))
[com.moclojer.components.logs :as logs]))

(defn create-domain-handler
[{:keys [event]} components]
Expand Down
29 changes: 13 additions & 16 deletions src/cloud_ops/api/server.clj
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
(ns cloud-ops.api.server
(:require [cloud-ops.api.ports.workers :as p.workers]
[com.moclojer.components.core :as components]
[com.moclojer.components.logs :as logs]
[com.stuartsierra.component :as component]
[components.config :as config]
[components.http :as http]
[components.logs :as logs]
[components.redis-publisher :as redis-publisher]
[components.redis-queue :as redis-queue]
[components.sentry :as sentry])
[components.publisher :as publisher])
(:gen-class))

(def system-atom (atom nil))

(defn build-system-map []
(component/system-map
:config (config/new-config)
:http (http/new-http)
:sentry (component/using (sentry/new-sentry) [:config])
;; :sentry (sentry/new-mock-sentry)
:config (components/new-config "back/config.edn")
:http (components/new-http)
:sentry (component/using (components/new-sentry) [:config])
;; :sentry (components/new-sentry-mock)
:publisher (component/using
(redis-publisher/new-redis-publisher)
(components/new-publisher)
[:config :sentry])
:workers (component/using
(redis-queue/new-redis-queue p.workers/workers true)
(components/new-consumer p.workers/workers true)
[:config :http :publisher :sentry])))

(defn start-system! [system-map]
(logs/setup [["*" :info]] :auto :prod)
(components/setup-logger [["*" :info]] :auto :prod)
(->> system-map
component/start
(reset! system-atom)))
Expand All @@ -47,9 +44,9 @@
(comment
(start-system! (build-system-map))

(redis-publisher/publish! (:publisher @system-atom) "domain.create"
{:event {:domain "nao-existe-123-j0suetm"
:retrying? false}})
(publisher/publish! (:publisher @system-atom) "domain.create"
{:event {:domain "nao-existe-123-j0suetm"
:retrying? false}})

(stop-system!)
;;
Expand Down
37 changes: 15 additions & 22 deletions src/dev/api/dev.clj
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
(ns dev.api.dev
(:require [back.api.ports.workers :as p.workers]
[back.api.routes :as routes]
[com.moclojer.components.core :as components]
[com.moclojer.components.publisher :as publisher]
[com.stuartsierra.component :as component]
[components.config :as config]
[components.database :as database]
[components.http :as http]
[components.redis-publisher :as redis-publisher]
[components.redis-queue :as redis-queue]
[components.router :as router]
[components.sentry :as sentry]
[components.webserver :as webserver]
[dev.utils :as utils])
(:gen-class))

Expand All @@ -19,12 +13,12 @@

(defn build-system-map []
(component/system-map
:config (config/new-config)
:sentry (sentry/new-mock-sentry)
:http (http/new-http)
:router (router/new-router routes/routes)
:database (component/using (database/new-database) [:config])
:publisher (component/using (redis-publisher/new-redis-publisher
:config (components/new-config "back/config.edn")
:sentry (components/new-sentry-mock)
:http (components/new-http)
:router (components/new-router routes/routes)
:database (component/using (components/new-database) [:config])
:publisher (component/using (components/new-publisher
#_[{:qname "mocks.verify"
:event {}
;; every other minute
Expand All @@ -34,21 +28,20 @@
;; every 5 minutes
:delay 300000}])
[:config])
:webserver (component/using (webserver/new-webserver)
:webserver (component/using (components/new-webserver)
[:config :http :router :database :publisher])
:workers (component/using
(redis-queue/new-redis-queue p.workers/workers false)
[:config :database :publisher])))
:workers (component/using (components/new-consumer p.workers/workers false)
[:config :database :publisher])))

(comment
;; init
(utils/start-system-dev! sys-atom (build-system-map))

(redis-publisher/publish! (:publisher @sys-atom) "mock.publication"
{:event {:domain "test-josue"
:new-status "published"}})
(publisher/publish! (:publisher @sys-atom) "mock.publication"
{:event {:domain "test-josue"
:new-status "published"}})

(redis-publisher/publish! (:publisher @sys-atom) "unified.verification.dispatch" {})
(publisher/publish! (:publisher @sys-atom) "unified.verification.dispatch" {})

;; iterate
(utils/stop-system-dev! sys-atom false)
Expand Down
Loading

0 comments on commit 5a905bc

Please sign in to comment.