blob: 188508a5deaef9e5f3140eeed5cde8ca72802e84 (
plain)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
(.module:
[lux (#- or)
[control
[monad (#+ do)]
[concurrency
["." promise]]]
[data
["." maybe]
["." text ("text/." Equivalence<Text>)]]]
["." // (#+ URI Server)
["//." status]
["//." response]])
(do-template [<scheme> <name>]
[(def: #export (<name> server)
(-> Server Server)
(function (_ request)
(let [[identification protocol resource message] request]
(case (get@ #//.scheme protocol)
<scheme>
(server request)
_
(promise.resolved //response.not-found)))))]
[#//.HTTP http]
[#//.HTTPS https]
)
(do-template [<method> <name>]
[(def: #export (<name> server)
(-> Server Server)
(function (_ request)
(let [[identification protocol resource message] request]
(case (get@ #//.method resource)
<method>
(server request)
_
(promise.resolved //response.not-found)))))]
[#//.Get get]
[#//.Post post]
[#//.Put put]
[#//.Patch patch]
[#//.Delete delete]
[#//.Head head]
[#//.Connect connect]
[#//.Options options]
[#//.Trace trace]
)
(def: #export (uri path server)
(-> URI Server Server)
(function (_ request)
(let [[identification protocol resource message] request]
(if (text/= path (get@ #//.uri resource))
(server request)
(promise.resolved //response.not-found)))))
(def: #export (sub path server)
(-> URI Server Server)
(function (_ request)
(let [[identification protocol resource message] request]
(if (text.starts-with? path (get@ #//.uri resource))
(server [identification
protocol
(update@ #//.uri
(|>> (text.clip' (text.size path)) maybe.assume)
resource)
message])
(promise.resolved //response.not-found)))))
(def: #export (or primary alternative)
(-> Server Server Server)
(function (_ request)
(do promise.Monad<Promise>
[response (primary request)
#let [[status message] response]]
(if (n/= //status.not-found status)
(alternative request)
(wrap response)))))
|