summaryrefslogtreecommitdiff
path: root/bin/ultramarine
blob: b01527cdf5c540d0bc698c575c9d69f07a874026 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/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;
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 {
    # 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;
    get -> 'ping.view' { respond [] }
};
 
my $ultramarine = route {
    include rest => $ultramarine_rest;
}
 
 
 
my Cro::Service $um = Cro::HTTP::Server.new(
    :host<192.168.1.145>,
    :port<8080>,
    application => $ultramarine,
    before => [
               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;
}