| 1 | package Chouette; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use common::sense; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use EV; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use AnyEvent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use AnyEvent::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use AnyEvent::Task::Client; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use AnyEvent::Task::Server; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Feersum; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Callback::Frame; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Log::File::Rolling; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Cwd; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Regexp::Assemble; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use Session::Token; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use Chouette::Context; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 1 | our $VERSION = '0.101'; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | sub new { | ||||||||||||||||||||||||||||
| 25 | 1 | ▶ | my ($class, $app_spec) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | my $self = { | |||||||||||||||||||||||||||
| 28 | app_spec => $app_spec, | ||||||||||||||||||||||||||||
| 29 | }; | ||||||||||||||||||||||||||||
| 30 | 1 | bless $self, $class; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 1 | my $config = {}; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 1 | ▶ | if ($app_spec->{config_file}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 0 | require YAML; | |||||||||||||||||||||||||||
| 36 | 0 | $config = YAML::LoadFile($app_spec->{config_file}); | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | $self->{config} = { | ||||||||||||||||||||||||||||
| 40 | 1 | %{ $app_spec->{config_defaults} }, | |||||||||||||||||||||||||||
| 41 | %$config, | ||||||||||||||||||||||||||||
| 42 | }; | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 1 | $self->{quiet} = $app_spec->{quiet}; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 1 | $self->_validate_config(); | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 1 | $self->_compile_app(); | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 1 | $self->{_done_gensym} = \''; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 1 | return $self; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | sub _validate_config { | ||||||||||||||||||||||||||||
| 59 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_validate_config: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 1 | ▶ | die "var_dir $self->{config}->{var_dir} is not a directory" if !-e $self->{config}->{var_dir}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | sub _compile_app { | ||||||||||||||||||||||||||||
| 66 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_compile_app: called CC=14 SCAR=41.2
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | ## Middleware | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 1 | foreach my $middleware_spec (@{ $self->{app_spec}->{middleware} }) { | |||||||||||||||||||||||||||
| 71 | 0 | ▶ | $middleware_spec = [ $middleware_spec ] if !ref($middleware_spec); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | $middleware_spec = [ @$middleware_spec ]; ## copy so don't destroy app_spec version | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | my $pkg = $middleware_spec->[0]; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | if ($pkg =~ m{^Plack::Middleware::}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | eval "require $pkg" || die "Couldn't require middleware $pkg\n\n$@"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | } else { | ||||||||||||||||||||||||||||
| 79 | 0 | ▶ | if (!eval "require $pkg") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | my $new_pkg = "Plack::Middleware::" . $pkg; | |||||||||||||||||||||||||||
| 81 | 0 | ▶ | eval "require $new_pkg" || die "Couldn't require middleware $pkg (or $new_pkg)"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | $middleware_spec->[0] = $new_pkg; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | push @{ $self->{middleware_specs} }, $middleware_spec; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | ## Pre-route wrappers | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 1 | ▶ | if (defined $self->{app_spec}->{pre_route}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 0 | $self->{pre_route_cb} = $self->_load_function($self->{app_spec}->{pre_route}, "pre-route"); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | ## Routes | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 1 | $self->{route_regexp_assemble} = Regexp::Assemble->new->track(1); | |||||||||||||||||||||||||||
| 100 | 1 | $self->{route_patterns} = {}; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 1 | my $routes = $self->{app_spec}->{routes}; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 1 | foreach my $route (keys %$routes) { | |||||||||||||||||||||||||||
| 105 | 1 | my $re = '\A' . $route . '\z'; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 1 | $re =~ s{/}{\\/}g; ## Hack for Regexp::Assemble: https://github.com/ronsavage/Regexp-Assemble/issues/4 | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 1 | $re =~ s{:([\w]+)}{(?<$1>[^/]+)}; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 1 | $self->{route_regexp_assemble}->add($re); | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 1 | my $methods = {}; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 1 | foreach my $method (keys %{ $routes->{$route} }) { | |||||||||||||||||||||||||||
| 116 | 1 | $methods->{$method} = $self->_load_function($routes->{$route}->{$method}, "route: $method $route"); | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 1 | $self->{route_patterns}->{$re} = $methods; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 1 | $self->{route_regexp} = $self->{route_regexp_assemble}->re; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | ## Tasks | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 1 | foreach my $task_name (keys %{ $self->{app_spec}->{tasks} }) { | |||||||||||||||||||||||||||
| 128 | 0 | ▶ | die "invalid task name: $task_name" if $task_name !~ /\A\w+\z/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | my $task = $self->{app_spec}->{tasks}->{$task_name}; | |||||||||||||||||||||||||||
| 131 | 0 | my $pkg = $task->{pkg}; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | ▶ | eval "require $pkg" || die "Couldn't require task package $pkg (required for task $task_name)\n\n$@"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 0 | ▶ | die "Couldn't find function new in $pkg (needed task $task_name)" if !defined &{ "${pkg}::new" }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | sub _load_function { | ||||||||||||||||||||||||||||
| 141 | 1 | ▶ | my ($self, $spec, $needed_for) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_load_function: called CC=6 SCAR=29.9
| |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 1 | ▶ | $needed_for = "(needed for $needed_for)" if defined $needed_for; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 1 | ▶ | if (ref $spec eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 146 | 1 | return $spec; | |||||||||||||||||||||||||||
| 147 | } elsif ($spec =~ /^(.*)::([^:]+)$/) { | ||||||||||||||||||||||||||||
| 148 | 0 | my ($pkg, $func_name) = ($1, $2); | |||||||||||||||||||||||||||
| 149 | 0 | ▶ | eval "require $pkg" || die "Couldn't require $pkg $needed_for\n\n$@"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 0 | ▶ | die "Couldn't find function $func_name in $pkg $needed_for" if !defined &{ "${pkg}::${func_name}" }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 0 | my $func = \&{ "${pkg}::${func_name}" }; | |||||||||||||||||||||||||||
| 152 | 0 | return $func; | |||||||||||||||||||||||||||
| 153 | } else { | ||||||||||||||||||||||||||||
| 154 | 0 | die "couldn't parse function: '$spec'"; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | sub _listen { | ||||||||||||||||||||||||||||
| 161 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_listen: called CC=6 SCAR=25.7
| |||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 1 | my $listen = $self->{config}->{listen}; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 1 | my $socket; | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 1 | ▶ | if ($listen =~ m{^unix:(.*)}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 1 | my $socket_file = $1; | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 1 | require IO::Socket::UNIX; | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 1 | unlink($socket_file); | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 1 | ▶ | $socket = IO::Socket::UNIX->new( | ||||||||||||||||||||||||||
|
Condition50%1/2
'IO::Socket::UNIX'->new('Listen', 5, 'Type', 1, 'Local', $socket_file) || die("unable to listen on $listen : $!")
MC/DC0%0/1
'IO::Socket::UNIX'->new('Listen', 5, 'Type', 1, 'Local', $socket_file) || die("unable to listen on $listen : $!")
'IO::Socket::UNIX'->new('Listen', 5, 'Type', 1, 'Local', $socket_file)
Truth table50%1/2
A
A= 'IO::Socket::UNIX'->new('Listen', 5, 'Type', 1, 'Local', $socket_file)
| |||||||||||||||||||||||||||||
| 175 | Listen => 5, | ||||||||||||||||||||||||||||
| 176 | Type => IO::Socket::SOCK_STREAM(), | ||||||||||||||||||||||||||||
| 177 | Local => $socket_file, | ||||||||||||||||||||||||||||
| 178 | ) || die "unable to listen on $listen : $!"; | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 1 | $self->{_friendly_socket_desc} = "http://[unix:$socket_file]"; | |||||||||||||||||||||||||||
| 181 | } else { | ||||||||||||||||||||||||||||
| 182 | 0 | my $local_addr = '0.0.0.0'; | |||||||||||||||||||||||||||
| 183 | 0 | my $port; | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 0 | ▶ | if ($listen =~ m{^(.*):(\d+)$}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 186 | 0 | $local_addr = $1; | |||||||||||||||||||||||||||
| 187 | 0 | $port = $2; | |||||||||||||||||||||||||||
| 188 | } elsif ($listen =~ m{^(\d+)$}) { | ||||||||||||||||||||||||||||
| 189 | 0 | $port = $1; | |||||||||||||||||||||||||||
| 190 | } else { | ||||||||||||||||||||||||||||
| 191 | 0 | die "unable to parse listen param: '$listen'"; | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 0 | require IO::Socket::INET; | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 0 | ▶ | $socket = IO::Socket::INET->new( | ||||||||||||||||||||||||||
|
Condition0%0/2
'IO::Socket::INET'->new('Listen', 5, 'Proto', 'tcp', 'LocalAddr', $local_addr, 'LocalPort', $port, 'ReuseAddr', 1) || die("unable to listen on $listen : $!")
MC/DC0%0/1
'IO::Socket::INET'->new('Listen', 5, 'Proto', 'tcp', 'LocalAddr', $local_addr, 'LocalPort', $port, 'ReuseAddr', 1) || die("unable to listen on $listen : $!")
'IO::Socket::INET'->new('Listen', 5, 'Proto', 'tcp', 'LocalAddr', $local_addr, 'LocalPort', $port, 'ReuseAddr', 1)
Truth table0%0/2
A
A= 'IO::Socket::INET'->new('Listen', 5, 'Proto', 'tcp', 'LocalAddr', $local_addr, 'LocalPort', $port, 'ReuseAddr', 1)
| |||||||||||||||||||||||||||||
| 197 | Listen => 5, | ||||||||||||||||||||||||||||
| 198 | Proto => 'tcp', | ||||||||||||||||||||||||||||
| 199 | LocalAddr => $local_addr, | ||||||||||||||||||||||||||||
| 200 | LocalPort => $port, | ||||||||||||||||||||||||||||
| 201 | ReuseAddr => 1, | ||||||||||||||||||||||||||||
| 202 | ) || die "unable to listen on $listen : $!"; | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 0 | $self->{_friendly_socket_desc} = "http://$local_addr:$port"; | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 1 | AnyEvent::Util::fh_nonblocking($socket, 1); | |||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 1 | $self->{accept_socket} = $socket; | |||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | sub _logging { | ||||||||||||||||||||||||||||
| 213 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_logging: called CC=6 SCAR=19.0
| |||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 1 | my $log_dir = "$self->{config}->{var_dir}/logs"; | |||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 1 | ▶ | if (!-e $log_dir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 218 | 1 | ▶ | mkdir($log_dir) || die "couldn't mkdir($log_dir): $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 1 | $log_dir = Cwd::abs_path($log_dir); | |||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 1 | ▶ | my $app_name = $self->{config}->{logging}->{file_prefix} // 'app'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"config"}{"logging"}{"file_prefix"} // "app"
MC/DC0%0/1
$self->{"config"}{"logging"}{"file_prefix"} // "app"
$self->{"config"}{"logging"}{"file_prefix"}
Truth table50%1/2
A
A= $self->{"config"}{"logging"}{"file_prefix"}
| |||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 1 | my $curr_symlink = "$log_dir/$app_name.current.log"; | |||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | $self->{raw_logger} = Log::File::Rolling->new( | ||||||||||||||||||||||||||||
| 228 | filename => "$log_dir/$app_name.%Y-%m-%dT%H.log", | ||||||||||||||||||||||||||||
| 229 | current_symlink => $curr_symlink, | ||||||||||||||||||||||||||||
| 230 | 1 | ▶ | timezone => ($self->{config}->{logging}->{timezone} // 'gmtime'), | ||||||||||||||||||||||||||
|
Condition50%2/4
'Log::File::Rolling'->new('filename', "$log_dir/$app_name.%Y-%m-%dT%H.log", 'current_symlink', $curr_symlink, 'timezone', $self->{'config'}{'logging'}{'timezone'} // 'gmtime') || die("Error creating Log::File::Rolling logger: $!")
$self->{"config"}{"logging"}{"timezone"} // "gmtime"
MC/DC0%0/1
'Log::File::Rolling'->new('filename', "$log_dir/$app_name.%Y-%m-%dT%H.log", 'current_symlink', $curr_symlink, 'timezone', $self->{'config'}{'logging'}{'timezone'} // 'gmtime') || die("Error creating Log::File::Rolling logger: $!")
'Log::File::Rolling'->new('filename', "$log_dir/$app_name.%Y-%m-%dT%H.log", 'current_symlink', $curr_symlink, 'timezone', $self->{'config'}{'logging'}{'timezone'} // 'gmtime')
MC/DC0%0/1
$self->{"config"}{"logging"}{"timezone"} // "gmtime"
$self->{"config"}{"logging"}{"timezone"}
Truth table50%1/2
A
A= 'Log::File::Rolling'->new('filename', "$log_dir/$app_name.%Y-%m-%dT%H.log", 'current_symlink', $curr_symlink, 'timezone', $self->{'config'}{'logging'}{'timezone'} // 'gmtime')
Truth table50%1/2
A
A= $self->{"config"}{"logging"}{"timezone"}
| |||||||||||||||||||||||||||||
| 231 | ) || die "Error creating Log::File::Rolling logger: $!"; | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | 1 | $self->{_friendly_current_logfile} = $curr_symlink; | |||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | sub _start_task_servers { | ||||||||||||||||||||||||||||
| 238 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_start_task_servers: called CC=7 SCAR=31.3
| |||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | 1 | my $task_dir = "$self->{config}->{var_dir}/tasks"; | |||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | 1 | ▶ | if ($self->{app_spec}->{tasks}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 243 | 1 | ▶ | if (!-e $task_dir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 244 | 1 | ▶ | mkdir($task_dir) || die "couldn't mkdir($task_dir): $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 1 | foreach my $task_name (keys %{ $self->{app_spec}->{tasks} }) { | |||||||||||||||||||||||||||
| 249 | 0 | my $task = $self->{app_spec}->{tasks}->{$task_name}; | |||||||||||||||||||||||||||
| 250 | 0 | my $pkg = $task->{pkg}; | |||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | 0 | my $obj; | |||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | 0 | my $constructor_func = \&{ "${pkg}::new" }; | |||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | 0 | my $checkout_done; | |||||||||||||||||||||||||||
| 257 | 0 | ▶ | $checkout_done = \&{ "${pkg}::CHECKOUT_DONE" } if defined &{ "${pkg}::CHECKOUT_DONE" }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | AnyEvent::Task::Server::fork_task_server( | ||||||||||||||||||||||||||||
| 260 | listen => ['unix/', "$task_dir/$task_name.socket"], | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | setup => sub { | ||||||||||||||||||||||||||||
| 263 | 0 | ▶ | $obj = $constructor_func->($pkg, $self->{config}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 264 | }, | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | interface => sub { | ||||||||||||||||||||||||||||
| 267 | 0 | ▶ | my ($method, @args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 268 | 0 | $obj->$method(@args); | |||||||||||||||||||||||||||
| 269 | }, | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | $checkout_done ? ( | ||||||||||||||||||||||||||||
| 272 | checkout_done => sub { | ||||||||||||||||||||||||||||
| 273 | 0 | ▶ | $checkout_done->($obj); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 274 | }, | ||||||||||||||||||||||||||||
| 275 | ) : (), | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | 0 | ▶ | %{ $task->{server} }, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 278 | ); | ||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | sub _start_task_clients { | ||||||||||||||||||||||||||||
| 284 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_start_task_clients: called CC=3 SCAR=16.0
| |||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | 1 | my $task_dir = "$self->{config}->{var_dir}/tasks"; | |||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | 1 | foreach my $task_name (keys %{ $self->{app_spec}->{tasks} }) { | |||||||||||||||||||||||||||
| 289 | 0 | my $task = $self->{app_spec}->{tasks}->{$task_name}; | |||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | $self->{task_clients}->{$task_name} = AnyEvent::Task::Client->new( | ||||||||||||||||||||||||||||
| 292 | connect => ['unix/', "$task_dir/$task_name.socket"], | ||||||||||||||||||||||||||||
| 293 | 0 | %{ $task->{client} }, | |||||||||||||||||||||||||||
| 294 | ); | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | 0 | ▶ | $self->{task_checkout_caching}->{$task_name} = 1 if $self->{app_spec}->{tasks}->{$task_name}->{checkout_caching}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | sub serve { | ||||||||||||||||||||||||||||
| 303 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
serve: called CC=3 SCAR=13.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | 1 | $self->{_serving} = 1; | |||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | 1 | $self->_start_task_servers(); | |||||||||||||||||||||||||||
| 308 | 1 | $self->_start_task_clients(); | |||||||||||||||||||||||||||
| 309 | 1 | $self->_listen(); | |||||||||||||||||||||||||||
| 310 | 1 | $self->_logging(); | |||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | 1 | $self->{feersum} = Feersum->endjinn; | |||||||||||||||||||||||||||
| 313 | 1 | $self->{feersum}->use_socket($self->{accept_socket}); | |||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | my $app = sub { | ||||||||||||||||||||||||||||
| 316 | 1 | ▶ | my $env = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | return sub { | ||||||||||||||||||||||||||||
| 319 | 1 | ▶ | my $responder = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | 1 | my $c = Chouette::Context->new( | |||||||||||||||||||||||||||
| 322 | chouette => $self, | ||||||||||||||||||||||||||||
| 323 | env => $env, | ||||||||||||||||||||||||||||
| 324 | responder => $responder, | ||||||||||||||||||||||||||||
| 325 | ); | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | 1 | $self->_handle_request($c); | |||||||||||||||||||||||||||
| 328 | 1 | }; | |||||||||||||||||||||||||||
| 329 | 1 | }; | |||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | 1 | foreach my $middleware_spec (@{ $self->{middleware_specs} }) { | |||||||||||||||||||||||||||
| 332 | 0 | my @s = @$middleware_spec; | |||||||||||||||||||||||||||
| 333 | 0 | my $pkg = shift(@s); | |||||||||||||||||||||||||||
| 334 | 0 | $app = $pkg->wrap($app, @s); | |||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | 1 | $self->{feersum}->psgi_request_handler($app); | |||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | 1 | ▶ | return if $self->{quiet}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | 0 | say "="x79; | |||||||||||||||||||||||||||
| 342 | 0 | say; | |||||||||||||||||||||||||||
| 343 | 0 | say "Chouette $VERSION"; | |||||||||||||||||||||||||||
| 344 | 0 | say; | |||||||||||||||||||||||||||
| 345 | 0 | say "PID = $$"; | |||||||||||||||||||||||||||
| 346 | 0 | say "UID/GIDs = $</$("; | |||||||||||||||||||||||||||
| 347 | 0 | say "Listening on: $self->{_friendly_socket_desc}"; | |||||||||||||||||||||||||||
| 348 | 0 | say; | |||||||||||||||||||||||||||
| 349 | 0 | say "Follow log messages:"; | |||||||||||||||||||||||||||
| 350 | 0 | say " log-defer-viz -F $self->{_friendly_current_logfile}"; | |||||||||||||||||||||||||||
| 351 | 0 | say; | |||||||||||||||||||||||||||
| 352 | 0 | say "="x79; | |||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | sub run { | ||||||||||||||||||||||||||||
| 357 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
run: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | 0 | ▶ | $self->serve unless $self->{_serving}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | 0 | AE::cv->recv; | |||||||||||||||||||||||||||
| 362 | } | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | sub _handle_request { | ||||||||||||||||||||||||||||
| 366 | 1 | ▶ | my ($self, $c) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_handle_request: called CC=1 SCAR=3.6
| |||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | 1 | my $req = $c->req; | |||||||||||||||||||||||||||
| 369 | 1 | $c->logger->info("Request from " . $req->address . " : " . $req->method . " " . $req->path); | |||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | frame_try { | ||||||||||||||||||||||||||||
| 372 | 1 | ▶ | if ($self->{pre_route_cb}) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=10.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 373 | 0 | ▶ | my $pre_route_cb = fub { $self->{pre_route_cb}->(@_) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 374 | 0 | ▶ | $pre_route_cb->($c, fub { $self->_do_routing($c) }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 375 | } else { | ||||||||||||||||||||||||||||
| 376 | 1 | $self->_do_routing($c); | |||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||
| 378 | } frame_catch { | ||||||||||||||||||||||||||||
| 379 | 1 | ▶ | my $err = $@; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=37.0
| |||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | 1 | ▶ | return if ref($err) && ($err + 0 == $c->{chouette}->{_done_gensym} + 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $err and $err + 0 == $c->{"chouette"}{"_done_gensym"} + 0
MC/DC0%0/2
ref $err and $err + 0 == $c->{"chouette"}{"_done_gensym"} + 0
ref $err
$err + 0 == $c->{"chouette"}{"_done_gensym"} + 0
Truth table33%1/3
A and B
A= ref $errB= $err + 0 == $c->{"chouette"}{"_done_gensym"} + 0
| |||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | 0 | ▶ | if ($err =~ /^(\d\d\d)\b(?:\s*:\s*)?(.*)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 384 | 0 | my $status = $1; | |||||||||||||||||||||||||||
| 385 | 0 | my $msg = $2; | |||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | 0 | my $body = {}; | |||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | 0 | ▶ | if ($status < 200 || $status >= 400) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$status < 200 or $status >= 400
MC/DC0%0/2
$status < 200 or $status >= 400
$status < 200
$status >= 400
Truth table0%0/3
A or B
A= $status < 200B= $status >= 400
| |||||||||||||||||||||||||||||
| 390 | 0 | $c->logger->warn("threw $err"); | |||||||||||||||||||||||||||
| 391 | 0 | ▶ | if (length($msg)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 392 | 0 | $msg =~ s/ at \S+ line \d+\.$//; | |||||||||||||||||||||||||||
| 393 | 0 | $body->{error} = $msg; | |||||||||||||||||||||||||||
| 394 | } else { | ||||||||||||||||||||||||||||
| 395 | 0 | $msg = "HTTP code $status"; | |||||||||||||||||||||||||||
| 396 | } | ||||||||||||||||||||||||||||
| 397 | } else { | ||||||||||||||||||||||||||||
| 398 | 0 | $msg =~ s/ at \S+ line \d+\.$//; | |||||||||||||||||||||||||||
| 399 | 0 | $body->{ok} = $msg; | |||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | 0 | $c->respond($body, $status); | |||||||||||||||||||||||||||
| 403 | 0 | return; | |||||||||||||||||||||||||||
| 404 | } | ||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | 0 | $c->logger->error($err); | |||||||||||||||||||||||||||
| 407 | 0 | $c->logger->data->{stacktrace} = $_[0]; | |||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | 0 | $c->respond({ error => 'internal server error' }, 500); | |||||||||||||||||||||||||||
| 410 | 1 | }; | |||||||||||||||||||||||||||
| 411 | } | ||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | sub _do_routing { | ||||||||||||||||||||||||||||
| 415 | 1 | ▶ | my ($self, $c) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_do_routing: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | 1 | my $path = $c->{env}->{PATH_INFO}; | |||||||||||||||||||||||||||
| 418 | 1 | ▶ | $path = '/' if $path eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | 1 | ▶ | die "404: Not Found" unless $path =~ $self->{route_regexp}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | 1 | my $route_params = \%+; | |||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | 1 | my $methods = $self->{route_patterns}->{ $self->{route_regexp_assemble}->source($^R) }; | |||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | 1 | my $method = $c->{env}->{REQUEST_METHOD}; | |||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | 1 | my $func = $methods->{$method}; | |||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | 1 | ▶ | die "405: Method Not Allowed" if !$func; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | 1 | $c->{route_params} = $route_params; | |||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | 1 | $func->($c); | |||||||||||||||||||||||||||
| 435 | } | ||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | sub generate_token { | ||||||||||||||||||||||||||||
| 440 | 0 | ▶ | state $generator = Session::Token->new; | ||||||||||||||||||||||||||
|
Subroutine
generate_token: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | 0 | return $generator->get; | |||||||||||||||||||||||||||
| 443 | } | ||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | 1 | 1; | |||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | __END__ | ||||||||||||||||||||||||||||