| 1 | package Clustericious::Plugin::CommonRoutes; | ||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||
| 3 | 47 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 4 | 47 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 5 | 47 | ▶ | use 5.010; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 6 | 47 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 7 | 47 | ▶ | use File::Basename (); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 8 | 47 | ▶ | use Sys::Hostname (); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 9 | 47 | ▶ | use List::Util qw/ uniq /; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||
| 11 | # ABSTRACT: Routes common to all clustericious applications | ||||||||||||||||||||||||||||||||||||
| 12 | 47 | our $VERSION = '1.29'; # VERSION | |||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||
| 15 | sub register | ||||||||||||||||||||||||||||||||||||
| 16 | 51 | ▶ | { | ||||||||||||||||||||||||||||||||||
|
Subroutine
register: called CC=7 SCAR=23.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 17 | 51 | my($self, $app) = @_; | |||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||
| 19 | $app->plugin('Clustericious::Plugin::AutodataHandler') | ||||||||||||||||||||||||||||||||||||
| 20 | 51 | ▶ | unless $app->renderer->handlers->{autodata}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||
| 23 | 51 | my $version = do { | |||||||||||||||||||||||||||||||||||
| 24 | 51 | my $class = ref $app; | |||||||||||||||||||||||||||||||||||
| 25 | 51 | ▶ | $class = eval { $app->renderer->classes->[0] } // 'main' if $class eq 'Mojolicious::Lite'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
eval {
do {
$app->renderer->classes->[0]
}
} // "main"
MC/DC0%0/1
eval {
do {
$app->renderer->classes->[0]
}
} // "main"
eval {
do {
$app->renderer->classes->[0]
}
}
Truth table50%1/2
A
A= eval {
do {
$app->renderer->classes->[0]
}
}
| |||||||||||||||||||||||||||||||||||||
| 26 | 51 | ▶ | $class->VERSION // 'dev'; | ||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$class->VERSION // "dev"
MC/DC100%1/1
$class->VERSION // "dev"
$class->VERSION
Truth table100%2/2
A
A= $class->VERSION
| |||||||||||||||||||||||||||||||||||||
| 27 | }; | ||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||
| 29 | 2 | ▶ | $app->routes->get('/version')->to(cb => sub { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 30 | 2 | shift->stash(autodata => [ $version ]); | |||||||||||||||||||||||||||||||||||
| 31 | 51 | }); | |||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||
| 34 | 51 | my($app_name, $hostname) = do { | |||||||||||||||||||||||||||||||||||
| 35 | 51 | my $name = ref $app; | |||||||||||||||||||||||||||||||||||
| 36 | 51 | ▶ | $name = File::Basename::basename($0) if $name eq 'Mojolicious::Lite'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 37 | 51 | ($name, Sys::Hostname::hostname()); | |||||||||||||||||||||||||||||||||||
| 38 | }; | ||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||
| 40 | 6 | ▶ | $app->routes->get('/status')->to(cb => sub { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 41 | 6 | my($self) = @_; | |||||||||||||||||||||||||||||||||||
| 42 | 6 | $self->stash(autodata => { | |||||||||||||||||||||||||||||||||||
| 43 | app_name => $app_name, | ||||||||||||||||||||||||||||||||||||
| 44 | server_version => $version, | ||||||||||||||||||||||||||||||||||||
| 45 | server_hostname => $hostname, | ||||||||||||||||||||||||||||||||||||
| 46 | server_url => $self->url_for('/')->to_abs->to_string, | ||||||||||||||||||||||||||||||||||||
| 47 | }); | ||||||||||||||||||||||||||||||||||||
| 48 | 51 | }); | |||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||
| 51 | 1 | ▶ | $app->routes->get('/api')->to(cb => sub { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 52 | 1 | shift->render( autodata => [ __PACKAGE__->_dump_api($app) ]); | |||||||||||||||||||||||||||||||||||
| 53 | 51 | }); | |||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | $app->routes->get('/api/:table')->to(cb => sub { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||
| 57 | 0 | my($self) = @_; | |||||||||||||||||||||||||||||||||||
| 58 | 0 | my $table = __PACKAGE__->_dump_api_table($self->stash('table')); | |||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | $table ? $self->render( autodata => $table ) : $self->reply->not_found; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 60 | 51 | }); | |||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | $app->routes->get('/log/:lines' => [ lines => qr/\d+/ ] => sub { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||
| 64 | 0 | my($self) = @_; | |||||||||||||||||||||||||||||||||||
| 65 | 0 | my $lines = $self->stash("lines"); | |||||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | unless ($self->config->export_logs(default => 0)) | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 67 | { | ||||||||||||||||||||||||||||||||||||
| 68 | 0 | return $self->render(text => 'logs not available'); | |||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | $self->render(text => Clustericious::Log->tail(lines => $lines || 10) || '** empty log **'); | ||||||||||||||||||||||||||||||||||
|
Condition0%0/4
'Clustericious::Log'->tail('lines', $lines || 10) || '** empty log **'
$lines || 10
MC/DC0%0/1
'Clustericious::Log'->tail('lines', $lines || 10) || '** empty log **'
'Clustericious::Log'->tail('lines', $lines || 10)
MC/DC0%0/1
$lines || 10
$lines
Truth table0%0/2
A
A= 'Clustericious::Log'->tail('lines', $lines || 10)
Truth table0%0/2
A
A= $lines
| |||||||||||||||||||||||||||||||||||||
| 71 | 51 | ▶ | }) if $app->isa('Clustericious::App'); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | $app->routes->options('/*opturl' => { opturl => '' } => sub { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||
| 74 | 0 | my($self) = @_; | |||||||||||||||||||||||||||||||||||
| 75 | 0 | $self->res->headers->add( 'Access-Control-Allow-Methods' => 'GET, POST, OPTIONS' ); | |||||||||||||||||||||||||||||||||||
| 76 | # Allow-Origin and Allow-Headers added in after_dispatch hook. | ||||||||||||||||||||||||||||||||||||
| 77 | 0 | $self->render(text => 'ok'); | |||||||||||||||||||||||||||||||||||
| 78 | 51 | }); | |||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||
| 81 | sub _have_rose | ||||||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_have_rose: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | Rose::Planter->can('tables') ? 1 : 0; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||
| 86 | 1 | ▶ | sub _dump_api { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_dump_api: called CC=11 SCAR=40.1
| |||||||||||||||||||||||||||||||||||||
| 87 | 1 | my $class = shift; | |||||||||||||||||||||||||||||||||||
| 88 | 1 | my $app = shift; | |||||||||||||||||||||||||||||||||||
| 89 | 1 | ▶ | my $routes = shift || $app->routes->children; | ||||||||||||||||||||||||||||||||||
|
Condition33%1/3
shift() || $app->routes->children
MC/DC0%0/2
shift() || $app->routes->children
shift()
$app->routes->children
Truth table33%1/3
A || B
A= shift()B= $app->routes->children
| |||||||||||||||||||||||||||||||||||||
| 90 | 1 | my @all; | |||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||
| 92 | 1 | for my $r (@$routes) | |||||||||||||||||||||||||||||||||||
| 93 | { | ||||||||||||||||||||||||||||||||||||
| 94 | 1 | my $pat = $r->pattern; | |||||||||||||||||||||||||||||||||||
| 95 | 1 | $pat->_compile; | |||||||||||||||||||||||||||||||||||
| 96 | 0 | my %placeholders = map { $_ => "<$_>" } @{ $pat->placeholders }; | |||||||||||||||||||||||||||||||||||
| 97 | 1 | ▶ | my $method = uc join ',', @{ $r->via || ["GET"] }; | ||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$r->via || ['GET']
MC/DC0%0/1
$r->via || ['GET']
$r->via
Truth table0%0/2
A
A= $r->via
| |||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | if (_have_rose() && $placeholders{table}) | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition0%0/3
_have_rose() and $placeholders{"table"}
MC/DC0%0/2
_have_rose() and $placeholders{"table"}
_have_rose()
$placeholders{"table"}
Truth table0%0/3
A and B
A= _have_rose()B= $placeholders{"table"}
| |||||||||||||||||||||||||||||||||||||
| 99 | { | ||||||||||||||||||||||||||||||||||||
| 100 | 0 | for my $table (Rose::Planter->tables) | |||||||||||||||||||||||||||||||||||
| 101 | { | ||||||||||||||||||||||||||||||||||||
| 102 | 0 | $placeholders{table} = $table; | |||||||||||||||||||||||||||||||||||
| 103 | 0 | my $pat = $pat->unparsed; | |||||||||||||||||||||||||||||||||||
| 104 | 0 | $pat =~ s/:table/$table/; | |||||||||||||||||||||||||||||||||||
| 105 | 0 | push @all, "$method $pat"; | |||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||
| 108 | ▶ | elsif (_have_rose() && $placeholders{items}) | |||||||||||||||||||||||||||||||||||
|
Condition0%0/3
_have_rose() and $placeholders{"items"}
MC/DC0%0/2
_have_rose() and $placeholders{"items"}
_have_rose()
$placeholders{"items"}
Truth table0%0/3
A and B
A= _have_rose()B= $placeholders{"items"}
| |||||||||||||||||||||||||||||||||||||
| 109 | { | ||||||||||||||||||||||||||||||||||||
| 110 | 0 | for my $plural (Rose::Planter->plurals) | |||||||||||||||||||||||||||||||||||
| 111 | { | ||||||||||||||||||||||||||||||||||||
| 112 | 0 | $placeholders{items} = $plural; | |||||||||||||||||||||||||||||||||||
| 113 | 0 | my $line = $pat->render(\%placeholders); | |||||||||||||||||||||||||||||||||||
| 114 | 0 | push @all, "$method $line"; | |||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||
| 117 | elsif (defined($pat->unparsed)) | ||||||||||||||||||||||||||||||||||||
| 118 | { | ||||||||||||||||||||||||||||||||||||
| 119 | 0 | push @all, join ' ', $method, $pat->unparsed; | |||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||
| 121 | else | ||||||||||||||||||||||||||||||||||||
| 122 | { | ||||||||||||||||||||||||||||||||||||
| 123 | 0 | push @all, $class->_dump_api($app, $r->children); | |||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||
| 126 | 0 | return uniq sort @all; | |||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||
| 129 | sub _dump_api_table_types | ||||||||||||||||||||||||||||||||||||
| 130 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_dump_api_table_types: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||
| 131 | 0 | my(undef, $rose_type) = @_; | |||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | return 'datetime' if $rose_type =~ /^datetime/; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 133 | state $types = { | ||||||||||||||||||||||||||||||||||||
| 134 | 0 | (map { $_ => 'string' } qw( character text varchar )), | |||||||||||||||||||||||||||||||||||
| 135 | 0 | (map { $_ => 'numeric' } 'numeric', 'float', 'double precision','decimal'), | |||||||||||||||||||||||||||||||||||
| 136 | 0 | (map { $_ => $_ } qw( blob set time interval enum bytea chkpass bitfield date boolean )), | |||||||||||||||||||||||||||||||||||
| 137 | 0 | (map { $_ => 'integer' } qw( bigint integer bigserial serial )), | |||||||||||||||||||||||||||||||||||
| 138 | 0 | (map { $_ => 'epoch' } 'epoch', 'epoch hires'), | |||||||||||||||||||||||||||||||||||
| 139 | 0 | (map { $_ => 'timestamp' } 'timestamp', 'timestamp with time zone'), | |||||||||||||||||||||||||||||||||||
| 140 | }; | ||||||||||||||||||||||||||||||||||||
| 141 | 0 | ▶ | return $types->{$rose_type} // 'unknown'; | ||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$types->{$rose_type} // "unknown"
MC/DC0%0/1
$types->{$rose_type} // "unknown"
$types->{$rose_type}
Truth table0%0/2
A
A= $types->{$rose_type}
| |||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||
| 144 | sub _dump_api_table | ||||||||||||||||||||||||||||||||||||
| 145 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_dump_api_table: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||
| 146 | 0 | my(undef, $table) = @_; | |||||||||||||||||||||||||||||||||||
| 147 | 0 | ▶ | return unless _have_rose(); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 148 | 0 | my $class = Rose::Planter->find_class($table); | |||||||||||||||||||||||||||||||||||
| 149 | 0 | ▶ | return unless defined $class; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||
| 151 | return { | ||||||||||||||||||||||||||||||||||||
| 152 | columns => { | ||||||||||||||||||||||||||||||||||||
| 153 | map { | ||||||||||||||||||||||||||||||||||||
| 154 | 0 | $_->name => { | |||||||||||||||||||||||||||||||||||
| 155 | rose_db_type => $_->type, | ||||||||||||||||||||||||||||||||||||
| 156 | not_null => $_->not_null, | ||||||||||||||||||||||||||||||||||||
| 157 | type => __PACKAGE__->_dump_api_table_types($_->type), | ||||||||||||||||||||||||||||||||||||
| 158 | } } $class->meta->columns | ||||||||||||||||||||||||||||||||||||
| 159 | }, | ||||||||||||||||||||||||||||||||||||
| 160 | primary_key => [ | ||||||||||||||||||||||||||||||||||||
| 161 | 0 | map { $_->name } $class->meta->primary_key_columns | |||||||||||||||||||||||||||||||||||
| 162 | ], | ||||||||||||||||||||||||||||||||||||
| 163 | }; | ||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||
| 166 | 47 | 1; | |||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||
| 168 | __END__ | ||||||||||||||||||||||||||||||||||||