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

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

revision 267 by dpavlin, Wed Nov 5 08:20:37 2008 UTC revision 636 by dpavlin, Sun Nov 30 14:42:43 2008 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 Frey::Server;
11    
12      # Try to find a static file  use Data::Dump qw/dump/;
13      $self->static->dispatch($c);  
14    __PACKAGE__->attr(
15      # Use routes if we don't have a response code yet      static => (                            
16      $self->routes->dispatch($c) unless $c->res->code;          chained => 1,                      
17            default => sub { MojoX::Dispatcher::Static->new }
18      # Nothing found      )
19      unless ($c->res->code) {  );
20          $self->static->serve($c, '/404.html');  
21          $c->res->code(404);  sub new {
22      }      my $self = shift->SUPER::new();
23    
24        # This app should log only errors to STDERR
25        $self->log->level('error');
26        $self->log->path(undef);
27    
28            warn "# home ", $self->home;
29    
30            $self->static->root( './' );
31    
32        return $self;
33  }  }
34    
35  # This method will run once at server start  sub handler {
36  sub startup {          my ($self, $tx) = @_;
37      my $self = shift;  
38            if ( $self->static->dispatch($tx) ) {
39      # The routes  #               warn "# static ",dump( $tx );
40      my $r = $self->routes;                  return $tx;
41            }
42      # Default route  
43      $r->route('/:controller/:action/:id')          my $body;
44        ->to(controller => 'example', action => 'welcome', id => 1);  
45            my $server = Frey::Server->new;
46            $server->{_print} = sub {
47                    $body .= join("\n", @_);
48            };
49    
50            my $url = $tx->req->url->to_string;
51            my $params = $tx->req->params->to_hash;
52    
53            warn "# url $url params ",dump($params);
54    
55            $server->request( $url, $params ); # fetch body
56    
57    =for developer
58    
59            # compatiblity with unpatched Mojo
60            sub class2rest {
61                    my $c = shift;
62                    $c =~ s/::/-/gs;
63                    $c;
64            }
65            $body =~ s{(/\w+::\w+[\w:]+)}{class2rest($1)}sge;
66    
67    =cut
68    
69            $tx->res->code(200);
70            $tx->res->headers->content_type('text/html');
71            $tx->res->body( $body );
72    
73            warn dump( $tx->res->headers );
74            return $tx;
75  }  }
76    
77  1;  1;

Legend:
Removed from v.267  
changed lines
  Added in v.636

  ViewVC Help
Powered by ViewVC 1.1.26