|
41 | 41 | (assoc :controller-error e)
|
42 | 42 | (assoc :response {:status 500 :body "Internal Server error"}))))))
|
43 | 43 |
|
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)) |
47 | 49 | method (:request-method request)
|
48 | 50 | 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 |
51 | 53 | (xiana/ok (-> state
|
52 | 54 | (?assoc-in [:request-data :match] match)
|
53 | 55 | (?assoc-in [:request-data :handler] handler)
|
54 |
| - (assoc-in [:request-data :action] controller))) |
| 56 | + (assoc-in [:request-data :action] action))) |
55 | 57 |
|
56 | 58 | (if handler
|
57 | 59 | (xiana/ok (-> state
|
|
96 | 98 | (fn [x] (add-http-request x http-request))
|
97 | 99 | (fn [x] (init-acl x acl-cfg))]
|
98 | 100 | (-> this :router-interceptors (select-interceptors :enter identity))
|
99 |
| - [(fn [x] (default-handler x))] |
| 101 | + [(fn [x] (route x))] |
100 | 102 | (-> this :router-interceptors (select-interceptors :leave reverse))
|
| 103 | + |
101 | 104 | (-> this :controller-interceptors (select-interceptors :enter identity))
|
102 | 105 | [(fn [x] (run-controller x))]
|
103 | 106 | (-> this :controller-interceptors (select-interceptors :leave reverse))))
|
104 | 107 | (xiana/extract)
|
105 | 108 | (get :response))))))
|
106 | 109 |
|
107 | 110 | (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 |
112 | 116 | controller-interceptors :controller-interceptors}]
|
113 | 117 | (map->App {:config config
|
114 | 118 | :acl-cfg acl-cfg
|
115 | 119 | :session-backend session-backend
|
116 | 120 | :router-interceptors router-interceptors
|
117 | 121 | :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))})) |
0 commit comments