File Coverage

File:script/database-bi
Coverage:94.2%

linestmtbrancondsubtimecode
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
29
29
29
97338
17
299
use strict;
16
29
29
29
20
17
422
use warnings;
17
18
29
29
29
30
11
95
use File::Spec ();
19
29
29
29
2145
7758
466
use FindBin;
20
29
14529
BEGIN { unshift @INC, "$FindBin::Bin/../lib" }
21
22
26
1108376
require Mojo::Loader;
23
26
1516473
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.
29sub _names_a_database {
30
23
23
        my ($arg) = @_;
31
32
23
29
        return 0 if $arg =~ /\A-/;
33
23
28
        (my $name = $arg) =~ s/-/_/g;
34
23
23
13
49
        for my $namespace (@{ Mojolicious::Commands->new->namespaces }, 'Database::BI::Command') {
35
61
2170
                my $class = "${namespace}::$name";
36
61
112
                return 0 if $name =~ /\A\w+\z/ && !Mojo::Loader::load_class($class);
37        }
38
11
203
        return -e $arg || $arg !~ /\A\w[\w-]*\z/;
39}
40
41sub _refuse {
42
8
13
        my ($message) = @_;
43
44
8
128
        print STDERR "database-bi: $message\n";
45
8
221
        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.
52sub _open_on_start {
53
10
9
        my ($database, @others) = @_;
54
55
10
12
        _refuse('only one database can be opened from the command line.  To bring '
56                . 'several together, open one and use Merge or Combine in the browser.')
57                if @others;
58
59
6
11
        if ($database !~ m{\A(?:/\.\./|redis:|mariadb:)}i) {
60
6
6687
                require Database::BI::Controller::Dashboard;
61
6
52
                _refuse("cannot open $database: no such file") unless -e $database;
62
4
7
                _refuse("cannot open $database: not a file") unless -f _;
63
3
6
                _refuse("cannot open $database: permission denied") unless -r _;
64
3
66
                _refuse("cannot open $database: not a supported file type")
65                        unless Database::BI::Controller::Dashboard::is_supported_file($database);
66
2
38
                $database = File::Spec->rel2abs($database);
67        }
68
69
2
10
        $ENV{BI_OPEN} = $database;
70
2
2
        return;
71}
72
73
26
131985
if (@ARGV && _names_a_database($ARGV[0])) {
74
10
17
        _open_on_start(@ARGV);
75
2
1
        @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
18
359106
if (!@ARGV && !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
5
9
        require Mojo::Server;
92
5
13
        Mojo::Server->new->build_app('Database::BI');
93
4
1619
        require Mojo::Server::Morbo;
94
4
3900
        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
13
39
Mojolicious::Commands->start_app('Database::BI');
101