| 1 | package Toadfarm; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 21 | ▶ | use Mojo::Base 'Mojolicious'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 21 | ▶ | use Cwd 'abs_path'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 21 | ▶ | use Data::Dumper (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 21 | ▶ | use File::Basename qw(basename dirname); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 21 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 21 | ▶ | use File::Which; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 21 | ▶ | use Mojo::File; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 21 | ▶ | use Mojo::Util qw(class_to_path monkey_patch); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 21 | ▶ | use constant DEBUG => $ENV{TOADFARM_DEBUG} ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 21 | our $VERSION = '0.84'; | |||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 21 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=5 SCAR=19.2
| |||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 21 | ▶ | $ENV{TOADFARM_ACTION} //= (@ARGV and $ARGV[0] =~ /^(reload|start|stop)$/) ? $1 : 'load'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$ENV{"TOADFARM_ACTION"} //= @ARGV && $ARGV[0] =~ /^(reload|start|stop)$/u ? $1 : "load"
@ARGV and $ARGV[0] =~ /^(reload|start|stop)$/u
MC/DC0%0/3
$ENV{"TOADFARM_ACTION"} //= @ARGV && $ARGV[0] =~ /^(reload|start|stop)$/u ? $1 : "load"
$ENV{"TOADFARM_ACTION"}
@ARGV
$ARGV[0] =~ /^(reload|start|stop)$/u
Truth table0%0/4
A //= B and C
A= $ENV{"TOADFARM_ACTION"}B= @ARGVC= $ARGV[0] =~ /^(reload|start|stop)$/u
| |||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 21 | ▶ | $ENV{MOJO_CONFIG} = $ENV{TOADFARM_CONFIG} if $ENV{TOADFARM_CONFIG}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 18 | ▶ | sub import { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=4 SCAR=14.4
| |||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 18 | ▶ | return unless grep {/^(-dsl|-init|-test)/} @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 17 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 25 | 17 | my $caller = caller; | |||||||||||||||||||||||||||||||||||||||||||||
| 26 | 17 | my $app = Toadfarm->new; | |||||||||||||||||||||||||||||||||||||||||||||
| 27 | 17 | ▶ | my $tf = $app->config->{tf} ||= {}; # internal | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$app->config->{"tf"} ||= {}
MC/DC0%0/1
$app->config->{"tf"} ||= {}
$app->config->{"tf"}
Truth table50%1/2
A
A= $app->config->{"tf"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 17 | $_->import for qw(strict warnings utf8); | |||||||||||||||||||||||||||||||||||||||||||||
| 30 | 17 | feature->import(':5.10'); | |||||||||||||||||||||||||||||||||||||||||||||
| 31 | 17 | unshift @{$app->commands->namespaces}, 'Toadfarm::Command'; | |||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||
| 33 | monkey_patch $caller, ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 7 | ▶ | app => sub {$app}, | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
app: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 35 | change_root => \&_change_root, | ||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | logging => sub { $tf->{logging}++; $app->_setup_log(@_) }, | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
Subroutine
logging: called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 6 | ▶ | mount => sub { push @{$app->config->{apps}}, @_ == 2 ? @_ : ($_[0], {}); $app }, | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 1 | ▶ | plugin => sub { push @{$app->config->{tf_plugins}}, @_ == 2 ? @_ : ($_[0], {}); $app }, | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 39 | run_as => \&_run_as, | ||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | secrets => sub { $tf->{secrets}++; $app->secrets([@_]) }, | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 5 | ▶ | start => sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=25.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 5 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 1 | ▶ | my $listen = ref $_[0] eq 'ARRAY' ? shift : undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | $app->config->{hypnotoad} = @_ > 1 ? {@_} : {%{$_[0]}} if @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 1 | ▶ | $app->config->{hypnotoad}{listen} = $listen if $listen; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 5 | ▶ | $app->moniker($class->_moniker) if $app->moniker eq 'toadfarm'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 5 | ▶ | $app->config->{hypnotoad}{pid_file} ||= $class->_pid_file($app); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$app->config->{"hypnotoad"}{"pid_file"} ||= $class->_pid_file($app)
MC/DC0%0/2
$app->config->{"hypnotoad"}{"pid_file"} ||= $class->_pid_file($app)
$app->config->{"hypnotoad"}{"pid_file"}
$class->_pid_file($app)
Truth table33%1/3
A ||= B
A= $app->config->{"hypnotoad"}{"pid_file"}B= $class->_pid_file($app)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 5 | ▶ | $app = $class->_setup_app($app) if $ENV{TOADFARM_ACTION}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 5 | Mojo::UserAgent::Server->app($app); | |||||||||||||||||||||||||||||||||||||||||||||
| 52 | 5 | warn '$config=' . Mojo::Util::dumper($app->config) if DEBUG; | |||||||||||||||||||||||||||||||||||||||||||||
| 53 | 5 | ▶ | $class->_die_on_insecure($app) unless $ENV{TOADFARM_INSECURE}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 5 | $app->start; | |||||||||||||||||||||||||||||||||||||||||||||
| 55 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 17 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 21 | ▶ | sub startup { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
startup: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 21 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 61 | 21 | ▶ | my $config = $ENV{MOJO_CONFIG} ? $self->plugin('Config') : {}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | # remember the config when hot reloading the app | ||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 21 | $ENV{TOADFARM_CONFIG} = delete $ENV{MOJO_CONFIG}; | |||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 21 | $self->{mounted} = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 67 | 21 | ▶ | $self->_setup_log($config->{log}) if $config->{log}{file}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 21 | ▶ | $self->_paths($config->{paths}) if $config->{paths}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 21 | ▶ | $self->secrets([$config->{secret}]) if $config->{secret}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 21 | ▶ | $self->secrets($config->{secrets}) if $config->{secrets}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 21 | ▶ | $self->_mount_apps(@{$config->{apps}}) if $config->{apps}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 21 | ▶ | $self->_load_plugins(@{$config->{tf_plugins}}) if $config->{tf_plugins}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 21 | ▶ | $self->_mount_root_app(delete $self->{root_app}) if $self->{root_app}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | sub _change_root { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_change_root: not called CC=5 SCAR=24.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | my @cmd = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | my $exit = -2; | |||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | return 1 if $<; # not root | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | unshift @cmd, $ENV{TOADFARM_CHROOT_BIN} || 'chroot'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition20%1/5
$ENV{'TOADFARM_CHROOT_BIN'} || 'chroot'
$ENV{'TOADFARM_CHROOT_BIN'} || 'chroot'
MC/DC0%0/1
$ENV{'TOADFARM_CHROOT_BIN'} || 'chroot'
$ENV{'TOADFARM_CHROOT_BIN'}
MC/DC0%0/1
$ENV{'TOADFARM_CHROOT_BIN'} || 'chroot'
$ENV{'TOADFARM_CHROOT_BIN'}
Truth table0%0/2
A
A= $ENV{'TOADFARM_CHROOT_BIN'}
Truth table50%1/2
A
A= $ENV{'TOADFARM_CHROOT_BIN'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | push @cmd, $^X; | |||||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | push @cmd, -I => $INC[0] if $ENV{TOADFARM_ACTION} eq 'test'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | push @cmd, File::Spec->rel2abs($0), @ARGV; | |||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | warn "[Toadfarm] system @cmd\n" if DEBUG; | |||||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | system @cmd; | |||||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | die "Could not run '@cmd' exit=$exit\n" if $exit = $? >> 8; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | exit $?; | |||||||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 15 | ▶ | sub _die_on_insecure { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_die_on_insecure: called CC=7 SCAR=21.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 15 | my ($class, $app) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 95 | 15 | my $config = $app->config; | |||||||||||||||||||||||||||||||||||||||||||||
| 96 | 15 | ▶ | my $plugins = $config->{tf_plugins} || []; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$config->{'tf_plugins'} || []
MC/DC100%1/1
$config->{'tf_plugins'} || []
$config->{'tf_plugins'}
Truth table100%2/2
A
A= $config->{'tf_plugins'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 15 | ▶ | die "Cannot change user without TOADFARM_INSECURE=1" if $config->{hypnotoad}{user}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 14 | ▶ | die "Cannot change group without TOADFARM_INSECURE=1" if $config->{hypnotoad}{group}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 100 | die "Cannot run as 'root' without TOADFARM_INSECURE=1" | ||||||||||||||||||||||||||||||||||||||||||||||
| 101 | if +($> == 0 or $< == 0) | ||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | and !grep {/\bSetUserGroup$/} @$plugins; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$> == 0 || $< == 0 and not grep({/\bSetUserGroup$/u;} @$plugins)
$> == 0 or $< == 0
MC/DC0%0/3
$> == 0 || $< == 0 and not grep({/\bSetUserGroup$/u;} @$plugins)
$> == 0
$< == 0
not grep({/\bSetUserGroup$/u;} @$plugins)
Truth table20%1/5
A or B and not(C)
A= $> == 0B= $< == 0C= not grep({/\bSetUserGroup$/u;} @$plugins)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | sub _exit { say shift and exit 0 } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_exit: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 4 | ▶ | sub _load_plugins { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_load_plugins: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 4 | unshift @{$self->plugins->namespaces}, 'Toadfarm::Plugin'; | |||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 4 | ▶ | while (@_) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 4 | my ($plugin, $config) = (shift @_, shift @_); | |||||||||||||||||||||||||||||||||||||||||||||
| 114 | 4 | $self->log->info("Loading plugin $plugin"); | |||||||||||||||||||||||||||||||||||||||||||||
| 115 | 4 | $self->plugin($plugin, $config); | |||||||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 14 | ▶ | sub _moniker { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_moniker: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 14 | my $moniker = basename $0; | |||||||||||||||||||||||||||||||||||||||||||||
| 121 | 14 | $moniker =~ s!\W!_!g; | |||||||||||||||||||||||||||||||||||||||||||||
| 122 | 14 | $moniker; | |||||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 9 | ▶ | sub _mount_apps { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_mount_apps: called CC=26 SCAR=34.1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 9 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 127 | 9 | my $routes = $self->routes; | |||||||||||||||||||||||||||||||||||||||||||||
| 128 | 9 | my $config = $self->config; | |||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 9 | ▶ | while (@_) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 14 | my ($app, $rules) = (shift @_, shift @_); | |||||||||||||||||||||||||||||||||||||||||||||
| 132 | 14 | my $server = Mojo::Server->new; | |||||||||||||||||||||||||||||||||||||||||||||
| 133 | 14 | my $mount_point = delete $rules->{mount_point}; | |||||||||||||||||||||||||||||||||||||||||||||
| 134 | 14 | my ($request_base, $tmp, @over); | |||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 14 | local $ENV{MOJO_CONFIG} = $ENV{MOJO_CONFIG}; | |||||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 14 | ▶ | if (ref $rules->{config} eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 6 | require File::Temp; | |||||||||||||||||||||||||||||||||||||||||||||
| 140 | 6 | my %config = (%{$self->config}, %{$rules->{config}}); | |||||||||||||||||||||||||||||||||||||||||||||
| 141 | 6 | $tmp = File::Temp->new; | |||||||||||||||||||||||||||||||||||||||||||||
| 142 | Mojo::File->new($tmp->filename)->spurt( | ||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 6 | do { | |||||||||||||||||||||||||||||||||||||||||||||
| 144 | 6 | local $Data::Dumper::Terse = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 145 | 6 | local $Data::Dumper::Deepcopy = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 146 | 6 | Data::Dumper::Dumper(\%config); | |||||||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 148 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 6 | $ENV{MOJO_CONFIG} = $tmp->filename; | |||||||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 14 | ▶ | unless (ref $app and UNIVERSAL::isa($app, 'Mojolicious')) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $app and UNIVERSAL::isa($app, "Mojolicious")
MC/DC50%1/2
ref $app and UNIVERSAL::isa($app, "Mojolicious")
ref $app
UNIVERSAL::isa($app, "Mojolicious")
Truth table66%2/3
A and B
A= ref $appB= UNIVERSAL::isa($app, "Mojolicious")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 13 | my ($class, $path, @error) = ($app, $app); | |||||||||||||||||||||||||||||||||||||||||||||
| 154 | 13 | ▶ | $path = File::Which::which($path) || class_to_path($path) unless -r $path; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
File::Which::which($path) || class_to_path($path)
MC/DC0%0/2
File::Which::which($path) || class_to_path($path)
File::Which::which($path)
class_to_path($path)
Truth table33%1/3
A || B
A= File::Which::which($path)B= class_to_path($path)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 13 | ▶ | $app = eval { $server->build_app($class) } or push @error, $@ if $class =~ /^[\w:]+$/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$class =~ /^[\w:]+$/u && (($app = eval {
do {
$server->build_app($class)
}
}) || push(@error, $@))
$app = eval {
do {
$server->build_app($class)
}
} or push @error, $@
MC/DC0%0/3
$class =~ /^[\w:]+$/u && (($app = eval {
do {
$server->build_app($class)
}
}) || push(@error, $@))
$class =~ /^[\w:]+$/u
$app = eval {
do {
$server->build_app($class)
}
}
push @error, $@
Truth table0%0/4
A && B or C
A= $class =~ /^[\w:]+$/uB= $app = eval {
do {
$server->build_app($class)
}
}C= push @error, $@
| |||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | ▶ | $app = eval { $server->load_app($path) } or push @error, $@ unless ref $app; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition16%1/6
ref $app || (($app = eval {
do {
$server->load_app($path)
}
}) || push(@error, $@))
$app = eval {
do {
$server->load_app($path)
}
} or push @error, $@
MC/DC0%0/3
ref $app || (($app = eval {
do {
$server->load_app($path)
}
}) || push(@error, $@))
ref $app
$app = eval {
do {
$server->load_app($path)
}
}
push @error, $@
Truth table0%0/4
A || B or C
A= ref $appB= $app = eval {
do {
$server->load_app($path)
}
}C= push @error, $@
| |||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 13 | ▶ | die join "\n", @error unless $app; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 14 | ▶ | $app->log($self->log) if $config->{log}{combined}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 14 | ▶ | $app->secrets($self->secrets) if $config->{tf}{secrets}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 14 | ▶ | if (ref $rules->{config} eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 6 | my $local = delete $rules->{config}; | |||||||||||||||||||||||||||||||||||||||||||||
| 165 | 6 | $app->config->{$_} = $local->{$_} for keys %$local; | |||||||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 14 | ▶ | $app->config->{$_} ||= $config->{$_} for keys %$config; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$app->config->{$_} ||= $config->{$_}
MC/DC0%0/2
$app->config->{$_} ||= $config->{$_}
$app->config->{$_}
$config->{$_}
Truth table66%2/3
A ||= B
A= $app->config->{$_}B= $config->{$_}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 14 | for my $k (qw(local_port remote_address remote_port)) { | |||||||||||||||||||||||||||||||||||||||||||||
| 171 | 42 | push @over, $self->_skip_if(tx => $k, delete $rules->{$k}); | |||||||||||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 14 | for my $name (sort keys %$rules) { | |||||||||||||||||||||||||||||||||||||||||||||
| 175 | 9 | ▶ | $request_base = $rules->{$name} if $name eq 'X-Request-Base'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 9 | push @over, $self->_skip_if(header => $name, $rules->{$name}); | |||||||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 14 | ▶ | if (@over) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 8 | $self->log->info("Mounting @{[$app->moniker]} with conditions"); | |||||||||||||||||||||||||||||||||||||||||||||
| 181 | 8 | unshift @over, "sub { my \$h = \$_[1]->req->headers;\nlocal \$1;"; | |||||||||||||||||||||||||||||||||||||||||||||
| 182 | 8 | ▶ | push @over, "\$_[1]->req->url->base(Mojo::URL->new(\$1 || '$request_base'));" if $request_base; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 8 | push @over, "return 1; }"; | |||||||||||||||||||||||||||||||||||||||||||||
| 184 | 8 | ▶ | $routes->add_condition("toadfarm_condition_$self->{mounted}", => eval "@over" || die "@over: $@"); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
eval join($", @over) || die("@{over}: $@")
MC/DC0%0/1
eval join($", @over) || die("@{over}: $@")
eval join($", @over)
Truth table50%1/2
A
A= eval join($", @over)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 8 | ▶ | $routes->any($mount_point || '/')->requires("toadfarm_condition_$self->{mounted}")->partial(1)->to(app => $app); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$mount_point || '/'
MC/DC100%1/1
$mount_point || '/'
$mount_point
Truth table100%2/2
A
A= $mount_point
| |||||||||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 187 | elsif ($mount_point) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 3 | $routes->any($mount_point)->partial(1)->to(app => $app); | |||||||||||||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 190 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 3 | $self->{root_app} = $app; | |||||||||||||||||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 14 | $self->{mounted}++; | |||||||||||||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 9 | $self; | |||||||||||||||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 1 | ▶ | sub _mount_root_app { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_mount_root_app: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 1 | my ($self, $app) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 202 | 1 | $self->log->info("Mounting @{[$app->moniker]} without conditions."); | |||||||||||||||||||||||||||||||||||||||||||||
| 203 | 1 | $self->routes->any('/')->partial(1)->to(app => $app); | |||||||||||||||||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 1 | ▶ | sub _paths { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_paths: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 1 | my ($self, $config) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 1 | for my $type (qw(renderer static)) { | |||||||||||||||||||||||||||||||||||||||||||||
| 210 | 2 | ▶ | my $paths = $config->{$type} or next; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 2 | $self->$type->paths($paths); | |||||||||||||||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 15 | ▶ | sub _pid_file { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_pid_file: called CC=2 SCAR=7.4
| |||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 15 | my ($class, $app) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 217 | 15 | my $name = basename $0; | |||||||||||||||||||||||||||||||||||||||||||||
| 218 | 15 | my $dir = dirname abs_path $0; | |||||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 15 | ▶ | return File::Spec->catfile($dir, "$name.pid") if -w $dir; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | return File::Spec->catfile(File::Spec->tmpdir, "toadfarm-$name.pid"); | |||||||||||||||||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | ▶ | sub _run_as { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_run_as: not called CC=10 SCAR=43.1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | ▶ | my $user = shift || die "Usage: run_as('username')"; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || die(q[Usage: run_as('username')])
MC/DC0%0/1
shift() || die(q[Usage: run_as('username')])
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 0 | my ($exit, $uid, @sudo); | |||||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | ▶ | $uid = $user =~ m!^\d+$! ? $user : scalar getpwnam $user; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 0 | ▶ | die "Could not find uid for user $user\n" unless $uid; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 0 | ▶ | return 1 if $uid == $>; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 0 | for my $p (File::Spec->path) { | |||||||||||||||||||||||||||||||||||||||||||||
| 233 | 0 | $sudo[0] = File::Spec->catfile($p, 'sudo'); | |||||||||||||||||||||||||||||||||||||||||||||
| 234 | 0 | ▶ | next unless -x $sudo[0]; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 0 | push @sudo, qw(-i -n -u), "#$uid"; | |||||||||||||||||||||||||||||||||||||||||||||
| 236 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 0 | ▶ | die "Cannot change to uid=$uid: 'sudo' was not found.\n" unless @sudo > 1; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 0 | push @sudo, $^X; | |||||||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | ▶ | push @sudo, -I => $INC[0] if $ENV{TOADFARM_ACTION} eq 'test'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 0 | push @sudo, File::Spec->rel2abs($0), @ARGV; | |||||||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | warn "[Toadfarm] system @sudo\n" if DEBUG; | |||||||||||||||||||||||||||||||||||||||||||||
| 244 | 0 | system @sudo; | |||||||||||||||||||||||||||||||||||||||||||||
| 245 | 0 | ▶ | die "Could not run '@sudo' exit=$exit\n" if $exit = $? >> 8; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 0 | exit $?; | |||||||||||||||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 15 | ▶ | sub _setup_app { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_setup_app: called CC=11 SCAR=24.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 15 | my ($class, $app) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 251 | 15 | my $config = $app->config; | |||||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 15 | ▶ | $app->secrets([Mojo::Util::md5_sum($$ . $0 . time . rand)]) unless $config->{tf}{secrets}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 15 | ▶ | $app->_mount_apps(@{$config->{apps}}) if $config->{apps}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 15 | ▶ | $app->_load_plugins(@{$config->{tf_plugins}}) if $config->{tf_plugins}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 15 | ▶ | if (my $root_app = delete $app->{root_app}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 3 | ▶ | if (@{$config->{apps} || []} == 2) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$config->{'apps'} || []
MC/DC0%0/1
$config->{'apps'} || []
$config->{'apps'}
Truth table50%1/2
A
A= $config->{'apps'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 2 | ▶ | my $plugins = $config->{tf_plugins} || []; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$config->{'tf_plugins'} || []
MC/DC100%1/1
$config->{'tf_plugins'} || []
$config->{'tf_plugins'}
Truth table100%2/2
A
A= $config->{'tf_plugins'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 2 | ▶ | $root_app->config(hypnotoad => $config->{hypnotoad}) if $config->{hypnotoad}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 2 | ▶ | $root_app->log($app->log) if $config->{tf}{logging}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 2 | $root_app->plugin(shift(@$plugins), shift(@$plugins)) for @$plugins; | |||||||||||||||||||||||||||||||||||||||||||||
| 263 | 2 | $root_app->secrets($app->secrets); | |||||||||||||||||||||||||||||||||||||||||||||
| 264 | 2 | push @{$root_app->commands->namespaces}, 'Toadfarm::Command'; | |||||||||||||||||||||||||||||||||||||||||||||
| 265 | 2 | return $root_app; | |||||||||||||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 267 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 1 | $app->_mount_root_app($root_app); | |||||||||||||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 13 | return $app; | |||||||||||||||||||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 3 | ▶ | sub _setup_log { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_setup_log: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 3 | my ($self, $config) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 277 | 3 | my $log = Mojo::Log->new; | |||||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 3 | $self->config(log => $config); | |||||||||||||||||||||||||||||||||||||||||||||
| 280 | 3 | ▶ | $log->path($config->{path}) if $config->{path} ||= delete $config->{file}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$config->{"path"} ||= delete $config->{"file"}
MC/DC50%1/2
$config->{"path"} ||= delete $config->{"file"}
$config->{"path"}
delete $config->{"file"}
Truth table66%2/3
A ||= B
A= $config->{"path"}B= delete $config->{"file"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 3 | ▶ | $log->level($config->{level} || 'info'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$config->{'level'} || 'info'
MC/DC0%0/1
$config->{'level'} || 'info'
$config->{'level'}
Truth table50%1/2
A
A= $config->{'level'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 3 | $self->log($log); | |||||||||||||||||||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 51 | ▶ | sub _skip_if { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_skip_if: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 51 | my ($self, $type, $k, $value) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 287 | 51 | ▶ | my $format = $type eq 'tx' ? '$_[1]->tx->%s' : $type eq 'header' ? q[$h->header('%s')] : q[INVALID(%s)]; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 51 | ▶ | if (!defined $value) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 40 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 292 | elsif (ref $value eq 'Regexp') { | ||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 3 | $value =~ s,(?<!\\)/,\\/,g; | |||||||||||||||||||||||||||||||||||||||||||||
| 294 | 3 | return sprintf "return 0 unless +($format || '') =~ /(%s)/;", $k, $value; | |||||||||||||||||||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 296 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 297 | 8 | return sprintf "return 0 unless +($format || '') eq '%s';", $k, $value; | |||||||||||||||||||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 21 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||||||
| 303 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||||||
| 305 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||||||
| 307 | Toadfarm - One Mojolicious app to rule them all | ||||||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||||||
| 309 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 0.84 | ||||||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||||||
| 313 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||||||
| 315 | Toadfarm is a module for configuring and starting your L<Mojolicious> | ||||||||||||||||||||||||||||||||||||||||||||||
| 316 | applications. You can either combine multiple applications in one script, | ||||||||||||||||||||||||||||||||||||||||||||||
| 317 | or just use it as a init script. | ||||||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||||||
| 319 | Core features: | ||||||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||||||
| 321 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||||||
| 323 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||||||
| 325 | Wrapper around L<hypnotoad|Mojo::Server::Hypnotoad> that makes your | ||||||||||||||||||||||||||||||||||||||||||||||
| 326 | application L<Sys-V|https://www.debian-administration.org/article/28/Making_scripts_run_at_boot_time_with_Debian> | ||||||||||||||||||||||||||||||||||||||||||||||
| 327 | compatible. | ||||||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||||||
| 329 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||||||
| 331 | Advanced routing and virtual host configuration. Also support routing | ||||||||||||||||||||||||||||||||||||||||||||||
| 332 | from behind another web server, such as L<nginx|http://nginx.com/>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 333 | This feature is very much like L<Mojolicious::Plugin::Mount> on steroids. | ||||||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||||||
| 335 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||||||
| 337 | Hijacking log messages to a common log file. There's also plugin, | ||||||||||||||||||||||||||||||||||||||||||||||
| 338 | L<Toadfarm::Plugin::AccessLog>, that allows you to log the requests sent | ||||||||||||||||||||||||||||||||||||||||||||||
| 339 | to your server. | ||||||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||||||
| 341 | =back | ||||||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||||||
| 343 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||||||
| 345 | =head2 Script | ||||||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||||||
| 347 | Here is an example script that sets up logging and mounts some applications | ||||||||||||||||||||||||||||||||||||||||||||||
| 348 | under different domains, as well as loading in some custom plugins. | ||||||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||||||
| 350 | See L<Toadfarm::Manual::DSL> for more information about the different functions. | ||||||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||||||
| 352 | #!/usr/bin/perl | ||||||||||||||||||||||||||||||||||||||||||||||
| 353 | use Toadfarm -init; | ||||||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||||||
| 355 | logging { | ||||||||||||||||||||||||||||||||||||||||||||||
| 356 | combined => 1, | ||||||||||||||||||||||||||||||||||||||||||||||
| 357 | file => "/var/log/toadfarm/app.log", | ||||||||||||||||||||||||||||||||||||||||||||||
| 358 | level => "info", | ||||||||||||||||||||||||||||||||||||||||||||||
| 359 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||||||||||
| 361 | mount "MyApp" => { | ||||||||||||||||||||||||||||||||||||||||||||||
| 362 | Host => "myapp.example.com", | ||||||||||||||||||||||||||||||||||||||||||||||
| 363 | config => { | ||||||||||||||||||||||||||||||||||||||||||||||
| 364 | config_parameter_for_myapp => "foo" | ||||||||||||||||||||||||||||||||||||||||||||||
| 365 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 366 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||||||
| 368 | mount "/path/to/app" => { | ||||||||||||||||||||||||||||||||||||||||||||||
| 369 | Host => "example.com", | ||||||||||||||||||||||||||||||||||||||||||||||
| 370 | mount_point => "/other", | ||||||||||||||||||||||||||||||||||||||||||||||
| 371 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||||||||||
| 373 | mount "Catch::All::App"; | ||||||||||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||||||||||
| 375 | plugin "Toadfarm::Plugin::AccessLog"; | ||||||||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||||||
| 377 | start; # needs to be at the last line | ||||||||||||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||||||||||||
| 379 | =head2 Usage | ||||||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||||||
| 381 | You don't have to put L</Script> in init.d, but it will work with standard | ||||||||||||||||||||||||||||||||||||||||||||||
| 382 | start/stop actions. | ||||||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||||||
| 384 | $ /etc/init.d/your-script reload | ||||||||||||||||||||||||||||||||||||||||||||||
| 385 | $ /etc/init.d/your-script start | ||||||||||||||||||||||||||||||||||||||||||||||
| 386 | $ /etc/init.d/your-script stop | ||||||||||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||||||||||
| 388 | See also L<Toadfarm::Manual::RunningToadfarm/Init script> for more details. | ||||||||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||||||
| 390 | You can also start the application with normal L<Mojolicious> commands: | ||||||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||||||
| 392 | $ morbo /etc/init.d/your-script | ||||||||||||||||||||||||||||||||||||||||||||||
| 393 | $ /etc/init.d/your-script daemon | ||||||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||||||
| 395 | =head1 DOCUMENTATION INDEX | ||||||||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||||||
| 397 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||||||
| 399 | =item * L<Toadfarm::Manual::Intro> - Introduction. | ||||||||||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||||||||||
| 401 | =item * L<Toadfarm::Manual::DSL> - Domain specific language for Toadfarm. | ||||||||||||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||||||||||||
| 403 | =item * L<Toadfarm::Manual::Config> - Config file format. | ||||||||||||||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||||||||||||||
| 405 | =item * L<Toadfarm::Manual::RunningToadfarm> - Command line options. | ||||||||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||||||
| 407 | =item * L<Toadfarm::Manual::BehindReverseProxy> - Toadfarm behind nginx. | ||||||||||||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||||||||||||
| 409 | =item * L<Toadfarm::Manual::VirtualHost> - Virtual host setup. | ||||||||||||||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||||||||||||||
| 411 | =back | ||||||||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||||||||
| 413 | =head1 PLUGINS | ||||||||||||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||||||||||||
| 415 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||||||||||
| 417 | =item * L<Toadfarm::Plugin::AccessLog> | ||||||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||||||
| 419 | Log each request that hit your application. | ||||||||||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||||||||||
| 421 | =item * L<Mojolicious::Plugin::SizeLimit> | ||||||||||||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||||||||||||
| 423 | Kill Hypnotoad workers if they grow too large. | ||||||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||||||
| 425 | =item * L<Mojolicious::Plugin::SetUserGroup> | ||||||||||||||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||||||||||||||
| 427 | Start as root, run workers as less user. See also | ||||||||||||||||||||||||||||||||||||||||||||||
| 428 | L<Toadfarm::Manual::RunningToadfarm/Listen to standard HTTP ports>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||||||
| 430 | =back | ||||||||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||||||
| 432 | =head1 PREVIOUS VERSIONS | ||||||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||||||
| 434 | L<Toadfarm> prior to version 0.49 used to be a configuration file loaded in | ||||||||||||||||||||||||||||||||||||||||||||||
| 435 | by the C<toadfarm> script. This resulted in all the executables to be named | ||||||||||||||||||||||||||||||||||||||||||||||
| 436 | C<toadfarm> instead of something descriptive. It also felt a bit awkward to | ||||||||||||||||||||||||||||||||||||||||||||||
| 437 | take over C<MOJO_CONFIG> and use all the crazy hacks to start C<hypnotoad>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||||||||||||||||
| 439 | It also didn't work well as an init script, so there still had to be a | ||||||||||||||||||||||||||||||||||||||||||||||
| 440 | seperate solution for that. | ||||||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||||||
| 442 | The new L<Toadfarm> DSL aim to solve all of these issues. This means that | ||||||||||||||||||||||||||||||||||||||||||||||
| 443 | if you decide to still use any C<MOJO_CONFIG>, it should be for the | ||||||||||||||||||||||||||||||||||||||||||||||
| 444 | applications loaded from inside C<Toadfarm> and not the startup script. | ||||||||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||||||||
| 446 | Note that the old solution still works, but a warning tells you to change | ||||||||||||||||||||||||||||||||||||||||||||||
| 447 | to the new L<DSL|Toadfarm::Manual::DSL> based API. | ||||||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||||||
| 449 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||||||||||
| 451 | Copyright (C) 2014, Jan Henning Thorsen | ||||||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||||||
| 453 | This program is free software, you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||||||||||||||||
| 454 | under the terms of the Artistic License version 2.0. | ||||||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||||||
| 456 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||||||
| 458 | Jan Henning Thorsen - C<jhthorsen@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||||||||||||
| 460 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||