| 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 | # make config paths absolute before handing off to the app |
|---|
| 51 | for my $file_key (qw{dbname htdigest access_log error_log pid_file}) { |
|---|
| 52 | if ($config->{$file_key}) { |
|---|
| 53 | $config->{$file_key} = rel2abs($config->{$file_key}, $config->{server_root}); |
|---|
| 54 | } |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | warn Dump($config) if $VERBOSE; |
|---|
| 58 | |
|---|
| 59 | return $config; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | sub run_server { |
|---|
| 63 | my $config = get_config(); |
|---|
| 64 | |
|---|
| 65 | require BookmarksApp; |
|---|
| 66 | my $app = BookmarksApp->new({ config => $config })->to_app; |
|---|
| 67 | |
|---|
| 68 | my $runner = Plack::Runner->new(server => 'Starman'); |
|---|
| 69 | $runner->parse_options( |
|---|
| 70 | '--listen', ':' . $config->{port}, |
|---|
| 71 | ); |
|---|
| 72 | $runner->run($app); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | sub start_server { |
|---|
| 76 | my $config = get_config(); |
|---|
| 77 | |
|---|
| 78 | require BookmarksApp; |
|---|
| 79 | my $app = BookmarksApp->new({ config => $config })->to_app; |
|---|
| 80 | |
|---|
| 81 | my $runner = Plack::Runner->new(server => 'Starman'); |
|---|
| 82 | $runner->parse_options( |
|---|
| 83 | '--daemonize', |
|---|
| 84 | '--listen', ':' . $config->{port}, |
|---|
| 85 | '--pid', $config->{pid_file}, |
|---|
| 86 | '--error-log', $config->{error_log}, |
|---|
| 87 | '--access-log', $config->{access_log}, |
|---|
| 88 | ); |
|---|
| 89 | $runner->run($app); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | sub signal_server { |
|---|
| 93 | my $config = get_config(); |
|---|
| 94 | my $signal = shift; |
|---|
| 95 | |
|---|
| 96 | my $pid_path = $config->{pid_file}; |
|---|
| 97 | unless (-e $pid_path) { |
|---|
| 98 | warn "$pid_path does not exist\n"; |
|---|
| 99 | return; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | my $pid_file = File::Pid->new({ |
|---|
| 103 | file => $pid_path, |
|---|
| 104 | }); |
|---|
| 105 | |
|---|
| 106 | if (my $pid = $pid_file->running) { |
|---|
| 107 | kill $signal, $pid; |
|---|
| 108 | } |
|---|
| 109 | } |
|---|