Changeset 115 in bookmarks
- Timestamp:
- 02/18/16 22:27:07 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/bin/bkmkd
r110 r115 9 9 use Plack::Runner; 10 10 use File::Pid; 11 use File::Spec::Functions qw{catfile}; 11 use File::Spec::Functions qw{catfile rel2abs}; 12 use Cwd; 12 13 13 14 GetOptions( 14 'file=s' => \my $CONFIG_FILE, 15 'file=s' => \my $CONFIG_FILE, 16 'verbose|v' => \my $VERBOSE, 17 'D=s' => \my %DEFINES, 15 18 ); 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 19 21 20 my %run = ( 22 21 run => \&run_server, 23 22 start => \&start_server, 24 stop => \&stop_server, 25 restart => sub { 26 my $config = shift; 27 stop_server($config); 28 start_server($config); 29 }, 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', 30 33 ); 31 34 … … 33 36 34 37 exists $run{$command} or die "Unrecognized command $command\n"; 35 $run{$command}->($config); 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 } 36 61 37 62 sub run_server { 38 my $config = shift; 63 my $config = get_config(); 64 39 65 require BookmarksApp; 40 66 my $app = BookmarksApp->new({ config => $config })->to_app; 41 67 42 my $server_root = $config->{server_root} || '.';43 my $listen = ':' . ($config->{port} || 5000);44 45 68 my $runner = Plack::Runner->new(server => 'Starman'); 46 69 $runner->parse_options( 47 '--listen', $listen,70 '--listen', ':' . $config->{port}, 48 71 ); 49 72 $runner->run($app); … … 51 74 52 75 sub start_server { 53 my $config = shift; 76 my $config = get_config(); 77 54 78 require BookmarksApp; 55 79 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 80 63 81 my $runner = Plack::Runner->new(server => 'Starman'); 64 82 $runner->parse_options( 65 83 '--daemonize', 66 '--listen', $listen,67 '--pid', $ pid_file,68 '--error-log', $ error_log,69 '--access-log', $ access_log,84 '--listen', ':' . $config->{port}, 85 '--pid', $config->{pid_file}, 86 '--error-log', $config->{error_log}, 87 '--access-log', $config->{access_log}, 70 88 ); 71 89 $runner->run($app); 72 90 } 73 91 74 sub s top_server {75 my $config = shift;76 my $s erver_root = $config->{server_root} || '.';92 sub signal_server { 93 my $config = get_config(); 94 my $signal = shift; 77 95 78 my $pid_path = catfile($server_root, 'pid');96 my $pid_path = $config->{pid_file}; 79 97 unless (-e $pid_path) { 80 98 warn "$pid_path does not exist\n"; … … 87 105 88 106 if (my $pid = $pid_file->running) { 89 kill 'TERM', $pid;107 kill $signal, $pid; 90 108 } 91 109 }
Note: See TracChangeset
for help on using the changeset viewer.