--- trunk/lib/Frey/Mojo.pm 2008/11/30 01:32:56 632 +++ trunk/lib/Frey/Mojo.pm 2008/11/30 01:46:13 633 @@ -3,45 +3,75 @@ use strict; use warnings; -use base 'Mojolicious'; +use base 'Mojo'; -# This method will run for each request -sub dispatch { - my ($self, $c) = @_; - - # Try to find a static file - $self->static->dispatch($c); - - # Use routes if we don't have a response code yet - $self->routes->dispatch($c) unless $c->res->code; - - # Nothing found - unless ($c->res->code) { - $self->static->serve($c, '/404.html'); - $c->res->code(404); - } +use MojoX::Dispatcher::Static; + +use Frey::Server; + +use Data::Dump qw/dump/; + +__PACKAGE__->attr( + static => ( + chained => 1, + default => sub { MojoX::Dispatcher::Static->new } + ) +); + +sub new { + my $self = shift->SUPER::new(); + + # This app should log only errors to STDERR + $self->log->level('error'); + $self->log->path(undef); + + warn "# home ", $self->home; + + $self->static->root( './' ); + + return $self; } -# This method will run once at server start -sub startup { - my $self = shift; +sub handler { + my ($self, $tx) = @_; + + if ( $self->static->dispatch($tx) ) { + warn "# static ",dump( $tx ); + return $tx; + } + + my $body; + + my $server = Frey::Server->new; + $server->{_print} = sub { + $body .= join("\n", @_); + }; + + my $url = $tx->req->url->to_string; + my $params = $tx->req->params; + + warn "# url $url params ",dump($params); - # The routes - my $r = $self->routes; + $server->request( $url, $params ); # fetch body -# $r->route('/:action/:class/:controller') -# ->to(controller => 'run', action => 'as_markup', class => 'Frey::ClassBrowser' ); +=for developer -# $r->route('/') -# ->to(controller => 'run', action => 'as_markup', class => 'Frey::ClassBrowser'); + # compatiblity with unpatched Mojo + sub class2rest { + my $c = shift; + $c =~ s/::/-/gs; + $c; + } + $body =~ s{(/\w+::\w+[\w:]+)}{class2rest($1)}sge; - $r->route('/:class') - ->to(controller => 'run', action => 'as_markup', class => 'Frey::Introspect' ); +=cut - # Default route - $r->route('/:class/:action/:controller') - ->to(controller => 'run', action => 'as_markup', class => 'Frey::ClassBrowser'); + $tx->res->code(200); + $tx->res->headers->content_type('text/html'); + $tx->res->body( $body ); + warn dump( $tx->res->headers ); + return $tx; } 1;