--- trunk/lib/Frey/Mojo.pm 2008/11/30 14:42:43 636 +++ trunk/lib/Frey/Mojo.pm 2009/06/30 21:30:02 1138 @@ -7,7 +7,9 @@ use MojoX::Dispatcher::Static; +use lib 'lib'; use Frey::Server; +use Frey::CouchAPI; use Data::Dump qw/dump/; @@ -25,17 +27,43 @@ $self->log->level('error'); $self->log->path(undef); - warn "# home ", $self->home; +# warn "# home ", $self->home; $self->static->root( './' ); return $self; } + sub handler { my ($self, $tx) = @_; - if ( $self->static->dispatch($tx) ) { + # 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; } @@ -50,9 +78,24 @@ my $url = $tx->req->url->to_string; my $params = $tx->req->params->to_hash; - warn "# url $url params ",dump($params); + 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 - $server->request( $url, $params ); # fetch body + warn "# request ", dump( $request ); + + foreach ( 'content_type', 'code' ) { + die "missing $_" unless defined $request->{$_}; + } =for developer @@ -66,8 +109,8 @@ =cut - $tx->res->code(200); - $tx->res->headers->content_type('text/html'); + $tx->res->code( $request->{code} ); + $tx->res->headers->content_type( $request->{content_type} ); $tx->res->body( $body ); warn dump( $tx->res->headers );