aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/world/net/http/route.lux
blob: 9689e3414cbd63a7185f6ad46ec55ce5e23f8bc2 (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
(.module:
  [library
   [lux (#- or)
    [control
     [monad (#+ do)]
     ["." maybe]
     [concurrency
      ["." async]]]
    [data
     ["." text]
     [number
      ["n" nat]]]]]
  ["." // (#+ URI Server)
   ["#." status]
   ["#." response]])

(template [<scheme> <name>]
  [(def: .public (<name> server)
     (-> Server Server)
     (function (_ (^@ request [identification protocol resource message]))
       (case (value@ #//.scheme protocol)
         <scheme>
         (server request)

         _
         (async.resolved //response.not_found))))]

  [#//.HTTP  http]
  [#//.HTTPS https]
  )

(template [<method> <name>]
  [(def: .public (<name> server)
     (-> Server Server)
     (function (_ (^@ request [identification protocol resource message]))
       (case (value@ #//.method resource)
         <method>
         (server request)

         _
         (async.resolved //response.not_found))))]

  [#//.Get     get]
  [#//.Post    post]
  [#//.Put     put]
  [#//.Patch   patch]
  [#//.Delete  delete]
  [#//.Head    head]
  [#//.Connect connect]
  [#//.Options options]
  [#//.Trace   trace]
  )

(def: .public (uri path server)
  (-> URI Server Server)
  (function (_ [identification protocol resource message])
    (if (text.starts_with? path (value@ #//.uri resource))
      (server [identification
               protocol
               (revised@ #//.uri
                         (|>> (text.clip' (text.size path)) maybe.trusted)
                         resource)
               message])
      (async.resolved //response.not_found))))

(def: .public (or primary alternative)
  (-> Server Server Server)
  (function (_ request)
    (do async.monad
      [response (primary request)
       .let [[status message] response]]
      (if (n.= //status.not_found status)
        (alternative request)
        (in response)))))