1 | #!/usr/bin/perl -w |
---|
2 | use strict; |
---|
3 | |
---|
4 | use FindBin; |
---|
5 | use lib "$FindBin::RealBin/../lib"; |
---|
6 | |
---|
7 | use Getopt::Long; |
---|
8 | use YAML; |
---|
9 | use Plack::Runner; |
---|
10 | use File::Pid; |
---|
11 | use File::Spec::Functions qw{catfile rel2abs}; |
---|
12 | use Cwd; |
---|
13 | |
---|
14 | GetOptions( |
---|
15 | 'file=s' => \my $CONFIG_FILE, |
---|
16 | 'verbose|v' => \my $VERBOSE, |
---|
17 | 'D=s' => \my %DEFINES, |
---|
18 | ); |
---|
19 | |
---|
20 | my %run = ( |
---|
21 | run => \&run_server, |
---|
22 | start => \&start_server, |
---|
23 | stop => sub { signal_server('QUIT') }, |
---|
24 | restart => sub { signal_server('HUP') }, |
---|
25 | ); |
---|
26 | |
---|
27 | my %default_config = ( |
---|
28 | server_root => cwd, |
---|
29 | port => 5000, |
---|
30 | access_log => 'access', |
---|
31 | error_log => 'error', |
---|
32 | pid_file => 'pid', |
---|
33 | ); |
---|
34 | |
---|
35 | my $command = shift or die "Usage: $0 <" . join('|', keys %run) . ">\n"; |
---|
36 | |
---|
37 | exists $run{$command} or die "Unrecognized command $command\n"; |
---|
38 | $run{$command}->(); |
---|
39 | |
---|
40 | sub get_config { |
---|
41 | $CONFIG_FILE ||= 'server.yml'; |
---|
42 | my $config_from_file = -e $CONFIG_FILE ? YAML::LoadFile($CONFIG_FILE) : {}; |
---|
43 | |
---|
44 | my $config = { |
---|
45 | %default_config, |
---|
46 | %{ $config_from_file }, |
---|
47 | %DEFINES, |
---|
48 | }; |
---|
49 | |
---|
50 | # if authentication is enabled and no digest_key is provided, generate one |
---|
51 | # don't do this if it isn't needed, since this is sometimes not a fast operation |
---|
52 | if ($config->{auth} && !$config->{digest_key}) { |
---|
53 | warn "Generating digest authentication secret...\n"; |
---|
54 | require Bytes::Random::Secure; |
---|
55 | $config->{digest_key} = Bytes::Random::Secure::random_bytes_base64(16, ''); |
---|
56 | } |
---|
57 | |
---|
58 | # make config paths absolute before handing off to the app |
---|
59 | for my $file_key (qw{dbname htdigest access_log error_log pid_file}) { |
---|
60 | if ($config->{$file_key}) { |
---|
61 | $config->{$file_key} = rel2abs($config->{$file_key}, $config->{server_root}); |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | warn Dump($config) if $VERBOSE; |
---|
66 | |
---|
67 | return $config; |
---|
68 | } |
---|
69 | |
---|
70 | sub run_server { |
---|
71 | my $config = get_config(); |
---|
72 | |
---|
73 | require BookmarksApp; |
---|
74 | my $app = BookmarksApp->new({ config => $config })->to_app; |
---|
75 | |
---|
76 | my $runner = Plack::Runner->new(server => 'Starman'); |
---|
77 | $runner->parse_options( |
---|
78 | '--listen', ':' . $config->{port}, |
---|
79 | ); |
---|
80 | $runner->run($app); |
---|
81 | } |
---|
82 | |
---|
83 | sub start_server { |
---|
84 | my $config = get_config(); |
---|
85 | |
---|
86 | require BookmarksApp; |
---|
87 | my $app = BookmarksApp->new({ config => $config })->to_app; |
---|
88 | |
---|
89 | my $runner = Plack::Runner->new(server => 'Starman'); |
---|
90 | $runner->parse_options( |
---|
91 | '--daemonize', |
---|
92 | '--listen', ':' . $config->{port}, |
---|
93 | '--pid', $config->{pid_file}, |
---|
94 | '--error-log', $config->{error_log}, |
---|
95 | '--access-log', $config->{access_log}, |
---|
96 | ); |
---|
97 | $runner->run($app); |
---|
98 | } |
---|
99 | |
---|
100 | sub signal_server { |
---|
101 | my $config = get_config(); |
---|
102 | my $signal = shift; |
---|
103 | |
---|
104 | my $pid_path = $config->{pid_file}; |
---|
105 | unless (-e $pid_path) { |
---|
106 | warn "$pid_path does not exist\n"; |
---|
107 | return; |
---|
108 | } |
---|
109 | |
---|
110 | my $pid_file = File::Pid->new({ |
---|
111 | file => $pid_path, |
---|
112 | }); |
---|
113 | |
---|
114 | if (my $pid = $pid_file->running) { |
---|
115 | kill $signal, $pid; |
---|
116 | } |
---|
117 | } |
---|