source: bookmarks/trunk/bin/bkmkd @ 118

Last change on this file since 118 was 118, checked in by peter, 8 years ago

Moved the digest_key generation into the BookmarksApp, so that it only happens on app startup.

  • Property svn:executable set to *
File size: 2.5 KB
RevLine 
[93]1#!/usr/bin/perl -w
2use strict;
[54]3
[93]4use FindBin;
[103]5use lib "$FindBin::RealBin/../lib";
[65]6
[94]7use Getopt::Long;
8use YAML;
[93]9use Plack::Runner;
[103]10use File::Pid;
[115]11use File::Spec::Functions qw{catfile rel2abs};
12use Cwd;
[93]13
[94]14GetOptions(
[115]15    'file=s'    => \my $CONFIG_FILE,
16    'verbose|v' => \my $VERBOSE,
17    'D=s'       => \my %DEFINES,
[94]18);
[93]19
[103]20my %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]27my %default_config = (
28    server_root => cwd,
29    port        => 5000,
30    access_log  => 'access',
31    error_log   => 'error',
32    pid_file    => 'pid',
33);
34
[110]35my $command = shift or die "Usage: $0 <" . join('|', keys %run) . ">\n";
36
[104]37exists $run{$command} or die "Unrecognized command $command\n";
[115]38$run{$command}->();
[103]39
[115]40sub 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
[110]62sub run_server {
[115]63    my $config = get_config();
64
[104]65    require BookmarksApp;
66    my $app = BookmarksApp->new({ config => $config })->to_app;
[103]67
[110]68    my $runner = Plack::Runner->new(server => 'Starman');
69    $runner->parse_options(
[115]70        '--listen', ':' . $config->{port},
[110]71    );
72    $runner->run($app);
73}
74
75sub start_server {
[115]76    my $config = get_config();
77
[110]78    require BookmarksApp;
79    my $app = BookmarksApp->new({ config => $config })->to_app;
80
[104]81    my $runner = Plack::Runner->new(server => 'Starman');
82    $runner->parse_options(
83        '--daemonize',
[115]84        '--listen',     ':' . $config->{port},
85        '--pid',        $config->{pid_file},
86        '--error-log',  $config->{error_log},
87        '--access-log', $config->{access_log},
[104]88    );
89    $runner->run($app);
90}
[103]91
[115]92sub signal_server {
93    my $config = get_config();
94    my $signal = shift;
[103]95
[115]96    my $pid_path = $config->{pid_file};
[104]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) {
[115]107        kill $signal, $pid;
[104]108    }
109}
Note: See TracBrowser for help on using the repository browser.