line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojo::Leds::Plugin::CommonRoutes; |
2
|
|
|
|
|
|
|
$Mojo::Leds::Plugin::CommonRoutes::VERSION = '1.13'; |
3
|
|
|
|
|
|
|
# ABSTRACT: Add routes to get app informations (library version, routes,...) |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
1142
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
4
|
|
6
|
1
|
|
|
1
|
|
121
|
use Mojo::File qw/curfile/; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
44
|
|
7
|
1
|
|
|
1
|
|
6
|
use Net::Domain qw(hostname); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
53
|
|
8
|
1
|
|
|
1
|
|
6
|
use re qw(regexp_pattern); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
118
|
|
9
|
1
|
|
|
1
|
|
4
|
use Mojo::Util qw(encode getopt tablify); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
700
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub register { |
12
|
1
|
|
|
1
|
1
|
27
|
my ( $s, $app ) = @_; |
13
|
1
|
|
|
|
|
7
|
$app->log->debug( "Loading " . __PACKAGE__ ); |
14
|
|
|
|
|
|
|
|
15
|
1
|
|
|
|
|
55
|
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
|
|
|
14
|
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
|
|
12397
|
json => { |
26
|
|
|
|
|
|
|
class => $class, |
27
|
|
|
|
|
|
|
version => $version |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
); |
30
|
|
|
|
|
|
|
} |
31
|
1
|
|
|
|
|
12
|
); |
32
|
|
|
|
|
|
|
|
33
|
1
|
|
|
|
|
413
|
my ( $app_name, $hostname ) = do { |
34
|
1
|
|
|
|
|
2
|
my $name = ref $app; |
35
|
1
|
50
|
|
|
|
5
|
$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
|
|
9055
|
my $s = shift; |
42
|
1
|
|
|
|
|
5
|
$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
|
|
|
|
|
408
|
); |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
my $get_routes = sub() { |
56
|
8
|
|
100
|
8
|
|
77
|
my $verbose = shift || ''; |
57
|
8
|
|
|
|
|
13
|
my $rows = []; |
58
|
8
|
|
|
|
|
12
|
_walk( $_, 0, $rows, $verbose ) for @{ $app->routes->children }; |
|
8
|
|
|
|
|
16
|
|
59
|
8
|
|
|
|
|
29
|
return $rows; |
60
|
1
|
|
|
|
|
228
|
}; |
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
|
|
33591
|
my $s = shift; |
69
|
4
|
|
|
|
|
11
|
$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
|
|
|
|
|
137
|
); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub _walk { |
79
|
32
|
|
|
32
|
|
122
|
my ( $route, $depth, $rows, $verbose ) = @_; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# Pattern |
82
|
32
|
|
|
|
|
36
|
my $prefix = ''; |
83
|
32
|
100
|
|
|
|
50
|
if ( my $i = $depth * 2 ) { $prefix .= ' ' x $i . '+' } |
|
8
|
|
|
|
|
17
|
|
84
|
32
|
|
100
|
|
|
52
|
push @$rows, my $row = [ $prefix . ( $route->pattern->unparsed || '/' ) ]; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# Flags |
87
|
32
|
|
|
|
|
191
|
my @flags; |
88
|
32
|
50
|
50
|
|
|
32
|
push @flags, @{ $route->requires // [] } ? 'C' : '.'; |
|
32
|
|
|
|
|
50
|
|
89
|
32
|
50
|
|
|
|
196
|
push @flags, ( my $partial = $route->partial ) ? 'P' : '.'; |
90
|
32
|
50
|
|
|
|
103
|
push @flags, $route->inline ? 'U' : '.'; |
91
|
32
|
50
|
|
|
|
114
|
push @flags, $route->is_websocket ? 'W' : '.'; |
92
|
32
|
100
|
|
|
|
132
|
push @$row, join( '', @flags ) if $verbose; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# Methods |
95
|
32
|
|
|
|
|
50
|
my $methods = $route->methods; |
96
|
32
|
50
|
|
|
|
163
|
push @$row, !$methods ? '*' : uc join ',', @$methods; |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
# Name |
99
|
32
|
|
|
|
|
70
|
my $name = $route->name; |
100
|
32
|
50
|
|
|
|
129
|
push @$row, $route->has_custom_name ? qq{"$name"} : $name; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# Regex (verbose) |
103
|
32
|
|
|
|
|
114
|
my $pattern = $route->pattern; |
104
|
32
|
|
66
|
|
|
105
|
$pattern->match( '/', $route->is_endpoint && !$partial ); |
105
|
32
|
100
|
|
|
|
737
|
push @$row, ( regexp_pattern $pattern->regex )[0] if $verbose; |
106
|
|
|
|
|
|
|
|
107
|
32
|
|
|
|
|
72
|
$depth++; |
108
|
32
|
|
|
|
|
31
|
_walk( $_, $depth, $rows, $verbose ) for @{ $route->children }; |
|
32
|
|
|
|
|
41
|
|
109
|
32
|
|
|
|
|
112
|
$depth--; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
1; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
__END__ |