-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.clj
46 lines (44 loc) · 1.8 KB
/
route.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
(ns corbihttp.interceptor.route
(:require [corbihttp.metric :as metric]
[corbihttp.log :as log]
[exoscale.ex :as ex]
[reitit.core :as r])
(:import io.micrometer.core.instrument.Timer))
(defn route!
[{:keys [request] :as ctx}
{:keys [registry router]}]
(let [uri (str (:uri request))
method (-> request :request-method)
match-result (r/match-by-path router
uri)
req-handler (get (:data match-result) method)]
(if req-handler
(assoc ctx
:request (assoc request :path-params (:path-params match-result))
:start-time (when registry
(java.time.Instant/now))
:handler req-handler
:timer (when registry
(metric/get-timer! registry
:http.request.duration
{"uri" uri
"method" (name method)})))
(do (log/warnf {}
"uri %s not found for method %s"
uri method)
(throw (ex/ex-info (format "uri %s not found for method %s"
uri (name method))
[::not-found [:corbi/user ::ex/not-found]]))))))
(defn route
"Computes the handler to use using the provided router"
[{:keys [router] :as params}]
{:name ::main-handler
:enter (fn [ctx]
(route! ctx
(assoc params :router (r/router router))))
:leave (fn [ctx]
(when-let [start-time (:start-time ctx)]
(let [end (java.time.Instant/now)]
(.record ^Timer (:timer ctx)
(java.time.Duration/between start-time end)))
ctx))})