-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.clj
48 lines (44 loc) · 1.74 KB
/
example.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
47
48
(ns prometheus-example.handler.example
(:require [compojure.core :refer :all]
[integrant.core :as ig]
[hugsql.core :as hugsql]))
(hugsql/def-db-fns "sql/user.sql")
(defmethod ig/init-key :prometheus-example.handler/example [_ {:keys [db]}]
(context "/user" []
(context "/:email" [email]
(GET "/info" []
{:body (get-user-by-email (:spec db) {:email email})})
(context "/nested/:some" [some]
(GET "/" []
{:body (assoc
(get-user-by-email (:spec db) {:email email})
:some some)})))
(GET "/" []
(Thread/sleep 10000)
{:body {:example "data"}})
(GET "/some-path/:path-param/before" [path-param]
{:body {:example "data" :param path-param}})
(POST "/" [email]
{:body (upsert-user! (:spec db) {:email email})})))
(defmacro prometheus-context
"Same as compojure.core/context but adds a :whole-context-path key to the request with a vector with all the contexts matched"
[path args & routes]
`(context ~path ~args
(let [f# (routes ~@routes)]
(fn [request#]
(f# (update request# :whole-context-path
(fnil conj []) ~path))))))
(defmethod ig/init-key :prometheus-example.handler/example-nested-context [_ {:keys [db]}]
(prometheus-context "/other-user-path" []
(prometheus-context "/:email" [email]
(GET "/info" []
{:body (get-user-by-email (:spec db) {:email email})})
(prometheus-context "/nested/:some" [some]
(GET "/" []
{:body (assoc
(get-user-by-email (:spec db) {:email email})
:some some)})))
(GET "/" []
{:body {:example "data"}})
(POST "/" [email]
{:body (upsert-user! (:spec db) {:email email})})))