summaryrefslogtreecommitdiff
path: root/lib/Ultramarine/Server.pm
blob: ec7220c4ccd823791f5dae45ea2d21b76633673c (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
use v6.d.PREVIEW;
use Cro::HTTP::Server;
use Cro::HTTP::Request;
use Cro::HTTP::Response;
 
class Ultramarine::Server {
    has $.authentication is required;
    has @.serialisers is required;
    has $.set-content-type is required;
    has $.controller is required;
    has $.host = 'localhost';
    has $.port = 8080;
    has $.do-trace = False;
    
    my class Trace does Cro::Transform {
        has $.type is required;
 
        method consumes() { $!type }
        method produces() { $!type }
        
        method transformer(Supply $input --> Supply{
            supply whenever $input -> $thing {
                say $thing.trace-output,"\n";
                say (await $thing.body-text).indent(2);
                emit $thing;
            }
        }
    }
 
    method server() {
        return Cro::HTTP::Server.new(
            :$!host,
            :$!port,
            application => $!controller,
            before => [
                       $.do-trace ?? Trace.new(type=>Cro::HTTP::Request!! (),
                       $.authentication,
                   ],
            add-body-serializers => @.serialisers,
            after => [
                      $.set-content-type,
                      $.do-trace ?? Trace.new(type=>Cro::HTTP::Response!! (),
                  ],
        );
    }
}