our
$AUTHORITY
=
'cpan:SUKRIA'
;
$Dancer::Handler::PSGI::VERSION
=
'1.3402'
;
sub
new {
my
$class
=
shift
;
raise
core_handler_PSGI
=>
"Plack::Request is needed by the PSGI handler"
unless
Dancer::ModuleLoader->load(
'Plack::Request'
);
my
$self
= {};
bless
$self
,
$class
;
}
sub
start {
my
$self
=
shift
;
my
$app
=
$self
->psgi_app();
foreach
my
$setting
(
qw/plack_middlewares plack_middlewares_map/
) {
if
(Dancer::Config::setting(
$setting
)) {
my
$method
=
'apply_'
.
$setting
;
$app
=
$self
->
$method
(
$app
);
}
}
return
$app
;
}
sub
apply_plack_middlewares_map {
my
(
$self
,
$app
) =
@_
;
my
$mw_map
= Dancer::Config::setting(
'plack_middlewares_map'
);
foreach
my
$req
(
qw(Plack::App::URLMap Plack::Builder)
) {
raise
core_handler_PSGI
=>
"$req is needed to use apply_plack_middlewares_map"
unless
Dancer::ModuleLoader->load(
$req
);
}
my
$urlmap
= Plack::App::URLMap->new;
while
(
my
(
$path
,
$mw
) =
each
%$mw_map
) {
my
$builder
= Plack::Builder->new();
$builder
->add_middleware(
@$_
)
for
@$mw
;
$urlmap
->
map
(
$path
=>
$builder
->to_app(
$app
) );
}
$urlmap
->
map
(
'/'
=>
$app
)
unless
$mw_map
->{
'/'
};
return
$urlmap
->to_app;
}
sub
apply_plack_middlewares {
my
(
$self
,
$app
) =
@_
;
my
$middlewares
= Dancer::Config::setting(
'plack_middlewares'
);
raise
core_handler_PSGI
=>
"Plack::Builder is needed for middlewares support"
unless
Dancer::ModuleLoader->load(
'Plack::Builder'
);
ref
$middlewares
eq
"ARRAY"
or raise
core_handler_PSGI
=>
"'plack_middlewares' setting must be an ArrayRef"
;
my
$builder
= Plack::Builder->new();
for
my
$mw
(
@$middlewares
) {
Dancer::Logger::core
"add middleware "
.
$mw
->[0];
$builder
->add_middleware(
@$mw
)
}
return
$builder
->to_app(
$app
);
}
sub
init_request_headers {
my
(
$self
,
$env
) =
@_
;
my
$plack
= Plack::Request->new(
$env
);
Dancer::SharedData->headers(
$plack
->headers);
}
1;