--- trunk/lib/Frey/Server.pm 2008/11/27 22:11:13 570 +++ trunk/lib/Frey/Server.pm 2008/11/29 15:05:55 617 @@ -35,6 +35,8 @@ =cut +our $editor = Frey::Editor->new; + sub run { my ( $self, $port ) = @_; $server = Continuity->new( @@ -49,6 +51,7 @@ ); $Module::Reload::Debug = 1; # auto if debug_level > 1 Frey::ClassLoader->new->load_all_classes(); + $editor->switch_screen if $ENV{FREY_SWITCH_SCREEN}; $server->loop; } @@ -68,33 +71,20 @@ eval { - sub refresh { - my ( $url, $time ) = @_; - $url ||= '/'; - $time ||= 1; - warn "# refresh $url"; - qq| - - - - - - Refresh $url in $time sec - - - \n\r\n\r - |; # XXX newlines at end are important to flush content to browser - } - if ( $path =~ m{/reload(.*)} ) { $ENV{FREY_NO_LOG} = 1; my $cmd = "perl -c $0"; warn "# check config with $cmd"; if ( system($cmd) == 0 ) { - Frey::Server->new->load_config; + $req->print( "\r\n" ); + my $server = Frey::Server->new; + $server->load_config; + $req->print( "\r\n" ); Module::Reload->check; + $req->print( "\r\n" ); $req->print( refresh( $1, 1 ) ); + $req->print( "\r\n" ); warn "# reload done"; return; } else { @@ -106,6 +96,7 @@ # FIXME do we need some kind of check here for production? :-) # ./bin/dev.sh will restart us during development $req->print( refresh( $1, 2 ) ); + $req->print( "\r\n" ); exit; } @@ -123,8 +114,6 @@ my $f; - my $editor = Frey::Editor->new; - # shared run params my $run = { request_url => $req->request->url, @@ -143,16 +132,16 @@ $f->request( $req ); } elsif ( $path =~ $editor->url_regex ) { $req->print( $editor->command( $path ) ); - system( $editor->command( $path ) ); + $editor->command( $path ); return; } elsif ( - $path =~ m{/([^/]+)/(as_\w+)/?([^/]+)?} + $path =~ m{/([^/]+)/(\w*as_\w+)/?([^/]+)?} ) { my $class = rest2class $1; warn "# run $path -> $class $2"; $run->{format} = $3 if $3; $params{request_url} = $req->request->url; - $req->send_headers( "X-Frey: run $class" ); # send something to browser so we don't time-out + $req->print( "\r\n\r\n" ); # send something to browser so we don't time-out $f = Frey::Run->new( class => $class, params => \%params, run => $2, %$run ); } elsif ( $path =~ m{/([^/]+)/?$} @@ -167,24 +156,23 @@ if ( $f ) { $f->clean_status; $f->add_status( { request => $req } ); - warn "## status ", dump( map { keys %$_ } $f->status ); - my $html = $f->html; - die "no html output" unless $html; - warn "## html ",length($html)," bytes"; - eval { + $f->status_parts; + if ( my $html = $f->html ) { + warn "## html ",length($html)," bytes"; $req->print( $html ); - }; - die "can't send to wire: $@" if $@; + } else { + confess "no output from $f"; + } } else { - warn "# can't call request on nothing!"; + confess "# can't call request on nothing!"; } }; if ( $@ ) { - warn $@; - $req->conn->send_error( 404 ); # FIXME this should probably be 500, but we can't ship page with it - $req->print( qq{
$@
} );
+		warn "SERVER ERROR: $@";
+#		$req->conn->send_error( 404 );	# FIXME this should probably be 500, but we can't ship page with it
+		$req->print( qq{
$@
\r\n\r\n} );
 #		Carp::REPL::repl;
 	}
 
@@ -199,6 +187,7 @@
 	}
 
 	if ( $req->conn ) {
+		$req->print( "\r\n" ); # flush
 		$req->conn->close;
 		warn "## close connection: $@";
 	}
@@ -241,4 +230,22 @@
   }
 }
 
+sub refresh {
+	my ( $url, $time ) = @_;
+	$url  ||= '/';
+	$time ||= 1;
+	warn "# refresh $url";
+	qq|
+		
+		
+			
+		
+		
+			Refresh $url in $time sec
+		
+		
+		\n\r\n\r
+	|; # XXX newlines at end are important to flush content to browser
+}
+
 1;