Mercury.pm

filter: Statementstmt 94.7% 54 / 57 Branchbran 66.6% 8 / 12 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 81.8% 9 / 11 Podpod 100% 1 / 1 totaltotal 89.5% 77 / 86 CC 9 SCAR 11.1
CC
9
Cov
90%
__ANON__
6.0
startup
5.1
__ANON__
3.0
CRAP 3.0
« Mercury/Command/mercury/broker.pm ↑ Summary Mercury/Pattern/PushPull.pm »
1 package Mercury;
2 6 our $VERSION = '0.016';
3 # ABSTRACT: Main broker application class
4  
5 #pod =head1 SYNOPSIS
6 #pod
7 #pod # Start the broker
8 #pod $ mercury broker
9 #pod
10 #pod # To get help
11 #pod $ mercury help broker
12 #pod
13 #pod =head1 DESCRIPTION
14 #pod
15 #pod This is the main broker application class. This is the standard broker
16 #pod application that is started when you use C<mercury broker>.
17 #pod
18 #pod To learn how to use this application to broker messages, see L<the main
19 #pod Mercury documentation|mercury>. For how to start the broker application,
20 #pod see L<the mercury broker command documentation|Mercury::Command::mercury::broker>
21 #pod or run C<mercury help broker>.
22 #pod
23 #pod To learn how to create a custom message broker to integrate authentication,
24 #pod logging, and other customizations, see L<Mojolicious::Plugin::Mercury>.
25 #pod
26 #pod =cut
27  
28 6 ▶use Mojo::Base 'Mojolicious';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 6 ▶use Scalar::Util qw( weaken refaddr );
Subroutine BEGIN: called CC=1 SCAR=0.0
30 6 ▶use File::Basename qw( dirname );
Subroutine BEGIN: called CC=1 SCAR=0.0
31 6 ▶use File::Spec::Functions qw( catdir );
Subroutine BEGIN: called CC=1 SCAR=0.0
32 6 ▶use Mojo::WebSocket 'WS_PING';
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 4 ▶sub startup {
Subroutine startup: called CC=5 SCAR=16.2
Pod documented
35 4 my ( $app ) = @_;
36 4 $app->log( Mojo::Log->new ); # Log only to STDERR
37 4 $app->plugin( 'Config', { default => { broker => { } } } );
38 4 $app->commands->namespaces( [ 'Mercury::Command::mercury' ] );
39  
40 4 my $r = $app->routes;
41 4 ▶ if ( my $origin = $app->config->{broker}{allow_origin} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $origin = $app->config->{"broker"}{"allow_origin"}) 1 3 4
42 # Allow only '*' for wildcards
43 1 ▶ my @origin = map { quotemeta } ref $origin eq 'ARRAY' ? @$origin : $origin;
Branch: 1 branch
BranchTrueFalseTotal
ref $origin eq 'ARRAY' ? : 0 1 1
44 1 s/\\\*/.*/g for @origin;
45  
46 15 ▶ $r = $r->under( '/' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
47 #say "Got origin: " . $_[0]->req->headers->origin;
48 #say "Checking against: @origin";
49 15 my $origin = $_[0]->req->headers->origin;
50 15 ▶ if ( !$origin || !grep { $origin =~ /$_/ } @origin ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($origin and grep {$origin =~ /$_/u;} @origin) 10 5 15
Condition100%3/3
$origin and grep {$origin =~ /$_/u;} @origin
!ll&&!rl&&r
555
MC/DC100%2/2
$origin and grep {$origin =~ /$_/u;} @origin
$origin grep {$origin =~ /$_/u;} @origin
Truth table100%3/3
A and B
A= $originB= grep {$origin =~ /$_/u;} @origin
ABresult
0X0
100
111
51 10 $_[0]->render(
52 status => '401',
53 text => 'Origin check failed',
54 );
55 10 return;
56 }
57 5 return 1;
58 1 } );
59 }
60  
61 37 ▶ $app->hook( before_dispatch => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
62 37 my ( $c ) = @_;
63 37 ▶ if ( $c->tx->is_websocket ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->tx->is_websocket) 33 4 37
64 33 weaken $c;
65 0 ▶ my $id = Mojo::IOLoop->recurring( 300, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
66 0 ▶ return unless $c;
Branch: 1 branch
BranchTrueFalseTotal
unless $c 0 0 0
67 0 $c->tx->send([1, 0, 0, 0, WS_PING, 'Still alive!']);
68 33 } );
69 33 ▶ $c->tx->once( finish => sub { Mojo::IOLoop->remove( $id ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
70 }
71 4 } );
72  
73 4 $app->plugin( 'Mercury' );
74 4 $r->websocket( '/push/*topic' )
75 ->to( controller => 'PushPull', action => 'push' )
76 ->name( 'push' );
77 4 $r->post( '/push/*topic' )
78 ->to( controller => 'PushPull', action => 'post' )
79 ->name( 'push_post' );
80 4 $r->websocket( '/pull/*topic' )
81 ->to( controller => 'PushPull', action => 'pull' )
82 ->name( 'pull' );
83  
84 4 $r->websocket( '/pub/*topic' )
85 ->to( controller => 'PubSub::Cascade', action => 'publish' )
86 ->name( 'pub' );
87 4 $r->post( '/pub/*topic' )
88 ->to( controller => 'PubSub::Cascade', action => 'post' )
89 ->name( 'pub_post' );
90 4 $r->websocket( '/sub/*topic' )
91 ->to( controller => 'PubSub::Cascade', action => 'subscribe' )
92 ->name( 'sub' );
93  
94 4 $r->websocket( '/bus/*topic' )
95 ->to( controller => 'Bus', action => 'connect' )
96 ->name( 'bus' );
97 4 $r->post( '/bus/*topic' )
98 ->to( controller => 'Bus', action => 'post' )
99 ->name( 'bus_post' );
100  
101 4 ▶ if ( $app->mode eq 'development' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($app->mode eq "development") 4 0 4
102 # Enable the example app
103 4 my $root = catdir( dirname( __FILE__ ), 'Mercury' );
104 4 $app->static->paths->[0] = catdir( $root, 'public' );
105 4 $app->renderer->paths->[0] = catdir( $root, 'templates' );
106 0 ▶ $app->routes->any( '/' )->to( cb => sub { shift->render( 'index' ) } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
107 }
108 }
109  
110 6 1;
111  
112 __END__
« Mercury/Command/mercury/broker.pm ↑ Summary Mercury/Pattern/PushPull.pm »