source: bookmarks/trunk/bin/bkmkd @ 122

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

#7: Added an init mode to bkmkd.

Writes a new config file to the file given with the -f switch (defaults to "server.yml") and the built-in default settings. These settings can be overridden using the -D command line switch.

http://trac.echodin.net/ticket/7

  • Property svn:executable set to *
File size: 2.7 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 %default_config = (
21    server_root => cwd,
22    port        => 5000,
23    access_log  => 'access',
24    error_log   => 'error',
25    pid_file    => 'pid',
26);
27
28my %run = (
29    init    => sub {
30        $CONFIG_FILE ||= 'server.yml';
31        my $config = {
32            %default_config,
33            %DEFINES,
34        };
35        print Dump($config) if $VERBOSE;
36        YAML::DumpFile($CONFIG_FILE, $config);
37        print "Wrote config to $CONFIG_FILE\n" if $VERBOSE;
38    },
39    run     => \&run_server,
40    start   => \&start_server,
41    stop    => sub { signal_server('QUIT') },
42    restart => sub { signal_server('HUP') },
43);
44
45my $command = shift or die "Usage: $0 <" . join('|', keys %run) . ">\n";
46
47exists $run{$command} or die "Unrecognized command $command\n";
48$run{$command}->();
49
50sub get_config {
51    $CONFIG_FILE ||= 'server.yml';
52    my $config_from_file = -e $CONFIG_FILE ? YAML::LoadFile($CONFIG_FILE) : {};
53
54    my $config = {
55        %default_config,
56        %{ $config_from_file },
57        %DEFINES,
58    };
59
60    # make config paths absolute before handing off to the app
61    for my $file_key (qw{dbname htdigest access_log error_log pid_file}) {
62        if ($config->{$file_key}) {
63            $config->{$file_key} = rel2abs($config->{$file_key}, $config->{server_root});
64        }
65    }
66
67    warn Dump($config) if $VERBOSE;
68
69    return $config;
70}
71
72sub run_server {
73    my $config = get_config();
74
75    require BookmarksApp;
76    my $app = BookmarksApp->new({ config => $config })->to_app;
77
78    my $runner = Plack::Runner->new(server => 'Starman');
79    $runner->parse_options(
80        '--listen', ':' . $config->{port},
81    );
82    $runner->run($app);
83}
84
85sub start_server {
86    my $config = get_config();
87
88    require BookmarksApp;
89    my $app = BookmarksApp->new({ config => $config })->to_app;
90
91    my $runner = Plack::Runner->new(server => 'Starman');
92    $runner->parse_options(
93        '--daemonize',
94        '--listen',     ':' . $config->{port},
95        '--pid',        $config->{pid_file},
96        '--error-log',  $config->{error_log},
97        '--access-log', $config->{access_log},
98    );
99    $runner->run($app);
100}
101
102sub signal_server {
103    my $config = get_config();
104    my $signal = shift;
105
106    my $pid_path = $config->{pid_file};
107    unless (-e $pid_path) {
108        warn "$pid_path does not exist\n";
109        return;
110    }
111
112    my $pid_file = File::Pid->new({
113        file => $pid_path,
114    });
115
116    if (my $pid = $pid_file->running) {
117        kill $signal, $pid;
118    }
119}
Note: See TracBrowser for help on using the repository browser.