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 |
sub handler { |
37 |
sub startup { |
my ($self, $tx) = @_; |
38 |
my $self = shift; |
|
39 |
|
if ( $self->static->dispatch($tx) ) { |
40 |
|
# warn "# static ",dump( $tx ); |
41 |
|
return $tx; |
42 |
|
} |
43 |
|
|
44 |
|
my $body; |
45 |
|
|
46 |
|
my $server = Frey::Server->new; |
47 |
|
$server->{_print} = sub { |
48 |
|
$body .= join("\n", @_); |
49 |
|
}; |
50 |
|
|
51 |
|
my $url = $tx->req->url->to_string; |
52 |
|
my $params = $tx->req->params->to_hash; |
53 |
|
|
54 |
|
warn "# url $url params ",dump($params); |
55 |
|
|
56 |
# The routes |
my $content_type = $server->request( $url, $params ); # fetch body |
|
my $r = $self->routes; |
|
57 |
|
|
58 |
# $r->route('/:action/:class/:controller') |
=for developer |
|
# ->to(controller => 'run', action => 'as_markup', class => 'Frey::ClassBrowser' ); |
|
59 |
|
|
60 |
# $r->route('/') |
# compatiblity with unpatched Mojo |
61 |
# ->to(controller => 'run', action => 'as_markup', class => 'Frey::ClassBrowser'); |
sub class2rest { |
62 |
|
my $c = shift; |
63 |
|
$c =~ s/::/-/gs; |
64 |
|
$c; |
65 |
|
} |
66 |
|
$body =~ s{(/\w+::\w+[\w:]+)}{class2rest($1)}sge; |
67 |
|
|
68 |
$r->route('/:class') |
=cut |
|
->to(controller => 'run', action => 'as_markup', class => 'Frey::Introspect' ); |
|
69 |
|
|
70 |
# Default route |
$tx->res->code(200); |
71 |
$r->route('/:class/:action/:controller') |
$tx->res->headers->content_type( $content_type ); |
72 |
->to(controller => 'run', action => 'as_markup', class => 'Frey::ClassBrowser'); |
$tx->res->body( $body ); |
73 |
|
|
74 |
|
warn dump( $tx->res->headers ); |
75 |
|
return $tx; |
76 |
} |
} |
77 |
|
|
78 |
1; |
1; |