-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathroutes.clj
36 lines (28 loc) · 915 Bytes
/
routes.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
(ns grumpy.routes
(:refer-clojure :exclude [sort])
(:require
[io.pedestal.http.route :as route]))
(defn- expand-route [[method path & interceptors]]
(let [interceptors' (->> interceptors flatten (remove nil?) vec)
handler (last interceptors')
name (if (symbol? handler)
(keyword (str handler))
(keyword (str (name method) ":" path)))]
[path method interceptors' :route-name name]))
(defn expand [& routes]
(->> routes
(map expand-route)
(into #{})
(route/expand-routes)))
(defn compare-parts [[p1 & ps1] [p2 & ps2]]
(cond
(and (nil? p1) (nil? p2)) 0
(= p1 p2) (compare-parts ps1 ps2)
(= (type p1) (type p2)) (compare p1 p2)
(nil? p1) -1
(nil? p2) 1
(string? p1) -1
(string? p2) 1
:else (compare p1 p2)))
(defn sort [routes]
(sort-by :path-parts compare-parts routes))