aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEduardo Julian2019-01-09 20:15:07 -0400
committerEduardo Julian2019-01-09 20:15:07 -0400
commit4ddbc26a1e30a9323ac4f55bdf6992c540bbe207 (patch)
treec3f1d29b513e139df5d7c5b7ef083b87def15c99
parentaedd31b078d43d04e6877238c90d1db4021b5bf2 (diff)
Added routing.
-rw-r--r--stdlib/source/lux/world/net/http/route.lux82
1 files changed, 82 insertions, 0 deletions
diff --git a/stdlib/source/lux/world/net/http/route.lux b/stdlib/source/lux/world/net/http/route.lux
new file mode 100644
index 000000000..188508a5d
--- /dev/null
+++ b/stdlib/source/lux/world/net/http/route.lux
@@ -0,0 +1,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)))))