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