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;
use Ultramarine::Serialiser::JSON;
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::Authorisation;
post -> 'ping.view' { respond [] }
post -> 'getLicense.view' {
respond [ license => [
:valid<true>,
:email<foo@bar.com>,
: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(
: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,
],
);
$um.start;
react whenever signal(SIGINT) {
$um.stop;
exit;
}