line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojo::Leds::Plugin::CommonRoutes; |
2
|
|
|
|
|
|
|
$Mojo::Leds::Plugin::CommonRoutes::VERSION = '1.14'; |
3
|
|
|
|
|
|
|
# ABSTRACT: Add routes to get app informations (library version, routes,...) |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
1322
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
5
|
|
6
|
1
|
|
|
1
|
|
175
|
use Mojo::File qw/curfile/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
61
|
|
7
|
1
|
|
|
1
|
|
7
|
use Net::Domain qw(hostname); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
57
|
|
8
|
1
|
|
|
1
|
|
7
|
use re qw(regexp_pattern); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
110
|
|
9
|
1
|
|
|
1
|
|
5
|
use Mojo::Util qw(encode getopt tablify); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
823
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub register { |
12
|
1
|
|
|
1
|
1
|
37
|
my ( $s, $app ) = @_; |
13
|
1
|
|
|
|
|
7
|
$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
|
|
|
16
|
my $version = $class->VERSION // 'dev'; |
19
|
|
|
|
|
|
|
|
20
|
1
|
|
|
|
|
6
|
my $r = $app->routes; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
$r->get('/version')->to( |
23
|
|
|
|
|
|
|
cb => sub { |
24
|
|
|
|
|
|
|
shift->render( |
25
|
1
|
|
|
1
|
|
14248
|
json => { |
26
|
|
|
|
|
|
|
class => $class, |
27
|
|
|
|
|
|
|
version => $version |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
); |
30
|
|
|
|
|
|
|
} |
31
|
1
|
|
|
|
|
12
|
); |
32
|
|
|
|
|
|
|
|
33
|
1
|
|
|
|
|
476
|
my ( $app_name, $hostname ) = do { |
34
|
1
|
|
|
|
|
4
|
my $name = ref $app; |
35
|
1
|
50
|
|
|
|
5
|
$name = curfile if $name eq 'Mojolicious::Lite'; |
36
|
1
|
|
|
|
|
5
|
( $name, hostname() ); |
37
|
|
|
|
|
|
|
}; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
$r->get('/status')->to( |
40
|
|
|
|
|
|
|
cb => sub { |
41
|
1
|
|
|
1
|
|
11074
|
my $s = shift; |
42
|
1
|
|
|
|
|
13
|
$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
|
|
|
|
|
385
|
); |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
my $get_routes = sub() { |
56
|
8
|
|
100
|
8
|
|
147
|
my $verbose = shift || ''; |
57
|
8
|
|
|
|
|
13
|
my $rows = []; |
58
|
8
|
|
|
|
|
12
|
_walk( $_, 0, $rows, $verbose ) for @{ $app->routes->children }; |
|
8
|
|
|
|
|
21
|
|
59
|
8
|
|
|
|
|
33
|
return $rows; |
60
|
1
|
|
|
|
|
261
|
}; |
61
|
|
|
|
|
|
|
|
62
|
1
|
|
|
|
|
5
|
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
|
|
42997
|
my $s = shift; |
69
|
4
|
|
|
|
|
12
|
$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
|
|
|
|
|
156
|
); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub _walk { |
79
|
32
|
|
|
32
|
|
129
|
my ( $route, $depth, $rows, $verbose ) = @_; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# Pattern |
82
|
32
|
|
|
|
|
40
|
my $prefix = ''; |
83
|
32
|
100
|
|
|
|
62
|
if ( my $i = $depth * 2 ) { $prefix .= ' ' x $i . '+' } |
|
8
|
|
|
|
|
26
|
|
84
|
32
|
|
100
|
|
|
61
|
push @$rows, my $row = [ $prefix . ( $route->pattern->unparsed || '/' ) ]; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# Flags |
87
|
32
|
|
|
|
|
215
|
my @flags; |
88
|
32
|
50
|
50
|
|
|
36
|
push @flags, @{ $route->requires // [] } ? 'C' : '.'; |
|
32
|
|
|
|
|
61
|
|
89
|
32
|
50
|
|
|
|
210
|
push @flags, ( my $partial = $route->partial ) ? 'P' : '.'; |
90
|
32
|
50
|
|
|
|
115
|
push @flags, $route->inline ? 'U' : '.'; |
91
|
32
|
50
|
|
|
|
113
|
push @flags, $route->is_websocket ? 'W' : '.'; |
92
|
32
|
100
|
|
|
|
159
|
push @$row, join( '', @flags ) if $verbose; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# Methods |
95
|
32
|
|
|
|
|
59
|
my $methods = $route->methods; |
96
|
32
|
50
|
|
|
|
166
|
push @$row, !$methods ? '*' : uc join ',', @$methods; |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
# Name |
99
|
32
|
|
|
|
|
56
|
my $name = $route->name; |
100
|
32
|
50
|
|
|
|
149
|
push @$row, $route->has_custom_name ? qq{"$name"} : $name; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# Regex (verbose) |
103
|
32
|
|
|
|
|
124
|
my $pattern = $route->pattern; |
104
|
32
|
|
66
|
|
|
110
|
$pattern->match( '/', $route->is_endpoint && !$partial ); |
105
|
32
|
100
|
|
|
|
875
|
push @$row, ( regexp_pattern $pattern->regex )[0] if $verbose; |
106
|
|
|
|
|
|
|
|
107
|
32
|
|
|
|
|
85
|
$depth++; |
108
|
32
|
|
|
|
|
37
|
_walk( $_, $depth, $rows, $verbose ) for @{ $route->children }; |
|
32
|
|
|
|
|
55
|
|
109
|
32
|
|
|
|
|
134
|
$depth--; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
1; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
__END__ |