/[Frey]/trunk/lib/Frey/Server.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/lib/Frey/Server.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 571 by dpavlin, Thu Nov 27 22:29:01 2008 UTC revision 625 by dpavlin, Sat Nov 29 17:48:54 2008 UTC
# Line 2  package Frey::Server; Line 2  package Frey::Server;
2    
3  use Moose;  use Moose;
4  extends 'Frey';  extends 'Frey';
 with 'Frey::Web';  
5  with 'Frey::Config';  with 'Frey::Config';
6    
7  use Continuity;  use Continuity;
# Line 35  This is one of pissible server implement Line 34  This is one of pissible server implement
34    
35  =cut  =cut
36    
37    our $editor = Frey::Editor->new;
38    our $port;
39    
40  sub run {  sub run {
41          my ( $self, $port ) = @_;          my ( $self ) = @_;
42    
43            $port = $ENV{FREY_PORT} || $self->config->{port} || 16001;
44    
45          $server = Continuity->new(          $server = Continuity->new(
46                  port => $port || $self->config->{port} || 16001,                  port => $port,
47                  path_session => 1,                  path_session => 1,
48                  cookie_session => 'sid',                  cookie_session => 'sid',
49                  callback => \&main,                  callback => \&main,
# Line 49  sub run { Line 54  sub run {
54          );          );
55          $Module::Reload::Debug = 1; # auto if debug_level > 1          $Module::Reload::Debug = 1; # auto if debug_level > 1
56          Frey::ClassLoader->new->load_all_classes();          Frey::ClassLoader->new->load_all_classes();
57            $editor->switch_screen if $ENV{FREY_SWITCH_SCREEN};
58          $server->loop;          $server->loop;
59  }  }
60    
# Line 66  sub main { Line 72  sub main {
72    
73          my $path = $req->request->url->path;          my $path = $req->request->url->path;
74    
75          eval {  #       eval {
76            {
                 sub refresh {  
                         my ( $url, $time ) = @_;  
                         $url  ||= '/';  
                         $time ||= 1;  
                         warn "# refresh $url";  
                         qq|  
                                 <html>  
                                 <head>  
                                         <META HTTP-EQUIV="Refresh" CONTENT="$time; URL=$url"></META>  
                                 </head>  
                                 <body>  
                                         Refresh <a href="$url"><tt>$url</tt></a> in $time sec  
                                 </body>  
                                 </html>  
                                 \n\r\n\r  
                         |; # XXX newlines at end are important to flush content to browser  
                 }  
77    
78                  if ( $path =~ m{/reload(.*)} ) {                  if ( $path =~ m{/reload(.*)} ) {
79    
# Line 92  sub main { Line 81  sub main {
81                          my $cmd = "perl -c $0";                          my $cmd = "perl -c $0";
82                          warn "# check config with $cmd";                          warn "# check config with $cmd";
83                          if ( system($cmd) == 0 ) {                          if ( system($cmd) == 0 ) {
84                                  Frey::Server->new->load_config;                                  $req->print( "\r\n" );
85                                    my $server = Frey::Server->new;
86                                    $server->load_config;
87                                    $req->print( "\r\n" );
88                                  Module::Reload->check;                                  Module::Reload->check;
89                                    $req->print( "\r\n" );
90                                  $req->print( refresh( $1, 1 ) );                                  $req->print( refresh( $1, 1 ) );
91                                    $req->print( "\r\n" );
92                                  warn "# reload done";                                  warn "# reload done";
93                                  return;                                  return;
94                          } else {                          } else {
# Line 106  sub main { Line 100  sub main {
100                          # FIXME do we need some kind of check here for production? :-)                          # FIXME do we need some kind of check here for production? :-)
101                          # ./bin/dev.sh will restart us during development                          # ./bin/dev.sh will restart us during development
102                          $req->print( refresh( $1, 2 ) );                          $req->print( refresh( $1, 2 ) );
103                            $req->print( "\r\n" );
104                          exit;                          exit;
105                  }                  }
106    
# Line 123  sub main { Line 118  sub main {
118    
119                  my $f;                  my $f;
120    
                 my $editor = Frey::Editor->new;  
   
121                  # shared run params                  # shared run params
122                  my $run = {                  my $run = {
123                          request_url => $req->request->url,                          request_url => $req->request->url,
124                          debug => 1,  #                       debug => 1,
125                  };                  };
126    
127                  if (                  if (
# Line 143  sub main { Line 136  sub main {
136                          $f->request( $req );                          $f->request( $req );
137                  } elsif ( $path =~ $editor->url_regex ) {                  } elsif ( $path =~ $editor->url_regex ) {
138                          $req->print( $editor->command( $path ) );                          $req->print( $editor->command( $path ) );
                         system( $editor->command( $path ) );  
139                          return;                          return;
140                  } elsif (                  } elsif (
141                          $path =~ m{/([^/]+)/(as_\w+)/?([^/]+)?}                          $path =~ m{/([^/]+)/(\w*as_\w+)/?([^/]+)?}
142                  ) {                  ) {
143                          my $class = rest2class $1;                          my $class = rest2class $1;
144                          warn "# run $path -> $class $2";                          warn "# run $path -> $class $2";
145                          $run->{format} = $3 if $3;                          $run->{format} = $3 if $3;
146                          $params{request_url} = $req->request->url;                          $params{request_url} = $req->request->url;
147                          $req->print( "\r\n\r\n" ); # 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
148                          $f = Frey::Run->new( class => $class, params => \%params, run => $2, %$run );                          $run->{$_} = $params{$_} foreach keys %params;
149                            $f = Frey::Run->new( class => $class, params => $run, run => $2, request_url => $req->request->url );
150                  } elsif (                  } elsif (
151                          $path =~ m{/([^/]+)/?$}                          $path =~ m{/([^/]+)/?$}
152                  ) {                  ) {
153                          my $class = rest2class $1;                          my $class = rest2class $1;
154                          warn "# introspect $class";                          warn "# introspect $class";
155                          $f = Frey::Run->new( class => 'Frey::Introspect', params => { class => $class }, %$run );                          $run->{class} ||= $class;
156                            $f = Frey::Run->new( class => 'Frey::Introspect', params => $run, request_url => $req->request->url );
157                  } else {                  } else {
158                          $f = Frey::Run->new( class => 'Frey::ClassBrowser', %$run );                          $f = Frey::Run->new( class => 'Frey::ClassBrowser', params => $run, request_url => $req->request->url );
159                  }                  }
160    
161                  if ( $f ) {                  if ( $f ) {
162                          $f->clean_status;                          $f->clean_status;
163                          $f->add_status( { request => $req } );                          $f->add_status( { request => $req } );
164                          warn "## status ", dump( map { keys %$_ } $f->status );                          $f->status_parts;
165                          my $html = $f->html;                          if ( my $html = $f->html ) {
166                          die "no html output" unless $html;                                  warn "## html ",length($html)," bytes";
                         warn "## html ",length($html)," bytes";  
                         eval {  
167                                  $req->print( $html );                                  $req->print( $html );
168                          };                          } else {
169                          die "can't send to wire: $@" if $@;                                  confess "no output from $f";
170                            }
171                  } else {                  } else {
172                          warn "# can't call request on nothing!";                          confess "# can't call request on nothing!";
173                  }                  }
174    
175          };          };
176    
177          if ( $@ ) {          if ( $@ ) {
178                  warn $@;                  warn "SERVER ERROR: $@";
179                  $req->conn->send_error( 404 );  # FIXME this should probably be 500, but we can't ship page with it  #               $req->conn->send_error( 404 );  # FIXME this should probably be 500, but we can't ship page with it
180                  $req->print( qq{<pre class="frey-error">$@<pre>} );                  $req->print( qq{<pre class="frey-error">$@<pre>\r\n\r\n} );
181  #               Carp::REPL::repl;  #               Carp::REPL::repl;
182          }          }
183    
# Line 199  sub main { Line 192  sub main {
192          }          }
193    
194          if ( $req->conn ) {          if ( $req->conn ) {
195                    $req->print( "\r\n" ); # flush
196                  $req->conn->close;                  $req->conn->close;
197                  warn "## close connection: $@";                  warn "## close connection: $@";
198          }          }
# Line 241  sub send_message { Line 235  sub send_message {
235    }    }
236  }  }
237    
238    sub refresh {
239            my ( $url, $time ) = @_;
240            $url  ||= '/';
241            $time ||= 1;
242            warn "# refresh $url";
243            qq|
244                    <html>
245                    <head>
246                            <META HTTP-EQUIV="Refresh" CONTENT="$time; URL=$url"></META>
247                    </head>
248                    <body>
249                            Refresh <a href="$url"><tt>$url</tt></a> in $time sec
250                    </body>
251                    </html>
252                    \n\r\n\r
253            |; # XXX newlines at end are important to flush content to browser
254    }
255    
256  1;  1;

Legend:
Removed from v.571  
changed lines
  Added in v.625

  ViewVC Help
Powered by ViewVC 1.1.26