$Dancer2::Core::Runner::VERSION
=
'1.1.2'
;
has
config
=> (
is
=>
'ro'
,
isa
=> HashRef,
lazy
=> 1,
builder
=>
'_build_config'
,
);
has
mime_type
=> (
is
=>
'ro'
,
isa
=> InstanceOf [
'Dancer2::Core::MIME'
],
default
=>
sub
{ Dancer2::Core::MIME->new(); },
);
has
server
=> (
is
=>
'ro'
,
isa
=> InstanceOf[
'HTTP::Server::PSGI'
],
lazy
=> 1,
builder
=>
'_build_server'
,
handles
=> [
'run'
],
);
has
apps
=> (
is
=>
'ro'
,
isa
=> ArrayRef,
default
=>
sub
{ [] },
);
has
postponed_hooks
=> (
is
=>
'ro'
,
isa
=> HashRef,
default
=>
sub
{ +{} },
);
has
environment
=> (
is
=>
'ro'
,
isa
=> Str,
required
=> 1,
default
=>
sub
{
$ENV
{DANCER_ENVIRONMENT} ||
$ENV
{PLACK_ENV} ||
'development'
},
);
has
host
=> (
is
=>
'ro'
,
lazy
=> 1,
default
=>
sub
{
$_
[0]->config->{
'host'
} },
);
has
port
=> (
is
=>
'ro'
,
lazy
=> 1,
default
=>
sub
{
$_
[0]->config->{
'port'
} },
);
has
timeout
=> (
is
=>
'ro'
,
lazy
=> 1,
default
=>
sub
{
$_
[0]->config->{
'timeout'
} },
);
sub
_build_server {
my
$self
=
shift
;
require_module(
'HTTP::Server::PSGI'
);
HTTP::Server::PSGI->new(
host
=>
$self
->host,
port
=>
$self
->port,
timeout
=>
$self
->timeout,
server_software
=>
"Perl Dancer2 "
. Dancer2->VERSION,
);
}
sub
_build_config {
my
$self
=
shift
;
$ENV
{PLACK_ENV}
and
$ENV
{DANCER_APPHANDLER} =
'PSGI'
;
return
{
behind_proxy
=> 0,
apphandler
=> (
$ENV
{DANCER_APPHANDLER} ||
'Standalone'
),
traces
=> (
$ENV
{DANCER_TRACES} || 0 ),
host
=> (
$ENV
{DANCER_SERVER} ||
'0.0.0.0'
),
port
=> (
$ENV
{DANCER_PORT} ||
'3000'
),
no_server_tokens
=> (
defined
$ENV
{DANCER_NO_SERVER_TOKENS} ?
$ENV
{DANCER_NO_SERVER_TOKENS} :
0 ),
startup_info
=> (
defined
$ENV
{DANCER_STARTUP_INFO} ?
$ENV
{DANCER_STARTUP_INFO} :
1 ),
};
}
sub
BUILD {
my
$self
=
shift
;
if
(
my
$traces
=
$self
->config->{traces} ) {
require_module(
'Carp'
);
$Carp::Verbose
=
$traces
? 1 : 0;
};
defined
$Dancer2::runner
or
$Dancer2::runner
=
$self
;
}
sub
register_application {
my
$self
=
shift
;
my
$app
=
shift
;
push
@{
$self
->apps },
$app
;
$self
->add_postponed_hooks(
$app
->name,
$app
->postponed_hooks );
}
sub
add_postponed_hooks {
my
$self
=
shift
;
my
$name
=
shift
;
my
$hooks
=
shift
;
@{
$self
->{
'postponed_hooks'
}{
$name
} }{
keys
%{
$hooks
} } =
values
%{
$hooks
};
}
sub
start {
my
$self
=
shift
;
my
$app
=
$self
->psgi_app;
$self
->config->{
'apphandler'
} eq
'PSGI'
and
return
$app
;
$self
->start_server(
$app
);
}
sub
start_server {
my
$self
=
shift
;
my
$app
=
shift
;
$self
->print_banner;
$self
->server->run(
$app
);
}
sub
psgi_app {
my
(
$self
,
$apps
) =
@_
;
if
(
$apps
&& @{
$apps
} ) {
my
@found_apps
= ();
foreach
my
$app_req
( @{
$apps
} ) {
if
( is_regexpref(
$app_req
) ) {
push
@found_apps
,
grep
+(
$_
->name =~
$app_req
), @{
$self
->apps };
}
elsif
(
ref
$app_req
eq
'Dancer2::Core::App'
) {
push
@found_apps
,
$app_req
;
}
elsif
( !is_ref(
$app_req
) ) {
push
@found_apps
,
grep
+(
$_
->name eq
$app_req
), @{
$self
->apps };
}
else
{
croak
"Invalid input to psgi_app: $app_req"
;
}
}
$apps
= \
@found_apps
;
}
else
{
$apps
=
$self
->apps;
}
my
$dispatcher
= Dancer2::Core::Dispatcher->new(
apps
=>
$apps
);
$dispatcher
->apps_psgi;
return
sub
{
my
$env
=
shift
;
$self
->{
'internal_dispatch'
} = 1;
my
$response
=
$dispatcher
->dispatch(
$env
);
delete
$self
->{
'internal_dispatch'
};
delete
$self
->{
'internal_sessions'
};
return
$response
;
};
}
sub
print_banner {
my
$self
=
shift
;
my
$pid
= $$;
$self
->config->{
'startup_info'
} or
return
;
print
STDERR
">> Dancer2 v"
. Dancer2->VERSION .
" server $pid listening "
.
$self
->host .
':'
.
$self
->port .
"\n"
;
foreach
my
$module
(
grep
{
$_
=~ m{^Dancer2/Plugin/} }
keys
%INC
) {
$module
=~ s{/}{::}g;
$module
=~ s{\.pm$}{};
my
$version
=
$module
->VERSION;
defined
$version
or
$version
=
'no version number defined'
;
print
STDERR
">> $module ($version)\n"
;
}
}
1;