added -daemonise patch from Ton Voon
Kieren Diment [Wed, 18 Oct 2006 12:19:23 +0000 (12:19 +0000)]
<ton.voon@altinity.com> and cookbook recipe to go with it to built in server

lib/Catalyst/Engine/HTTP.pm
lib/Catalyst/Manual/Cookbook.pod

index afc2603..fa653d9 100644 (file)
@@ -116,6 +116,12 @@ sub run {
 
     $options ||= {};
 
+    if ($options->{background}) {
+        my $child = fork;
+        die "Can't fork: $!" unless defined($child);
+        exit if $child;
+    }
+
     my $restart = 0;
     local $SIG{CHLD} = 'IGNORE';
 
@@ -147,9 +153,27 @@ sub run {
 
     print "You can connect to your server at $url\n";
 
+    if ($options->{background}) {
+        open STDIN,  "+</dev/null" or die $!;
+        open STDOUT, ">&STDIN"     or die $!;
+        open STDERR, ">&STDIN"     or die $!;
+        if ( $^O !~ /MSWin32/ ) {
+             require POSIX;
+             POSIX::setsid()
+                 or die "Can't start a new session: $!";
+        }
+    }
+
+    if (my $pidfile = $options->{pidfile}) {
+        if (! open PIDFILE, "> $pidfile") {
+            warn("Cannot open: $pidfile: $!");
+        }
+        print PIDFILE "$$\n";
+        close PIDFILE;
+    }
+
     $self->_keep_alive( $options->{keepalive} || 0 );
 
-    my $parent = $$;
     my $pid    = undef;
     while ( accept( Remote, $daemon ) )
     {    # TODO: get while ( my $remote = $daemon->accept ) to work
index 24a515b..06a6dbe 100644 (file)
@@ -1014,11 +1014,11 @@ Now, go ahead and make RSS feeds for all your stuff. The world *needs*
 updates on your goldfish!
 
 =head2 Forcing the browser to download content
+
 Sometimes you need your application to send content for download. For
 example, you can generate a comma-separated values (CSV) file for your
 users to download and import into their spreadsheet program.
+
 Let's say you have an C<Orders> controller which generates a CSV file
 in the C<export> action (i.e., C<http://localhost:3000/orders/export>):
 
@@ -1679,6 +1679,64 @@ Now, we simply configure Apache to connect to the running server.
 
 L<Catalyst::Engine::FastCGI>.
 
+=head2 Development server deployment
+
+The developemnt server is a mini web server written in perl.  If you
+expect a low number of hits or you don't need mod_perl/FastCGI speed,
+you could use the development server as the application server with a
+lightweight proxy web server at the front.  However, be aware that
+there are known issues, especially with Internet Explorer.  Many of
+these issues can be dealt with by running the server with the -k
+(keepalive) option but be aware for more complex applications this may
+not be suitable.  Consider using Catalyst::Engine::POE.  This recipe
+is easily adapted for POE as well.
+
+=head3 Pros
+
+As this is an application server setup, the pros are the same as
+FastCGI (with the exception of speed).
+It is also:
+
+=head4 Simple
+
+The development server is what you create your code on, so if it works here, it should work in production!
+
+=head3 Cons
+
+=head4 Speed
+
+Not as fast as mod_perl or FastCGI. Needs to fork for each request
+that comes in - make sure static files are served by the web server to
+save forking.
+
+=head3 Setup
+
+=head4 Start up the development server
+
+   script/myapp_server.pl -p 8080 -k  -f -pidfile=/tmp/myapp.pid -daemon
+
+You will probably want to write an init script to handle stop/starting
+the app using the pid file.
+
+=head4 Configuring Apache
+
+Make sure mod_proxy is enabled and add:
+
+    # Serve static content directly
+    DocumentRoot /var/www/MyApp/root
+    Alias /static /var/www/MyApp/root/static
+
+    ProxyRequests Off
+    <Proxy *>
+        Order deny,allow
+        Allow from all
+    </Proxy>
+    ProxyPass / http://localhost:8080/
+    ProxyPassReverse / http://localhost:8080/
+
+You can wrap the above within a VirtualHost container if you want
+different apps served on the same host.
+
 =head2 Quick deployment: Building PAR Packages
 
 You have an application running on your development box, but then you