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 |
|
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; |
52 |
|
|
53 |
|
warn "# url $url params ",dump($params); |
54 |
|
|
55 |
# The routes |
$server->request( $url, $params ); # fetch body |
|
my $r = $self->routes; |
|
56 |
|
|
57 |
# $r->route('/:action/:class/:controller') |
=for developer |
|
# ->to(controller => 'run', action => 'as_markup', class => 'Frey::ClassBrowser' ); |
|
58 |
|
|
59 |
# $r->route('/') |
# compatiblity with unpatched Mojo |
60 |
# ->to(controller => 'run', action => 'as_markup', class => 'Frey::ClassBrowser'); |
sub class2rest { |
61 |
|
my $c = shift; |
62 |
|
$c =~ s/::/-/gs; |
63 |
|
$c; |
64 |
|
} |
65 |
|
$body =~ s{(/\w+::\w+[\w:]+)}{class2rest($1)}sge; |
66 |
|
|
67 |
$r->route('/:class') |
=cut |
|
->to(controller => 'run', action => 'as_markup', class => 'Frey::Introspect' ); |
|
68 |
|
|
69 |
# Default route |
$tx->res->code(200); |
70 |
$r->route('/:class/:action/:controller') |
$tx->res->headers->content_type('text/html'); |
71 |
->to(controller => 'run', action => 'as_markup', class => 'Frey::ClassBrowser'); |
$tx->res->body( $body ); |
72 |
|
|
73 |
|
warn dump( $tx->res->headers ); |
74 |
|
return $tx; |
75 |
} |
} |
76 |
|
|
77 |
1; |
1; |