aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/world/net/http/route.lux
blob: 10d8e37fcea92f0dc35c9faf58a271ae518e8776 (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
(.using
 [library
  [lux {"-" or}
   [control
    [monad (.only do)]
    ["[0]" maybe]
    [concurrency
     ["[0]" async]]]
   [data
    ["[0]" text]
    [number
     ["n" nat]]]
   [macro
    ["^" pattern]]]]
 ["[0]" // (.only URI Server)
  ["[1][0]" status]
  ["[1][0]" response]])

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

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

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

(template [<method> <name>]
  [(def: .public (<name> server)
     (-> Server Server)
     (function (_ (^.let request [identification protocol resource message]))
       (case (the //.#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 (the //.#uri resource))
      (server [identification
               protocol
               (revised //.#uri
                        (|>> (text.clip_since (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)))))