--- trunk/lib/Frey/Mojo.pm 2008/11/05 08:20:38 268 +++ trunk/lib/Frey/Mojo.pm 2009/04/22 22:01:06 1046 @@ -3,39 +3,118 @@ 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 lib 'lib'; +use Frey::Server; +use Frey::CouchAPI; + +use Data::Dump qw/dump/; + +__PACKAGE__->attr( + static => ( + chained => 1, + default => sub { MojoX::Dispatcher::Static->new } + ) +); + +sub new { + my $self = shift->SUPER::new(); -use Frey::ClassLoader; + # 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; - Frey::ClassLoader->new->load_all_classes(); +sub handler { + my ($self, $tx) = @_; - # The routes - my $r = $self->routes; + # XXX fake app so static dispatcher won't die on us + { + package Fake::App; + use base 'Mojo::Transaction'; + sub app { + my $self = shift; +# warn "## $self app ", @_; + $self; + } + sub log { + my $self = shift; +# warn "## $self log ",@_; + return $self; + } + sub debug { + my $self = shift; + warn "## ",@_, $/; + return $self; + } + } + bless $tx, 'Fake::App'; + + # rewrite URL + Frey::CouchAPI->rewrite_urls( $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->to_hash; + + my $referer = $tx->req->content->headers->header('Referer'); + my $ajax = $tx->req->content->headers->header('X-Requested-With'); + warn "# referer $referer\n"; + warn "# headers = ", dump( $tx->req->content->headers ); + + if ( $referer =~ m{/_utils} || $ajax =~ m{XMLHttpRequest}i ) { + return Frey::CouchAPI->dispatch( $tx ); + } + + warn "# url $url from $referer params ",dump($params); + + my $request = $server->request( $url, $params ); # fetch body + + warn "# request ", dump( $request ); + + foreach ( 'content_type', 'code' ) { + die "missing $_" unless defined $request->{$_}; + } + +=for developer + + # compatiblity with unpatched Mojo + sub class2rest { + my $c = shift; + $c =~ s/::/-/gs; + $c; + } + $body =~ s{(/\w+::\w+[\w:]+)}{class2rest($1)}sge; + +=cut + + $tx->res->code( $request->{code} ); + $tx->res->headers->content_type( $request->{content_type} ); + $tx->res->body( $body ); - # Default route - $r->route('/:controller/:action/:class') - ->to(controller => 'run', action => 'markup', class => 'Frey::ClassBrowser'); + warn dump( $tx->res->headers ); + return $tx; } 1;