]> ruin.nu Git - ndwebbie.git/blob - script/ndweb_server.pl
7e5aa21ec94235571faa1ceaf0571b3d9b621318
[ndwebbie.git] / script / ndweb_server.pl
1 #!/usr/bin/env perl
2
3 BEGIN {
4     $ENV{CATALYST_ENGINE} ||= 'HTTP';
5     $ENV{CATALYST_SCRIPT_GEN} = 38;
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     );
106
107     $restarter->run_and_watch;
108 }
109 else {
110     $runner->();
111 }
112
113 1;
114
115 =head1 NAME
116
117 ndweb_server.pl - Catalyst Testserver
118
119 =head1 SYNOPSIS
120
121 ndweb_server.pl [options]
122
123  Options:
124    -d -debug          force debug mode
125    -f -fork           handle each request in a new process
126                       (defaults to false)
127    -? -help           display this help and exits
128       -host           host (defaults to all)
129    -p -port           port (defaults to 3000)
130    -k -keepalive      enable keep-alive connections
131    -r -restart        restart when files get modified
132                       (defaults to false)
133    -rd -restartdelay  delay between file checks
134                       (ignored if you have Linux::Inotify2 installed)
135    -rr -restartregex  regex match files that trigger
136                       a restart when modified
137                       (defaults to '\.yml$|\.yaml$|\.conf|\.pm$')
138    -restartdirectory  the directory to search for
139                       modified files, can be set mulitple times
140                       (defaults to '[SCRIPT_DIR]/..')
141    -follow_symlinks   follow symlinks in search directories
142                       (defaults to false. this is a no-op on Win32)
143    -background        run the process in the background
144    -pidfile           specify filename for pid file
145
146  See also:
147    perldoc Catalyst::Manual
148    perldoc Catalyst::Manual::Intro
149
150 =head1 DESCRIPTION
151
152 Run a Catalyst Testserver for this application.
153
154 =head1 AUTHORS
155
156 Catalyst Contributors, see Catalyst.pm
157
158 =head1 COPYRIGHT
159
160 This library is free software. You can redistribute it and/or modify
161 it under the same terms as Perl itself.
162
163 =cut