| 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}; |
|---|
| 12 | |
|---|
| 13 | GetOptions( |
|---|
| 14 | 'file=s' => \my $CONFIG_FILE, |
|---|
| 15 | ); |
|---|
| 16 | |
|---|
| 17 | $CONFIG_FILE ||= 'server.yml'; |
|---|
| 18 | -e $CONFIG_FILE or die "Config file $CONFIG_FILE not found\n"; |
|---|
| 19 | my $config = YAML::LoadFile($CONFIG_FILE); |
|---|
| 20 | |
|---|
| 21 | my %run = ( |
|---|
| 22 | run => \&run_server, |
|---|
| 23 | start => \&start_server, |
|---|
| 24 | stop => \&stop_server, |
|---|
| 25 | restart => sub { |
|---|
| 26 | my $config = shift; |
|---|
| 27 | stop_server($config); |
|---|
| 28 | start_server($config); |
|---|
| 29 | }, |
|---|
| 30 | ); |
|---|
| 31 | |
|---|
| 32 | my $command = shift or die "Usage: $0 <" . join('|', keys %run) . ">\n"; |
|---|
| 33 | |
|---|
| 34 | exists $run{$command} or die "Unrecognized command $command\n"; |
|---|
| 35 | $run{$command}->($config); |
|---|
| 36 | |
|---|
| 37 | sub run_server { |
|---|
| 38 | my $config = shift; |
|---|
| 39 | require BookmarksApp; |
|---|
| 40 | my $app = BookmarksApp->new({ config => $config })->to_app; |
|---|
| 41 | |
|---|
| 42 | my $server_root = $config->{server_root} || '.'; |
|---|
| 43 | my $listen = ':' . ($config->{port} || 5000); |
|---|
| 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); |
|---|
| 59 | my $access_log = catfile($server_root, 'access'); |
|---|
| 60 | my $error_log = catfile($server_root, 'errors'); |
|---|
| 61 | my $pid_file = catfile($server_root, 'pid'); |
|---|
| 62 | |
|---|
| 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 | } |
|---|
| 73 | |
|---|
| 74 | sub stop_server { |
|---|
| 75 | my $config = shift; |
|---|
| 76 | my $server_root = $config->{server_root} || '.'; |
|---|
| 77 | |
|---|
| 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 | } |
|---|