From ba1caee706c51e25a6ee325f4c338aa4d2d31292 Mon Sep 17 00:00:00 2001 From: dakkar Date: Sun, 17 Dec 2017 19:09:31 +0000 Subject: split pieces into modules --- bin/ultramarine | 81 +++++++++++++++------------------------------------------ 1 file changed, 21 insertions(+), 60 deletions(-) (limited to 'bin/ultramarine') diff --git a/bin/ultramarine b/bin/ultramarine index 9df01d3..07cd869 100644 --- a/bin/ultramarine +++ b/bin/ultramarine @@ -1,80 +1,41 @@ #!/usr/bin/env perl6 use v6.d.PREVIEW; -use Cro::HTTP::Router; -use Cro::HTTP::Server; +use Ultramarine::Model::License; use Ultramarine::Model::Users; use Ultramarine::Middleware::Authentication; use Ultramarine::Middleware::Authorisation; use Ultramarine::Middleware::SetContentType; use Ultramarine::Serialiser::XML; use Ultramarine::Serialiser::JSON; - -sub respond(*@body) { - response.status = 200; - response.set-body(@body); -} +use Ultramarine::Controller; +use Ultramarine::Server; my $users = Ultramarine::Model::Users.new( - accounts=>{me=>'sesame'}, + accounts=> { me => 'sesame' }, ); +my $controller = Ultramarine::Controller.new( + license => Ultramarine::Model::License.new, + authorisation => Ultramarine::Middleware::Authorisation.new, +).routes; -my $ultramarine_rest = route { - # this needs to be here, not in the Server because its - # short-circuited "unauthorised" response will be emited in an - # 'after' middleware, but the ResponseSerializerExtension is - # applied just before those middlewares, so our serialised won't - # be seen by the "unauthorised" response - before Ultramarine::Middleware::Authorisation; - post -> 'ping.view' { respond [] } - post -> 'getLicense.view' { - respond [ license => [ - :valid, - :email, - :licenseExpires<2019-01-01:00:00:00>, - ] ], - } -}; - -my $ultramarine = route { - include rest => $ultramarine_rest; -} - -my class Trace does Cro::HTTP::Middleware::RequestResponse { - method process-requests(Supply $requests --> Supply) { - supply whenever $requests -> $req { - say $req.Str; - emit $req; - } - } - method process-responses(Supply $responses --> Supply) { - supply whenever $responses -> $res { - say $res.Str; - emit $res; - } - } -} - -my Cro::Service $um = Cro::HTTP::Server.new( +my $server = Ultramarine::Server.new( :host<192.168.1.145>, :port<8080>, - application => $ultramarine, - before => [ - Trace, - Ultramarine::Middleware::Authentication.new(:$users), - ], - add-body-serializers => [ - Ultramarine::Serialiser::XML, - Ultramarine::Serialiser::JSON, - ], - after => [ - Ultramarine::Middleware::SetContentType, - ], + :$controller, + authentication => Ultramarine::Middleware::Authentication.new(:$users), + serialisers => [ + Ultramarine::Serialiser::XML.new, + Ultramarine::Serialiser::JSON.new, + ], + set-content-type => Ultramarine::Middleware::SetContentType.new, + do-trace => True, +).server; -); +$server.start; -$um.start; +say "ready"; react whenever signal(SIGINT) { - $um.stop; + $server.stop; exit; } -- cgit v1.2.3