Skip to content

Commit ddb93a9

Browse files
authored
Merge pull request #69 from Flexiana/easier-component-maintenance
Add a easier way to manage components
2 parents 574907a + bc04923 commit ddb93a9

File tree

6 files changed

+96
-21
lines changed

6 files changed

+96
-21
lines changed

.clj-kondo/config.edn

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{:lint-as {nubank.workspaces.core/defcard clojure.core/def
2+
nubank.workspaces.core/deftest clojure.test/deftest
3+
xiana.core/flow-> clojure.core/->}}

project.clj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(defproject com.flexiana/framework "0.1.7"
1+
(defproject com.flexiana/framework "0.1.8"
22
:description "Framework"
33
:url "https://github.com/Flexiana/framework"
44
:license {:name "FIXME" :url "FIXME"}

src/framework/components/app/core.clj

+45-11
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,19 @@
4141
(assoc :controller-error e)
4242
(assoc :response {:status 500 :body "Internal Server error"}))))))
4343

44-
(defn default-handler
45-
[{request :request {router :router} :deps :as state}]
46-
(let [match (r/match-by-path (:ring-router router) (:uri request))
44+
(defn route
45+
[{request :request
46+
{router :router} :deps
47+
:as state}]
48+
(let [match (r/match-by-path router (:uri request))
4749
method (:request-method request)
4850
handler (or (get-in match [:data :handler]) (-> match :result method :handler))
49-
controller (or (get-in match [:data :action]) (-> match :data method :action))]
50-
(if controller
51+
action (or (get-in match [:data :action]) (-> match :data method :action))]
52+
(if action
5153
(xiana/ok (-> state
5254
(?assoc-in [:request-data :match] match)
5355
(?assoc-in [:request-data :handler] handler)
54-
(assoc-in [:request-data :action] controller)))
56+
(assoc-in [:request-data :action] action)))
5557

5658
(if handler
5759
(xiana/ok (-> state
@@ -96,22 +98,54 @@
9698
(fn [x] (add-http-request x http-request))
9799
(fn [x] (init-acl x acl-cfg))]
98100
(-> this :router-interceptors (select-interceptors :enter identity))
99-
[(fn [x] (default-handler x))]
101+
[(fn [x] (route x))]
100102
(-> this :router-interceptors (select-interceptors :leave reverse))
103+
101104
(-> this :controller-interceptors (select-interceptors :enter identity))
102105
[(fn [x] (run-controller x))]
103106
(-> this :controller-interceptors (select-interceptors :leave reverse))))
104107
(xiana/extract)
105108
(get :response))))))
106109

107110
(defn make-app
108-
[{config :config
109-
acl-cfg :acl-cfg
110-
session-backend :session-backend
111-
router-interceptors :router-interceptors
111+
"DEPRECATED"
112+
[{config :config
113+
acl-cfg :acl-cfg
114+
session-backend :session-backend
115+
router-interceptors :router-interceptors
112116
controller-interceptors :controller-interceptors}]
113117
(map->App {:config config
114118
:acl-cfg acl-cfg
115119
:session-backend session-backend
116120
:router-interceptors router-interceptors
117121
:controller-interceptors controller-interceptors}))
122+
123+
(defn ->app
124+
[{acl-cfg :acl-cfg
125+
session-backend :session-backend
126+
:as config}]
127+
(with-meta config
128+
`{component/start ~(fn [{:keys [router db]
129+
:as this}]
130+
(let [state (xiana.core/flow->
131+
(create-empty-state)
132+
(add-deps {:router router, :db db})
133+
(add-session-backend session-backend)
134+
(init-acl acl-cfg))]
135+
(assoc this
136+
:handler
137+
(fn [http-request]
138+
(xiana/flow->
139+
state
140+
(add-http-request http-request)
141+
(-> this :router-interceptors (select-interceptors :enter identity))
142+
[(fn [x] (route x))]
143+
(-> this :router-interceptors (select-interceptors :leave reverse))
144+
145+
(-> this :controller-interceptors (select-interceptors :enter identity))
146+
[(fn [x] (run-controller x))]
147+
(-> this :controller-interceptors (select-interceptors :leave reverse))
148+
(xiana/extract)
149+
(get :response))))))
150+
component/stop ~(fn [this]
151+
(dissoc this :handler))}))

src/framework/components/router/core.clj

+16-5
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,23 @@
77
[db]
88
component/Lifecycle
99
(stop [this] this)
10-
(start
11-
[this]
12-
(assoc this :ring-router (ring/router (concat
13-
(:routes (:specific-component this)) ; adds routes specific to some component
14-
(:custom-routes this))))))
10+
(start [this]
11+
(assoc this :ring-router (ring/router (concat
12+
(:routes (:specific-component this)) ; adds routes specific to some component
13+
(:custom-routes this))))))
1514

1615
(defn make-router
16+
"DEPRECATED"
1717
[routes]
1818
(map->Router {:custom-routes routes}))
19+
20+
(defn ->router
21+
[_config routes]
22+
(with-meta {:custom-routes routes}
23+
`{component/start ~(fn [this]
24+
(assoc this :router
25+
(ring/router (concat
26+
(-> this :specific-component :routes)
27+
(-> this :custom-routes)))))
28+
component/stop ~(fn [this]
29+
(dissoc this :router))}))

src/framework/components/web_server/core.clj

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
(ns framework.components.web-server.core
22
(:require
33
[com.stuartsierra.component :as component]
4-
[ring.adapter.jetty :as jetty]))
4+
[ring.adapter.jetty :as jetty])
5+
(:import
6+
(org.eclipse.jetty.server
7+
Server)))
58

69
(defrecord WebServer
710
[http-server app config]
@@ -16,5 +19,19 @@
1619
this))
1720

1821
(defn make-web-server
22+
"DEPRECATED"
1923
[config]
2024
(map->WebServer {:config config}))
25+
26+
(defn ->web-server
27+
[{web-cfg :framework.app/web-server
28+
:as config}]
29+
(with-meta config
30+
`{component/start ~(fn [{{handler :handler} :app
31+
:as this}]
32+
(assoc this :web-server
33+
(jetty/run-jetty handler web-cfg)))
34+
component/stop ~(fn [{:keys [^Server web-server]
35+
:as this}]
36+
(.stop web-server)
37+
(dissoc this :web-server))}))

src/framework/db/storage.clj

+13-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
[com.stuartsierra.component :as component]
44
[next.jdbc :as jdbc]))
55

6+
;; DEPRECATED
67
(defrecord PostgreSQL
78
[config]
89
component/Lifecycle
@@ -14,6 +15,15 @@
1415
(stop [this]
1516
(dissoc this :datasource :connection)))
1617

17-
(defn postgresql
18-
[config]
19-
(->PostgreSQL config))
18+
(defn ->postgresql
19+
[{pg-cfg :framework.db.storage/postgresql}]
20+
(with-meta pg-cfg
21+
`{component/start ~(fn [this]
22+
(let [datasource (jdbc/get-datasource pg-cfg)]
23+
(-> this
24+
(assoc :datasource datasource)
25+
(assoc :connection (jdbc/get-connection datasource)))))
26+
component/stop ~(fn [{:keys [connection]
27+
:as this}]
28+
(.close connection)
29+
(dissoc this :datasource :connection))}))

0 commit comments

Comments
 (0)