1 |
package Frey::Mojo; |
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
|
6 |
use base 'Mojo'; |
7 |
|
8 |
use MojoX::Dispatcher::Static; |
9 |
|
10 |
use Frey::Server; |
11 |
|
12 |
use Data::Dump qw/dump/; |
13 |
|
14 |
__PACKAGE__->attr( |
15 |
static => ( |
16 |
chained => 1, |
17 |
default => sub { MojoX::Dispatcher::Static->new } |
18 |
) |
19 |
); |
20 |
|
21 |
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 |
sub handler { |
36 |
my ($self, $tx) = @_; |
37 |
|
38 |
if ( $self->static->dispatch($tx) ) { |
39 |
# warn "# static ",dump( $tx ); |
40 |
return $tx; |
41 |
} |
42 |
|
43 |
my $body; |
44 |
|
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; |