[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; |
---|
[102] | 11 | use File::Spec::Functions qw{catfile}; |
---|
[93] | 12 | |
---|
[94] | 13 | GetOptions( |
---|
| 14 | 'file=s' => \my $CONFIG_FILE, |
---|
| 15 | ); |
---|
[93] | 16 | |
---|
[96] | 17 | $CONFIG_FILE ||= 'server.yml'; |
---|
[94] | 18 | -e $CONFIG_FILE or die "Config file $CONFIG_FILE not found\n"; |
---|
| 19 | my $config = YAML::LoadFile($CONFIG_FILE); |
---|
| 20 | |
---|
[103] | 21 | my %run = ( |
---|
[110] | 22 | run => \&run_server, |
---|
| 23 | start => \&start_server, |
---|
| 24 | stop => \&stop_server, |
---|
[104] | 25 | restart => sub { |
---|
[103] | 26 | my $config = shift; |
---|
[110] | 27 | stop_server($config); |
---|
| 28 | start_server($config); |
---|
[104] | 29 | }, |
---|
| 30 | ); |
---|
[94] | 31 | |
---|
[110] | 32 | my $command = shift or die "Usage: $0 <" . join('|', keys %run) . ">\n"; |
---|
| 33 | |
---|
[104] | 34 | exists $run{$command} or die "Unrecognized command $command\n"; |
---|
| 35 | $run{$command}->($config); |
---|
[103] | 36 | |
---|
[110] | 37 | sub run_server { |
---|
[104] | 38 | my $config = shift; |
---|
| 39 | require BookmarksApp; |
---|
| 40 | my $app = BookmarksApp->new({ config => $config })->to_app; |
---|
[103] | 41 | |
---|
[104] | 42 | my $server_root = $config->{server_root} || '.'; |
---|
| 43 | my $listen = ':' . ($config->{port} || 5000); |
---|
[110] | 44 | |
---|
| 45 | my $runner = Plack::Runner->new(server => 'Starman'); |
---|
| 46 | $runner->parse_options( |
---|
| 47 | '--listen', $listen, |
---|
| 48 | ); |
---|
| 49 | $runner->run($app); |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | sub start_server { |
---|
| 53 | my $config = shift; |
---|
| 54 | require BookmarksApp; |
---|
| 55 | my $app = BookmarksApp->new({ config => $config })->to_app; |
---|
| 56 | |
---|
| 57 | my $server_root = $config->{server_root} || '.'; |
---|
| 58 | my $listen = ':' . ($config->{port} || 5000); |
---|
[104] | 59 | my $access_log = catfile($server_root, 'access'); |
---|
| 60 | my $error_log = catfile($server_root, 'errors'); |
---|
| 61 | my $pid_file = catfile($server_root, 'pid'); |
---|
[103] | 62 | |
---|
[104] | 63 | my $runner = Plack::Runner->new(server => 'Starman'); |
---|
| 64 | $runner->parse_options( |
---|
| 65 | '--daemonize', |
---|
| 66 | '--listen', $listen, |
---|
| 67 | '--pid', $pid_file, |
---|
| 68 | '--error-log', $error_log, |
---|
| 69 | '--access-log', $access_log, |
---|
| 70 | ); |
---|
| 71 | $runner->run($app); |
---|
| 72 | } |
---|
[103] | 73 | |
---|
[110] | 74 | sub stop_server { |
---|
[104] | 75 | my $config = shift; |
---|
| 76 | my $server_root = $config->{server_root} || '.'; |
---|
[103] | 77 | |
---|
[104] | 78 | my $pid_path = catfile($server_root, 'pid'); |
---|
| 79 | unless (-e $pid_path) { |
---|
| 80 | warn "$pid_path does not exist\n"; |
---|
| 81 | return; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | my $pid_file = File::Pid->new({ |
---|
| 85 | file => $pid_path, |
---|
| 86 | }); |
---|
| 87 | |
---|
| 88 | if (my $pid = $pid_file->running) { |
---|
| 89 | kill 'TERM', $pid; |
---|
| 90 | } |
---|
| 91 | } |
---|