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 |
|
13 |
$self->static->dispatch($c); |
use Data::Dump qw/dump/; |
14 |
|
|
15 |
# Use routes if we don't have a response code yet |
__PACKAGE__->attr( |
16 |
$self->routes->dispatch($c) unless $c->res->code; |
static => ( |
17 |
|
chained => 1, |
18 |
# Nothing found |
default => sub { MojoX::Dispatcher::Static->new } |
19 |
unless ($c->res->code) { |
) |
20 |
$self->static->serve($c, '/404.html'); |
); |
21 |
$c->res->code(404); |
|
22 |
} |
sub new { |
23 |
|
my $self = shift->SUPER::new(); |
24 |
|
|
25 |
|
# This app should log only errors to STDERR |
26 |
|
$self->log->level('error'); |
27 |
|
$self->log->path(undef); |
28 |
|
|
29 |
|
warn "# home ", $self->home; |
30 |
|
|
31 |
|
$self->static->root( './' ); |
32 |
|
|
33 |
|
return $self; |
34 |
} |
} |
35 |
|
|
36 |
# This method will run once at server start |
|
37 |
sub startup { |
sub handler { |
38 |
my $self = shift; |
my ($self, $tx) = @_; |
39 |
|
|
40 |
# The routes |
# XXX fake app so static dispatcher won't die on us |
41 |
my $r = $self->routes; |
{ |
42 |
|
package Fake::App; |
43 |
# Default route |
use base 'Mojo::Transaction'; |
44 |
$r->route('/:controller/:action/:id') |
sub app { |
45 |
->to(controller => 'example', action => 'welcome', id => 1); |
my $self = shift; |
46 |
|
warn "## $self app ", @_; |
47 |
|
$self; |
48 |
|
} |
49 |
|
sub log { |
50 |
|
my $self = shift; |
51 |
|
warn "## $self log ",@_; |
52 |
|
return $self; |
53 |
|
} |
54 |
|
sub debug { |
55 |
|
my $self = shift; |
56 |
|
warn "## $self debug ",@_; |
57 |
|
return $self; |
58 |
|
} |
59 |
|
} |
60 |
|
bless $tx, 'Fake::App'; |
61 |
|
|
62 |
|
if ( $self->static->dispatch($tx) ) { |
63 |
|
# warn "# static ",dump( $tx ); |
64 |
|
return $tx; |
65 |
|
} |
66 |
|
|
67 |
|
my $body; |
68 |
|
|
69 |
|
my $server = Frey::Server->new; |
70 |
|
$server->{_print} = sub { |
71 |
|
$body .= join("\n", @_); |
72 |
|
}; |
73 |
|
|
74 |
|
my $url = $tx->req->url->to_string; |
75 |
|
my $params = $tx->req->params->to_hash; |
76 |
|
|
77 |
|
warn "# url $url params ",dump($params); |
78 |
|
|
79 |
|
my $content_type = $server->request( $url, $params ); # fetch body |
80 |
|
|
81 |
|
=for developer |
82 |
|
|
83 |
|
# compatiblity with unpatched Mojo |
84 |
|
sub class2rest { |
85 |
|
my $c = shift; |
86 |
|
$c =~ s/::/-/gs; |
87 |
|
$c; |
88 |
|
} |
89 |
|
$body =~ s{(/\w+::\w+[\w:]+)}{class2rest($1)}sge; |
90 |
|
|
91 |
|
=cut |
92 |
|
|
93 |
|
$tx->res->code(200); |
94 |
|
$tx->res->headers->content_type( $content_type ); |
95 |
|
$tx->res->body( $body ); |
96 |
|
|
97 |
|
warn dump( $tx->res->headers ); |
98 |
|
return $tx; |
99 |
} |
} |
100 |
|
|
101 |
1; |
1; |