moosified server template.
[catagits/Catalyst-Devel.git] / share / script / myapp_server.pl.tt
1 package [% appprefix %]::Script::Server;
2
3 use Catalyst::Engine::HTTP;
4 use [% appprefix %];
5 use Moose;
6
7 with 'MooseX::GetOpt';
8
9 has argv => ( isa => 'ArrayRef', is => 'ro', required => 1 );
10 has [qw/ fork background keepalive /] => ( isa => 'Bool', is => 'ro', required => 1, default => 0 );
11 has pidfile => ( isa => 'Str', required => 0, is => 'ro' );
12
13 sub run {
14     my $self = shift;
15     [% appprefix %]->run(
16         $port, $host,
17         {
18             argv       => $self->argv,
19             'fork'     => $self->fork,
20             keepalive  => $self->keepalive,
21             background => $self->background,
22             pidfile    => $self->pidfile,
23         }
24     );
25 }
26
27 __PACKAGE__->new_with_options->run;
28
29
30 pod2usage(1) if $help;
31
32 if ( $debug ) {
33     $ENV{CATALYST_DEBUG} = 1;
34 }
35
36 # If we load this here, then in the case of a restarter, it does not
37 # need to be reloaded for each restart.
38 require Catalyst;
39
40 # If this isn't done, then the Catalyst::Devel tests for the restarter
41 # fail.
42 $| = 1 if $ENV{HARNESS_ACTIVE};
43
44 my $runner = sub {
45     # This is require instead of use so that the above environment
46     # variables can be set at runtime.
47     require [% name %];
48
49     [% name %]->run(
50         $port, $host,
51         {
52             argv       => \@argv,
53             'fork'     => $fork,
54             keepalive  => $keepalive,
55             background => $background,
56             pidfile    => $pidfile,
57         }
58     );
59 };
60
61 if ( $restart ) {
62     die "Cannot run in the background and also watch for changed files.\n"
63         if $background;
64
65     require Catalyst::Restarter;
66
67     my $subclass = Catalyst::Restarter->pick_subclass;
68
69     my %args;
70     $args{follow_symlinks} = 1
71         if $follow_symlinks;
72     $args{directories} = $watch_directory
73         if defined $watch_directory;
74     $args{sleep_interval} = $check_interval
75         if defined $check_interval;
76     $args{filter} = qr/$file_regex/
77         if defined $file_regex;
78
79     my $restarter = $subclass->new(
80         %args,
81         start_sub => $runner,
82     );
83
84     $restarter->run_and_watch;
85 }
86 else {
87     $runner->();
88 }
89
90 1;
91
92 =head1 NAME
93
94 [% appprefix %]_server.pl - Catalyst Testserver
95
96 =head1 SYNOPSIS
97
98 [% appprefix %]_server.pl [options]
99
100  Options:
101    -d -debug          force debug mode
102    -f -fork           handle each request in a new process
103                       (defaults to false)
104    -? -help           display this help and exits
105       -host           host (defaults to all)
106    -p -port           port (defaults to 3000)
107    -k -keepalive      enable keep-alive connections
108    -r -restart        restart when files get modified
109                       (defaults to false)
110    -rd -restartdelay  delay between file checks
111                       (ignored if you have Linux::Inotify2 installed)
112    -rr -restartregex  regex match files that trigger
113                       a restart when modified
114                       (defaults to '\.yml$|\.yaml$|\.conf|\.pm$')
115    -restartdirectory  the directory to search for
116                       modified files, can be set mulitple times
117                       (defaults to '[SCRIPT_DIR]/..')
118    -follow_symlinks   follow symlinks in search directories
119                       (defaults to false. this is a no-op on Win32)
120    -background        run the process in the background
121    -pidfile           specify filename for pid file
122
123  See also:
124    perldoc Catalyst::Manual
125    perldoc Catalyst::Manual::Intro
126
127 =head1 DESCRIPTION
128
129 Run a Catalyst Testserver for this application.
130
131 =head1 AUTHORS
132
133 Catalyst Contributors, see Catalyst.pm
134
135 =head1 COPYRIGHT
136
137 This library is free software. You can redistribute it and/or modify
138 it under the same terms as Perl itself.
139
140 =cut