/[Frey]/branches/zimbardo/lib/Frey/Mojo.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 /branches/zimbardo/lib/Frey/Mojo.pm

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

revision 627 by dpavlin, Sat Nov 29 22:02:08 2008 UTC revision 1046 by dpavlin, Wed Apr 22 22:01:06 2009 UTC
# Line 3  package Frey::Mojo; Line 3  package Frey::Mojo;
3  use strict;  use strict;
4  use warnings;  use warnings;
5    
6  use base 'Mojolicious';  use base 'Mojo';
7    
8  # This method will run for each request  use MojoX::Dispatcher::Static;
9  sub dispatch {  
10      my ($self, $c) = @_;  use lib 'lib';
11    use Frey::Server;
12      # Try to find a static file  use Frey::CouchAPI;
13      $self->static->dispatch($c);  
14    use Data::Dump qw/dump/;
15      # Use routes if we don't have a response code yet  
16      $self->routes->dispatch($c) unless $c->res->code;  __PACKAGE__->attr(
17        static => (                            
18      # Nothing found          chained => 1,                      
19      unless ($c->res->code) {          default => sub { MojoX::Dispatcher::Static->new }
20          $self->static->serve($c, '/404.html');      )
21          $c->res->code(404);  );
     }  
 }  
22    
23  # This method will run once at server start  sub new {
24  sub startup {      my $self = shift->SUPER::new();
     my $self = shift;  
25    
26      # The routes      # This app should log only errors to STDERR
27      my $r = $self->routes;      $self->log->level('error');
28        $self->log->path(undef);
29    
30  #       $r->route('/:action/:class/:controller')  #       warn "# home ", $self->home;
31  #               ->to(controller => 'run', action => 'as_markup', class => 'Frey::ClassBrowser' );  
32            $self->static->root( './' );
33    
34        return $self;
35    }
36    
 #       $r->route('/')  
 #               ->to(controller => 'run', action => 'as_markup', class => 'Frey::ClassBrowser');  
37    
38          $r->route('/:class')  sub handler {
39                  ->to(controller => 'run', action => 'as_markup', class => 'Frey::Introspect' );          my ($self, $tx) = @_;
40    
41      # Default route          # XXX fake app so static dispatcher won't die on us
42          $r->route('/:class/:action/:controller')          {
43                  ->to(controller => 'run', action => 'as_markup', class => 'Frey::ClassBrowser');                  package Fake::App;
44                    use base 'Mojo::Transaction';
45                    sub app {
46                            my $self = shift;
47    #                       warn "## $self app ", @_;
48                            $self;
49                    }
50                    sub log {
51                            my $self = shift;
52    #                       warn "## $self log ",@_;
53                            return $self;
54                    }
55                    sub debug {
56                            my $self = shift;
57                            warn "## ",@_, $/;
58                            return $self;
59                    }
60            }
61            bless $tx, 'Fake::App';
62    
63            # rewrite URL
64            Frey::CouchAPI->rewrite_urls( $tx );
65    
66            if ( $self->static->dispatch($tx) ) {
67    #               warn "# static ",dump( $tx );
68                    return $tx;
69            }
70    
71            my $body;
72    
73            my $server = Frey::Server->new;
74            $server->{_print} = sub {
75                    $body .= join("\n", @_);
76            };
77    
78            my $url = $tx->req->url->to_string;
79            my $params = $tx->req->params->to_hash;
80    
81            my $referer = $tx->req->content->headers->header('Referer');
82            my $ajax = $tx->req->content->headers->header('X-Requested-With');
83            warn "# referer $referer\n";
84            warn "# headers = ", dump( $tx->req->content->headers );
85    
86            if ( $referer =~ m{/_utils} || $ajax =~ m{XMLHttpRequest}i ) {
87                    return Frey::CouchAPI->dispatch( $tx );
88            }
89    
90            warn "# url $url from $referer params ",dump($params);
91    
92            my $request = $server->request( $url, $params ); # fetch body
93    
94            warn "# request ", dump( $request );
95    
96            foreach ( 'content_type', 'code' ) {
97                    die "missing $_" unless defined $request->{$_};
98            }
99    
100    =for developer
101    
102            # compatiblity with unpatched Mojo
103            sub class2rest {
104                    my $c = shift;
105                    $c =~ s/::/-/gs;
106                    $c;
107            }
108            $body =~ s{(/\w+::\w+[\w:]+)}{class2rest($1)}sge;
109    
110    =cut
111    
112            $tx->res->code( $request->{code} );
113            $tx->res->headers->content_type( $request->{content_type} );
114            $tx->res->body( $body );
115    
116            warn dump( $tx->res->headers );
117            return $tx;
118  }  }
119    
120  1;  1;

Legend:
Removed from v.627  
changed lines
  Added in v.1046

  ViewVC Help
Powered by ViewVC 1.1.26