line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojo::Leds::Plugin::CommonRoutes; |
2
|
|
|
|
|
|
|
$Mojo::Leds::Plugin::CommonRoutes::VERSION = '1.15'; |
3
|
|
|
|
|
|
|
# ABSTRACT: Add routes to get app informations (library version, routes,...) |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
1352
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
6
|
|
6
|
1
|
|
|
1
|
|
170
|
use Mojo::File qw/curfile/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
55
|
|
7
|
1
|
|
|
1
|
|
7
|
use Net::Domain qw(hostname); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
58
|
|
8
|
1
|
|
|
1
|
|
6
|
use re qw(regexp_pattern); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
107
|
|
9
|
1
|
|
|
1
|
|
5
|
use Mojo::Util qw(encode getopt tablify); |
|
1
|
|
|
|
|
10
|
|
|
1
|
|
|
|
|
819
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub register { |
12
|
1
|
|
|
1
|
1
|
37
|
my ( $s, $app ) = @_; |
13
|
1
|
|
|
|
|
8
|
$app->log->debug( "Loading " . __PACKAGE__ ); |
14
|
|
|
|
|
|
|
|
15
|
1
|
|
|
|
|
60
|
my $class = ref $app; |
16
|
1
|
50
|
0
|
|
|
3
|
$class = eval { $app->renderer->classes->[0] } // 'main' |
|
0
|
|
|
|
|
0
|
|
17
|
|
|
|
|
|
|
if $class eq 'Mojolicious::Lite'; |
18
|
1
|
|
50
|
|
|
18
|
my $version = $class->VERSION // 'dev'; |
19
|
|
|
|
|
|
|
|
20
|
1
|
|
|
|
|
7
|
my $r = $app->routes; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
$r->get('/version')->to( |
23
|
|
|
|
|
|
|
cb => sub { |
24
|
|
|
|
|
|
|
shift->render( |
25
|
1
|
|
|
1
|
|
14167
|
json => { |
26
|
|
|
|
|
|
|
class => $class, |
27
|
|
|
|
|
|
|
version => $version |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
); |
30
|
|
|
|
|
|
|
} |
31
|
1
|
|
|
|
|
12
|
); |
32
|
|
|
|
|
|
|
|
33
|
1
|
|
|
|
|
417
|
my ( $app_name, $hostname ) = do { |
34
|
1
|
|
|
|
|
2
|
my $name = ref $app; |
35
|
1
|
50
|
|
|
|
4
|
$name = curfile if $name eq 'Mojolicious::Lite'; |
36
|
1
|
|
|
|
|
4
|
( $name, hostname() ); |
37
|
|
|
|
|
|
|
}; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
$r->get('/status')->to( |
40
|
|
|
|
|
|
|
cb => sub { |
41
|
1
|
|
|
1
|
|
10878
|
my $s = shift; |
42
|
1
|
|
|
|
|
6
|
$s->render( |
43
|
|
|
|
|
|
|
json => { |
44
|
|
|
|
|
|
|
app_name => $app_name, |
45
|
|
|
|
|
|
|
server => { |
46
|
|
|
|
|
|
|
version => $version, |
47
|
|
|
|
|
|
|
hostname => $hostname, |
48
|
|
|
|
|
|
|
url => $s->url_for('/')->to_abs->to_string, |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
); |
52
|
|
|
|
|
|
|
} |
53
|
1
|
|
|
|
|
388
|
); |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
my $get_routes = sub() { |
56
|
8
|
|
100
|
8
|
|
91
|
my $verbose = shift || ''; |
57
|
8
|
|
|
|
|
13
|
my $rows = []; |
58
|
8
|
|
|
|
|
15
|
_walk( $_, 0, $rows, $verbose ) for @{ $app->routes->children }; |
|
8
|
|
|
|
|
16
|
|
59
|
8
|
|
|
|
|
34
|
return $rows; |
60
|
1
|
|
|
|
|
265
|
}; |
61
|
|
|
|
|
|
|
|
62
|
1
|
|
|
|
|
4
|
my $root_with_format = |
63
|
|
|
|
|
|
|
$r->get( '/' => [ format => [ 'json', 'html', 'txt' ] ] ); |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
$root_with_format->get( '/routes/:verbose' => { verbose => '' } )->to( |
66
|
|
|
|
|
|
|
format => undef, |
67
|
|
|
|
|
|
|
cb => sub { |
68
|
4
|
|
|
4
|
|
43620
|
my $s = shift; |
69
|
4
|
|
|
|
|
15
|
$s->respond_to( |
70
|
|
|
|
|
|
|
any => { json => $get_routes->( $s->stash('verbose') ) }, |
71
|
|
|
|
|
|
|
txt => |
72
|
|
|
|
|
|
|
{ text => tablify( $get_routes->( $s->stash('verbose') ) ) } |
73
|
|
|
|
|
|
|
); |
74
|
|
|
|
|
|
|
} |
75
|
1
|
|
|
|
|
159
|
); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub _walk { |
79
|
32
|
|
|
32
|
|
136
|
my ( $route, $depth, $rows, $verbose ) = @_; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# Pattern |
82
|
32
|
|
|
|
|
48
|
my $prefix = ''; |
83
|
32
|
100
|
|
|
|
60
|
if ( my $i = $depth * 2 ) { $prefix .= ' ' x $i . '+' } |
|
8
|
|
|
|
|
18
|
|
84
|
32
|
|
100
|
|
|
62
|
push @$rows, my $row = [ $prefix . ( $route->pattern->unparsed || '/' ) ]; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# Flags |
87
|
32
|
|
|
|
|
212
|
my @flags; |
88
|
32
|
50
|
50
|
|
|
37
|
push @flags, @{ $route->requires // [] } ? 'C' : '.'; |
|
32
|
|
|
|
|
58
|
|
89
|
32
|
50
|
|
|
|
224
|
push @flags, ( my $partial = $route->partial ) ? 'P' : '.'; |
90
|
32
|
50
|
|
|
|
118
|
push @flags, $route->inline ? 'U' : '.'; |
91
|
32
|
50
|
|
|
|
110
|
push @flags, $route->is_websocket ? 'W' : '.'; |
92
|
32
|
100
|
|
|
|
160
|
push @$row, join( '', @flags ) if $verbose; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# Methods |
95
|
32
|
|
|
|
|
58
|
my $methods = $route->methods; |
96
|
32
|
50
|
|
|
|
167
|
push @$row, !$methods ? '*' : uc join ',', @$methods; |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
# Name |
99
|
32
|
|
|
|
|
54
|
my $name = $route->name; |
100
|
32
|
50
|
|
|
|
143
|
push @$row, $route->has_custom_name ? qq{"$name"} : $name; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# Regex (verbose) |
103
|
32
|
|
|
|
|
122
|
my $pattern = $route->pattern; |
104
|
32
|
|
66
|
|
|
133
|
$pattern->match( '/', $route->is_endpoint && !$partial ); |
105
|
32
|
100
|
|
|
|
826
|
push @$row, ( regexp_pattern $pattern->regex )[0] if $verbose; |
106
|
|
|
|
|
|
|
|
107
|
32
|
|
|
|
|
90
|
$depth++; |
108
|
32
|
|
|
|
|
36
|
_walk( $_, $depth, $rows, $verbose ) for @{ $route->children }; |
|
32
|
|
|
|
|
47
|
|
109
|
32
|
|
|
|
|
124
|
$depth--; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
1; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
__END__ |