summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordakkar <dakkar@thenautilus.net>2017-12-16 20:14:08 +0000
committerdakkar <dakkar@thenautilus.net>2017-12-16 20:26:14 +0000
commitdb1f46d1af765dfa5cca3d23a9e7246d4966ccd8 (patch)
tree408106d9678e307e411f53c1039a943b9f662cc6
parentfix authorisation to return Subsonic response (diff)
downloadUltramarine-db1f46d1af765dfa5cca3d23a9e7246d4966ccd8.tar.gz
Ultramarine-db1f46d1af765dfa5cca3d23a9e7246d4966ccd8.tar.bz2
Ultramarine-db1f46d1af765dfa5cca3d23a9e7246d4966ccd8.zip
rough server w/ serialisation
-rw-r--r--bin/ultramarine46
-rw-r--r--lib/Ultramarine/Middleware/SetContentType.pm626
-rw-r--r--lib/Ultramarine/Serialiser/XML.pm630
3 files changed, 102 insertions, 0 deletions
diff --git a/bin/ultramarine b/bin/ultramarine
new file mode 100644
index 0000000..b98c358
--- /dev/null
+++ b/bin/ultramarine
@@ -0,0 +1,46 @@
+#!/usr/bin/env perl6
+use v6.d.PREVIEW;
+use Cro::HTTP::Router;
+use Cro::HTTP::Server;
+use Ultramarine::Model::Users;
+use Ultramarine::Middleware::Authentication;
+use Ultramarine::Middleware::Authorisation;
+use Ultramarine::Middleware::SetContentType;
+use Ultramarine::Serialiser::XML;
+
+sub respond(*@body) {
+ response.status = 200;
+ response.set-body(@body);
+}
+
+my $users = Ultramarine::Model::Users.new(
+ accounts=>{me=>'sesame'},
+);
+
+my $ultramarine_rest = route {
+ before Ultramarine::Middleware::Authentication.new(:$users);
+ before Ultramarine::Middleware::Authorisation;
+ after Ultramarine::Middleware::SetContentType;
+
+ get -> 'ping.view' { respond [] }
+};
+
+my $ultramarine = route {
+
+ body-serializer Ultramarine::Serialiser::XML.new;
+
+ delegate <rest *> => $ultramarine_rest;
+}
+
+my Cro::Service $um = Cro::HTTP::Server.new(
+ :host<localhost>,
+ :port<8080>,
+ application => $ultramarine,
+);
+
+$um.start;
+
+react whenever signal(SIGINT) {
+ $um.stop;
+ exit;
+}
diff --git a/lib/Ultramarine/Middleware/SetContentType.pm6 b/lib/Ultramarine/Middleware/SetContentType.pm6
new file mode 100644
index 0000000..a3fab94
--- /dev/null
+++ b/lib/Ultramarine/Middleware/SetContentType.pm6
@@ -0,0 +1,26 @@
+use v6.d.PREVIEW;
+use Cro::HTTP::Middleware;
+use Cro::HTTP::BodySerializerSelector;
+use Ultramarine::Serialiser::XML;
+
+my %types = (
+ xml => 'application/xml',
+ json => 'application/json',
+ jsonp => 'application/javascript',
+);
+
+class Ultramarine::Middleware::SetContentType
+ does Cro::HTTP::Middleware::Response {
+ method process(Supply:D $response-stream) {
+ supply whenever $response-stream -> $response {
+ with $response.request {
+ my $format = .query-value('f')[0] // 'xml';
+ with %types{$format} {
+ $response.append-header('Content-type',$_);
+ }
+ }
+
+ emit $response;
+ }
+ }
+}
diff --git a/lib/Ultramarine/Serialiser/XML.pm6 b/lib/Ultramarine/Serialiser/XML.pm6
new file mode 100644
index 0000000..7bb14b8
--- /dev/null
+++ b/lib/Ultramarine/Serialiser/XML.pm6
@@ -0,0 +1,30 @@
+use v6.d.PREVIEW;
+use Cro::HTTP::BodySerializer;
+use XML::Writer;
+
+class Ultramarine::Serialiser::XML
+ does Cro::HTTP::BodySerializer {
+
+ method is-applicable(Cro::HTTP::Message $message, $body --> Bool) {
+ with $message.content-type {
+ (.type eq 'application' && .subtype eq 'xml' || .suffix eq 'xml') &&
+ ($body ~~ Map || $body ~~ List)
+ }
+ else {
+ False
+ }
+ }
+
+ method serialize(Cro::HTTP::Message $message, $body --> Supply) {
+ my $xml = XML::Writer.serialize(
+ subsonic-response => [
+ :xmlns<http://subsonic.org/restapi>,
+ :version<1.13.0>,
+ |$body,
+ ],
+ ).encode('utf-8');
+ self!set-content-length($message, $xml.bytes);
+ supply { emit $xml }
+ }
+
+}