]> ruin.nu Git - ndwebbie.git/blob - script/ndweb_server.pl
940cf86e69f155024a37c6780c3d349b59634930
[ndwebbie.git] / script / ndweb_server.pl
1 #!/usr/bin/env perl
2
3 BEGIN {
4     $ENV{CATALYST_ENGINE} ||= 'HTTP';
5     $ENV{CATALYST_SCRIPT_GEN} = 39;
6     require Catalyst::Engine::HTTP;
7 }
8
9 use strict;
10 use warnings;
11 use Getopt::Long;
12 use Pod::Usage;
13 use FindBin;
14 use lib "$FindBin::Bin/../lib";
15
16 #Need to preload, otherwise the first hit is slow
17 use CGI qw/:standard/;
18 escapeHTML('');
19
20 my $debug             = 0;
21 my $fork              = 0;
22 my $help              = 0;
23 my $host              = undef;
24 my $port              = $ENV{NDWEB_PORT} || $ENV{CATALYST_PORT} || 3000;
25 my $keepalive         = 0;
26 my $restart           = $ENV{NDWEB_RELOAD} || $ENV{CATALYST_RELOAD} || 0;
27 my $background        = 0;
28 my $pidfile           = undef;
29
30 my $check_interval;
31 my $file_regex;
32 my $watch_directory;
33 my $follow_symlinks;
34
35 my @argv = @ARGV;
36
37 GetOptions(
38     'debug|d'             => \$debug,
39     'fork|f'              => \$fork,
40     'help|?'              => \$help,
41     'host=s'              => \$host,
42     'port|p=s'            => \$port,
43     'keepalive|k'         => \$keepalive,
44     'restart|r'           => \$restart,
45     'restartdelay|rd=s'   => \$check_interval,
46     'restartregex|rr=s'   => \$file_regex,
47     'restartdirectory=s@' => \$watch_directory,
48     'followsymlinks'      => \$follow_symlinks,
49     'background'          => \$background,
50     'pidfile=s'           => \$pidfile,
51 );
52
53 pod2usage(1) if $help;
54
55 if ( $debug ) {
56     $ENV{CATALYST_DEBUG} = 1;
57 }
58
59 # If we load this here, then in the case of a restarter, it does not
60 # need to be reloaded for each restart.
61 require Catalyst;
62
63 # If this isn't done, then the Catalyst::Devel tests for the restarter
64 # fail.
65 $| = 1 if $ENV{HARNESS_ACTIVE};
66
67 my $runner = sub {
68     # This is require instead of use so that the above environment
69     # variables can be set at runtime.
70     require NDWeb;
71
72     NDWeb->run(
73         $port, $host,
74         {
75             argv       => \@argv,
76             'fork'     => $fork,
77             keepalive  => $keepalive,
78             background => $background,
79             pidfile    => $pidfile,
80         }
81     );
82 };
83
84 if ( $restart ) {
85     die "Cannot run in the background and also watch for changed files.\n"
86         if $background;
87
88     require Catalyst::Restarter;
89
90     my $subclass = Catalyst::Restarter->pick_subclass;
91
92     my %args;
93     $args{follow_symlinks} = 1
94         if $follow_symlinks;
95     $args{directories} = $watch_directory
96         if defined $watch_directory;
97     $args{sleep_interval} = $check_interval
98         if defined $check_interval;
99     $args{filter} = qr/$file_regex/
100         if defined $file_regex;
101
102     my $restarter = $subclass->new(
103         %args,
104         start_sub => $runner,
105         argv      => \@argv,
106     );
107
108     $restarter->run_and_watch;
109 }
110 else {
111     $runner->();
112 }
113
114 1;
115
116 =head1 NAME
117
118 ndweb_server.pl - Catalyst Testserver
119
120 =head1 SYNOPSIS
121
122 ndweb_server.pl [options]
123
124  Options:
125    -d -debug          force debug mode
126    -f -fork           handle each request in a new process
127                       (defaults to false)
128    -? -help           display this help and exits
129       -host           host (defaults to all)
130    -p -port           port (defaults to 3000)
131    -k -keepalive      enable keep-alive connections
132    -r -restart        restart when files get modified
133                       (defaults to false)
134    -rd -restartdelay  delay between file checks
135                       (ignored if you have Linux::Inotify2 installed)
136    -rr -restartregex  regex match files that trigger
137                       a restart when modified
138                       (defaults to '\.yml$|\.yaml$|\.conf|\.pm$')
139    -restartdirectory  the directory to search for
140                       modified files, can be set mulitple times
141                       (defaults to '[SCRIPT_DIR]/..')
142    -follow_symlinks   follow symlinks in search directories
143                       (defaults to false. this is a no-op on Win32)
144    -background        run the process in the background
145    -pidfile           specify filename for pid file
146
147  See also:
148    perldoc Catalyst::Manual
149    perldoc Catalyst::Manual::Intro
150
151 =head1 DESCRIPTION
152
153 Run a Catalyst Testserver for this application.
154
155 =head1 AUTHORS
156
157 Catalyst Contributors, see Catalyst.pm
158
159 =head1 COPYRIGHT
160
161 This library is free software. You can redistribute it and/or modify
162 it under the same terms as Perl itself.
163
164 =cut