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