/
growthforecast.pl
executable file
·150 lines (133 loc) · 3.85 KB
/
growthforecast.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/perl
use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/extlib/lib/perl5";
use lib "$FindBin::Bin/lib";
use File::Basename;
use Getopt::Long;
use Plack::Loader;
use Plack::Builder;
use Plack::Builder::Conditionals;
use GrowthForecast::Web;
use GrowthForecast::Worker;
use Proclet;
use File::ShareDir qw/dist_dir/;
use Cwd;
use File::Path qw/mkpath/;
my $port = 5125;
my $host = 0;
my @front_proxy;
my @allow_from;
Getopt::Long::Configure ("no_ignore_case");
GetOptions(
'port=s' => \$port,
'host=s' => \$host,
'front-proxy=s' => \@front_proxy,
'allow-from=s' => \@allow_from,
'disable-1min-metrics' => \my $disable_short,
'with-mysql=s' => \my $mysql,
'data-dir=s' => \my $data_dir,
"h|help" => \my $help,
);
if ( $help ) {
print <<EOF;
usage: $0 --port 5005 --host 127.0.0.1 --front-proxy 127.0.0.1
--allow-from 127.0.0.1
--disable-1min-metrics
--data-dir dir
--with-mysql dbi:mysql:[dbname];hostname=[localhost]
If you want to use MySQL instead of SQLite, set with-mysql opt with your DSN.
MYSQL_USER,MYSQL_PASSWORD environment values are used as username and password
for connecting to MySQL.
eg:
\% MYSQL_USER=www MYSQL_PASSWORD=foobar perl $0 \\
--with-mysql dbi:mysql:growthforecast;hostname=localhost
GrowthForecast needs CREATE, ALTER, DELETE, INSERT, UPDATE and SELECT privileges
eg:
mysql> GRANT CREATE, ALTER, DELETE, INSERT, UPDATE, SELECT \\
ON growthforecast.* TO 'www'\@'localhost' IDENTIFIED BY foobar;
EOF
exit(1);
}
if ( $mysql ) {
eval { require GrowthForecast::Data::MySQL };
die "Cannot load MySQL: $@" if $@;
}
my $root_dir = File::Basename::dirname(__FILE__);
if ( ! -f "$root_dir/lib/GrowthForecast.pm" ) {
$root_dir = dist_dir('GrowthForecast');
}
if ( !$data_dir ) {
$data_dir = $root_dir . '/data';
}
else {
$data_dir = Cwd::realpath($data_dir);
}
{
if ( ! -d $data_dir ) {
mkpath($data_dir) or die "cannot create data directory '$data_dir': $!";
}
open( my $fh, '>', "$data_dir/$$.tmp") or die 'cannot create file in data_dir: $!';
close($fh);
unlink("$data_dir/$$.tmp");
}
my $proclet = Proclet->new;
$proclet->service(
code => sub {
local $0 = "$0 (GrowthForecast::Worker 1min)";
my $worker = GrowthForecast::Worker->new(
root_dir => $root_dir,
data_dir => $data_dir,
mysql => $mysql,
);
$worker->run('short');
}
) if !$disable_short;
$proclet->service(
code => sub {
local $0 = "$0 (GrowthForecast::Worker)";
my $worker = GrowthForecast::Worker->new(
root_dir => $root_dir,
data_dir => $data_dir,
mysql => $mysql
);
$worker->run;
}
);
$proclet->service(
code => sub {
local $0 = "$0 (GrowthForecast::Web)";
my $web = GrowthForecast::Web->new(
root_dir => $root_dir,
data_dir => $data_dir,
short => !$disable_short,
mysql => $mysql,
);
my $app = builder {
enable 'Lint';
enable 'StackTrace';
if ( @front_proxy ) {
enable match_if addr(\@front_proxy), 'ReverseProxy';
}
if ( @allow_from ) {
enable match_if addr('!',\@allow_from), sub {
sub { [403,['Content-Type','text/plain'], ['Forbidden']] }
};
}
enable 'Static',
path => qr!^/(?:(?:css|js|images)/|favicon\.ico$)!,
root => $root_dir . '/public';
enable 'Scope::Container';
$web->psgi;
};
my $loader = Plack::Loader->load(
'Starlet',
port => $port,
host => $host || 0,
max_workers => 4,
);
$loader->run($app);
}
);
$proclet->run;