[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 | |
---|
[103] | 20 | my %run = ( |
---|
[110] | 21 | run => \&run_server, |
---|
| 22 | start => \&start_server, |
---|
[115] | 23 | stop => sub { signal_server('QUIT') }, |
---|
| 24 | restart => sub { signal_server('HUP') }, |
---|
[104] | 25 | ); |
---|
[94] | 26 | |
---|
[115] | 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 | |
---|
[110] | 35 | my $command = shift or die "Usage: $0 <" . join('|', keys %run) . ">\n"; |
---|
| 36 | |
---|
[104] | 37 | exists $run{$command} or die "Unrecognized command $command\n"; |
---|
[115] | 38 | $run{$command}->(); |
---|
[103] | 39 | |
---|
[115] | 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 | |
---|
[116] | 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 | |
---|
[115] | 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 | |
---|
[110] | 70 | sub run_server { |
---|
[115] | 71 | my $config = get_config(); |
---|
| 72 | |
---|
[104] | 73 | require BookmarksApp; |
---|
| 74 | my $app = BookmarksApp->new({ config => $config })->to_app; |
---|
[103] | 75 | |
---|
[110] | 76 | my $runner = Plack::Runner->new(server => 'Starman'); |
---|
| 77 | $runner->parse_options( |
---|
[115] | 78 | '--listen', ':' . $config->{port}, |
---|
[110] | 79 | ); |
---|
| 80 | $runner->run($app); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | sub start_server { |
---|
[115] | 84 | my $config = get_config(); |
---|
| 85 | |
---|
[110] | 86 | require BookmarksApp; |
---|
| 87 | my $app = BookmarksApp->new({ config => $config })->to_app; |
---|
| 88 | |
---|
[104] | 89 | my $runner = Plack::Runner->new(server => 'Starman'); |
---|
| 90 | $runner->parse_options( |
---|
| 91 | '--daemonize', |
---|
[115] | 92 | '--listen', ':' . $config->{port}, |
---|
| 93 | '--pid', $config->{pid_file}, |
---|
| 94 | '--error-log', $config->{error_log}, |
---|
| 95 | '--access-log', $config->{access_log}, |
---|
[104] | 96 | ); |
---|
| 97 | $runner->run($app); |
---|
| 98 | } |
---|
[103] | 99 | |
---|
[115] | 100 | sub signal_server { |
---|
| 101 | my $config = get_config(); |
---|
| 102 | my $signal = shift; |
---|
[103] | 103 | |
---|
[115] | 104 | my $pid_path = $config->{pid_file}; |
---|
[104] | 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] | 115 | kill $signal, $pid; |
---|
[104] | 116 | } |
---|
| 117 | } |
---|