summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordakkar <dakkar@thenautilus.net>2017-12-17 19:56:20 +0000
committerdakkar <dakkar@thenautilus.net>2017-12-17 19:56:20 +0000
commit1cb6644638e373b03f796545afc4c87c74255e9b (patch)
tree9412eacd2f0a731826c5e4cd5973a9db0679cc39
parentsplit pieces into modules (diff)
downloadUltramarine-1cb6644638e373b03f796545afc4c87c74255e9b.tar.gz
Ultramarine-1cb6644638e373b03f796545afc4c87c74255e9b.tar.bz2
Ultramarine-1cb6644638e373b03f796545afc4c87c74255e9b.zip
rewrite URLs to allow optional '.view'
-rw-r--r--lib/Ultramarine/Controller.pm655
1 files changed, 49 insertions, 6 deletions
diff --git a/lib/Ultramarine/Controller.pm6 b/lib/Ultramarine/Controller.pm6
index 29433fd..35f37a9 100644
--- a/lib/Ultramarine/Controller.pm6
+++ b/lib/Ultramarine/Controller.pm6
@@ -1,5 +1,9 @@
use v6.d.PREVIEW;
use Cro::HTTP::Router;
+use Cro::HTTP::Request;
+use Cro::HTTP::Response;
+use Cro::Transform;
+use Cro;
class Ultramarine::Controller {
has $.license is required;
@@ -10,6 +14,33 @@ class Ultramarine::Controller {
response.set-body(@body);
}
+ my class URLRewrite does Cro::Transform {
+ method consumes() { Cro::HTTP::Request }
+ method produces() { Cro::HTTP::Request }
+
+ my token prefix { '/rest' }
+ my token api-method { '/' \w+ }
+ my token suffix { '.view' }
+ my token query { '?' .* }
+ my token api-url { ^ <prefix> <api-method> <suffix>? <query> $ }
+
+ method transformer(Supply $requests --> Supply) {
+ supply whenever $requests -> $request {
+ with $request {
+ if (.target ~~ /<api-url>/) {
+ emit .clone(
+ original-target => .original-target // .target,
+ target => ($<api-url><prefix api-method query>).join,
+ );
+ }
+ else {
+ emit $_;
+ }
+ }
+ }
+ }
+ }
+
submethod inner-routes() {
return route {
# this needs to be here, not in the Server because its
@@ -19,16 +50,28 @@ class Ultramarine::Controller {
# middlewares, so our serialised won't be seen by the
# "unauthorised" response
before $!authorisation;
- post -> 'ping.view' { respond [] }
- post -> 'getLicense.view' {
- respond [ license => [ |%($!license.status) ] ],
+ post -> 'ping' { respond [] }
+ post -> 'getLicense' {
+ respond [
+ :status<ok>,
+ license => [ |%($!license.status) ],
+ ],
+ }
+ post -> 'getMusicFolders' {
+ respond [
+ :status<ok>,
+ musicFolders => [
+ musicFolder => [ :id<0>, :name<Music> ],
+ ]
+ ],
}
}
}
method routes() {
- return route {
- include rest => self.inner-routes(),
- }
+ return Cro.compose(
+ URLRewrite,
+ route { include 'rest' => self.inner-routes() },
+ );
}
}