| 1 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # The shebang has to name perl, and must not name a server. morbo starts the | ||||||||||||||||||||||||||||
| 4 | # application as | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # perl <this script> daemon | ||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||
| 8 | # and perl hands a script whose #! line does not mention perl to the | ||||||||||||||||||||||||||||
| 9 | # interpreter that line names (see perlrun). With "#!/usr/bin/env morbo" that | ||||||||||||||||||||||||||||
| 10 | # interpreter is morbo again, so every worker started another morbo; each | ||||||||||||||||||||||||||||
| 11 | # generation exported the whole of @INC as PERL5LIB, so the environment grew | ||||||||||||||||||||||||||||
| 12 | # with every step until exec failed with "Argument list too long", and every | ||||||||||||||||||||||||||||
| 13 | # generation kept the listening socket, leaving a chain of stale servers. | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 6 | ▶ | use File::Spec (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 6 | ▶ | use FindBin; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 6 | ▶ | BEGIN { unshift @INC, "$FindBin::Bin/../lib" } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 6 | require Mojo::Loader; | |||||||||||||||||||||||||||
| 23 | 6 | require Mojolicious::Commands; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # True when an argument names something to open, not a Mojolicious command. A | ||||||||||||||||||||||||||||
| 26 | # command word always wins, even over a file of that name in the current | ||||||||||||||||||||||||||||
| 27 | # directory: morbo restarts the server as "perl <this script> daemon". A word | ||||||||||||||||||||||||||||
| 28 | # that is neither is left for Mojolicious, which says the command is unknown. | ||||||||||||||||||||||||||||
| 29 | 5 | ▶ | sub _names_a_database { | ||||||||||||||||||||||||||
|
Subroutine
_names_a_database: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 30 | 5 | my ($arg) = @_; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 5 | ▶ | return 0 if $arg =~ /\A-/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 5 | (my $name = $arg) =~ s/-/_/g; | |||||||||||||||||||||||||||
| 34 | 5 | for my $namespace (@{ Mojolicious::Commands->new->namespaces }, 'Database::BI::Command') { | |||||||||||||||||||||||||||
| 35 | 14 | my $class = "${namespace}::$name"; | |||||||||||||||||||||||||||
| 36 | 14 | ▶ | return 0 if $name =~ /\A\w+\z/ && !Mojo::Loader::load_class($class); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$name =~ /\A\w+\z/ and not Mojo::Loader::load_class($class)
MC/DC100%2/2
$name =~ /\A\w+\z/ and not Mojo::Loader::load_class($class)
$name =~ /\A\w+\z/
not Mojo::Loader::load_class($class)
Truth table100%3/3
A and not(B)
A= $name =~ /\A\w+\z/B= not Mojo::Loader::load_class($class)
| |||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | 4 | ▶ | return -e $arg || $arg !~ /\A\w[\w-]*\z/; | ||||||||||||||||||||||||||
|
Condition66%2/3
-e $arg || !($arg =~ /\A\w[\w-]*\z/)
MC/DC0%0/2
-e $arg || !($arg =~ /\A\w[\w-]*\z/)
-e $arg
!($arg =~ /\A\w[\w-]*\z/)
Truth table66%2/3
A || not(B)
A= -e $argB= !($arg =~ /\A\w[\w-]*\z/)
| |||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 4 | ▶ | sub _refuse { | ||||||||||||||||||||||||||
|
Subroutine
_refuse: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 4 | my ($message) = @_; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 4 | print STDERR "database-bi: $message\n"; | |||||||||||||||||||||||||||
| 45 | 4 | exit 2; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # Hands the one database named to the server through the environment, because | ||||||||||||||||||||||||||||
| 49 | # morbo restarts the application with its own arguments. GET / reads BI_OPEN. | ||||||||||||||||||||||||||||
| 50 | # A remote path, Redis spec or MariaDB spec cannot be checked from here, so /open | ||||||||||||||||||||||||||||
| 51 | # reports it. | ||||||||||||||||||||||||||||
| 52 | 4 | ▶ | sub _open_on_start { | ||||||||||||||||||||||||||
|
Subroutine
_open_on_start: called CC=7 SCAR=36.7
| |||||||||||||||||||||||||||||
| 53 | 4 | my ($database, @others) = @_; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 4 | ▶ | _refuse('only one database can be opened from the command line. To bring ' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | . 'several together, open one and use Merge or Combine in the browser.') | ||||||||||||||||||||||||||||
| 57 | if @others; | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | ▶ | if ($database !~ m{\A(?:/\.\./|redis:|mariadb:)}i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 0 | require Database::BI::Controller::Dashboard; | |||||||||||||||||||||||||||
| 61 | 0 | ▶ | _refuse("cannot open $database: no such file") unless -e $database; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | _refuse("cannot open $database: not a file") unless -f _; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | _refuse("cannot open $database: permission denied") unless -r _; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | _refuse("cannot open $database: not a supported file type") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | unless Database::BI::Controller::Dashboard::is_supported_file($database); | ||||||||||||||||||||||||||||
| 66 | 0 | $database = File::Spec->rel2abs($database); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | $ENV{BI_OPEN} = $database; | |||||||||||||||||||||||||||
| 70 | 0 | return; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 6 | ▶ | if (@ARGV && _names_a_database($ARGV[0])) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@ARGV and _names_a_database($ARGV[0])
MC/DC100%2/2
@ARGV and _names_a_database($ARGV[0])
@ARGV
_names_a_database($ARGV[0])
Truth table100%3/3
A and B
A= @ARGVB= _names_a_database($ARGV[0])
| |||||||||||||||||||||||||||||
| 74 | 4 | _open_on_start(@ARGV); | |||||||||||||||||||||||||||
| 75 | 0 | @ARGV = (); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | # With no arguments this is the development server, restarting when a file | ||||||||||||||||||||||||||||
| 79 | # changes - what "morbo script/database-bi" does. A command word (daemon, | ||||||||||||||||||||||||||||
| 80 | # routes, get, ...) goes to the application's command line interface, so the | ||||||||||||||||||||||||||||
| 81 | # script can still start a plain server or be used from a test. | ||||||||||||||||||||||||||||
| 82 | # | ||||||||||||||||||||||||||||
| 83 | # A server that loads this file instead of running it (hypnotoad) also leaves | ||||||||||||||||||||||||||||
| 84 | # @ARGV empty, and sets MOJO_APP_LOADER. It must not get the development server, | ||||||||||||||||||||||||||||
| 85 | # which never returns. Morbo's run exits, so start_app is reached only otherwise. | ||||||||||||||||||||||||||||
| 86 | 2 | ▶ | if (!@ARGV && !defined $ENV{MOJO_APP_LOADER}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@ARGV or defined $ENV{"MOJO_APP_LOADER"}
MC/DC50%1/2
@ARGV or defined $ENV{"MOJO_APP_LOADER"}
@ARGV
defined $ENV{"MOJO_APP_LOADER"}
Truth table66%2/3
A or B
A= @ARGVB= defined $ENV{"MOJO_APP_LOADER"}
| |||||||||||||||||||||||||||||
| 87 | # Morbo opens the port before the application is loaded, so load it once | ||||||||||||||||||||||||||||
| 88 | # here: its startup reads config listen into MOJO_LISTEN (this machine | ||||||||||||||||||||||||||||
| 89 | # only, unless the configuration says otherwise), and a configuration | ||||||||||||||||||||||||||||
| 90 | # that does not start stops here rather than in every restart. | ||||||||||||||||||||||||||||
| 91 | 1 | require Mojo::Server; | |||||||||||||||||||||||||||
| 92 | 1 | Mojo::Server->new->build_app('Database::BI'); | |||||||||||||||||||||||||||
| 93 | 0 | require Mojo::Server::Morbo; | |||||||||||||||||||||||||||
| 94 | 0 | Mojo::Server::Morbo->new(silent => 1) | |||||||||||||||||||||||||||
| 95 | ->run(File::Spec->catfile($FindBin::RealBin, $FindBin::Script)); | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # This must stay the last statement: a loading server takes the file's value as | ||||||||||||||||||||||||||||
| 99 | # the application, and refuses anything else. | ||||||||||||||||||||||||||||
| 100 | 1 | Mojolicious::Commands->start_app('Database::BI'); | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | __END__ | ||||||||||||||||||||||||||||