From db1f46d1af765dfa5cca3d23a9e7246d4966ccd8 Mon Sep 17 00:00:00 2001 From: dakkar Date: Sat, 16 Dec 2017 20:14:08 +0000 Subject: rough server w/ serialisation --- bin/ultramarine | 46 +++++++++++++++++++++++++++ lib/Ultramarine/Middleware/SetContentType.pm6 | 26 +++++++++++++++ lib/Ultramarine/Serialiser/XML.pm6 | 30 +++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 bin/ultramarine create mode 100644 lib/Ultramarine/Middleware/SetContentType.pm6 create mode 100644 lib/Ultramarine/Serialiser/XML.pm6 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 => $ultramarine_rest; +} + +my Cro::Service $um = Cro::HTTP::Server.new( + :host, + :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, + :version<1.13.0>, + |$body, + ], + ).encode('utf-8'); + self!set-content-length($message, $xml.bytes); + supply { emit $xml } + } + +} -- cgit v1.2.3