| 1 |
package Dancer2::Core::Runner; |
| 2 |
# ABSTRACT: Top-layer class to start a dancer app |
| 3 |
$Dancer2::Core::Runner::VERSION = '2.2.2'; |
| 4 |
use Moo; |
| 5 |
use Carp 'croak'; |
| 6 |
use Module::Runtime 'require_module'; |
| 7 |
use Dancer2::Core::MIME; |
| 8 |
use Dancer2::Core::Types; |
| 9 |
use Dancer2::Core::Dispatcher; |
| 10 |
use Plack::Builder qw(); |
| 11 |
use Ref::Util qw< is_ref is_regexpref >; |
| 12 |
|
| 13 |
# Hashref of configurable items for the runner. |
| 14 |
# Defaults come from ENV vars. Updated via global triggers |
| 15 |
# from app configs. |
| 16 |
has config => ( |
| 17 |
is => 'ro', |
| 18 |
isa => HashRef, |
| 19 |
lazy => 1, |
| 20 |
builder => '_build_config', |
| 21 |
); |
| 22 |
|
| 23 |
has mime_type => ( |
| 24 |
is => 'ro', |
| 25 |
isa => InstanceOf ['Dancer2::Core::MIME'], |
| 26 |
default => sub { Dancer2::Core::MIME->new(); }, |
| 27 |
); |
| 28 |
|
| 29 |
has server => ( |
| 30 |
is => 'ro', |
| 31 |
isa => InstanceOf['HTTP::Server::PSGI'], |
| 32 |
lazy => 1, |
| 33 |
builder => '_build_server', |
| 34 |
handles => ['run'], |
| 35 |
); |
| 36 |
|
| 37 |
has apps => ( |
| 38 |
is => 'ro', |
| 39 |
isa => ArrayRef, |
| 40 |
default => sub { [] }, |
| 41 |
); |
| 42 |
|
| 43 |
has postponed_hooks => ( |
| 44 |
is => 'ro', |
| 45 |
isa => HashRef, |
| 46 |
default => sub { +{} }, |
| 47 |
); |
| 48 |
|
| 49 |
has environment => ( |
| 50 |
is => 'ro', |
| 51 |
isa => Str, |
| 52 |
required => 1, |
| 53 |
default => sub { |
| 54 |
$ENV{DANCER_ENVIRONMENT} || $ENV{PLACK_ENV} || 'development' |
| 55 |
}, |
| 56 |
); |
| 57 |
|
| 58 |
has host => ( |
| 59 |
is => 'ro', |
| 60 |
lazy => 1, |
| 61 |
default => sub { $_[0]->config->{'host'} }, |
| 62 |
); |
| 63 |
|
| 64 |
has port => ( |
| 65 |
is => 'ro', |
| 66 |
lazy => 1, |
| 67 |
default => sub { $_[0]->config->{'port'} }, |
| 68 |
); |
| 69 |
|
| 70 |
has timeout => ( |
| 71 |
is => 'ro', |
| 72 |
lazy => 1, |
| 73 |
default => sub { $_[0]->config->{'timeout'} }, |
| 74 |
); |
| 75 |
|
| 76 |
sub _build_server { |
| 77 |
my $self = shift; |
| 78 |
|
| 79 |
require_module('HTTP::Server::PSGI'); |
| 80 |
my %args = ( |
| 81 |
host => $self->host, |
| 82 |
port => $self->port, |
| 83 |
timeout => $self->timeout, |
| 84 |
); |
| 85 |
|
| 86 |
if ( !$self->config->{'no_server_tokens'} ) { |
| 87 |
$args{'server_software'} = "Perl Dancer2 " . Dancer2->VERSION; |
| 88 |
} |
| 89 |
|
| 90 |
return HTTP::Server::PSGI->new(%args); |
| 91 |
} |
| 92 |
|
| 93 |
sub _build_config { |
| 94 |
my $self = shift; |
| 95 |
|
| 96 |
$ENV{PLACK_ENV} |
| 97 |
and $ENV{DANCER_APPHANDLER} = 'PSGI'; |
| 98 |
|
| 99 |
return { |
| 100 |
behind_proxy => 0, |
| 101 |
apphandler => ( $ENV{DANCER_APPHANDLER} || 'Standalone' ), |
| 102 |
traces => ( $ENV{DANCER_TRACES} || 0 ), |
| 103 |
host => ( $ENV{DANCER_SERVER} || '0.0.0.0' ), |
| 104 |
port => ( $ENV{DANCER_PORT} || '3000' ), |
| 105 |
no_server_tokens => ( defined $ENV{DANCER_NO_SERVER_TOKENS} ? |
| 106 |
$ENV{DANCER_NO_SERVER_TOKENS} : |
| 107 |
0 ), |
| 108 |
startup_info => ( defined $ENV{DANCER_STARTUP_INFO} ? |
| 109 |
$ENV{DANCER_STARTUP_INFO} : |
| 110 |
1 ), |
| 111 |
}; |
| 112 |
} |
| 113 |
|
| 114 |
sub BUILD { |
| 115 |
my $self = shift; |
| 116 |
|
| 117 |
# Enable traces if set by ENV var. |
| 118 |
if (my $traces = $self->config->{traces} ) { |
| 119 |
require_module('Carp'); |
| 120 |
$Carp::Verbose = $traces ? 1 : 0; |
| 121 |
}; |
| 122 |
|
| 123 |
# set the global runner object if one doesn't exist yet |
| 124 |
# this can happen if you create one without going through Dancer2 |
| 125 |
# which doesn't trigger the import that creates it |
| 126 |
defined $Dancer2::runner |
| 127 |
or $Dancer2::runner = $self; |
| 128 |
} |
| 129 |
|
| 130 |
sub register_application { |
| 131 |
my $self = shift; |
| 132 |
my $app = shift; |
| 133 |
|
| 134 |
push @{ $self->apps }, $app; |
| 135 |
|
| 136 |
# add postponed hooks to our psgi app |
| 137 |
$self->add_postponed_hooks( $app->name, $app->postponed_hooks ); |
| 138 |
} |
| 139 |
|
| 140 |
sub add_postponed_hooks { |
| 141 |
my $self = shift; |
| 142 |
my $name = shift; |
| 143 |
my $hooks = shift; |
| 144 |
|
| 145 |
# merge postponed hooks |
| 146 |
@{ $self->{'postponed_hooks'}{$name} }{ keys %{$hooks} } = values %{$hooks}; |
| 147 |
} |
| 148 |
|
| 149 |
# decide what to start |
| 150 |
# do we just return a PSGI app |
| 151 |
# or do we actually start a development standalone server? |
| 152 |
sub start { |
| 153 |
my $self = shift; |
| 154 |
my $app = $self->psgi_app; |
| 155 |
|
| 156 |
# we decide whether we return a PSGI coderef |
| 157 |
# or spin a local development PSGI server |
| 158 |
$self->config->{'apphandler'} eq 'PSGI' |
| 159 |
and return $app; |
| 160 |
|
| 161 |
$self->start_server($app); |
| 162 |
} |
| 163 |
|
| 164 |
sub start_server { |
| 165 |
my $self = shift; |
| 166 |
my $app = shift; |
| 167 |
|
| 168 |
# does not return |
| 169 |
$self->print_banner; |
| 170 |
$self->server->run($app); |
| 171 |
} |
| 172 |
|
| 173 |
sub psgi_app { |
| 174 |
my ($self, $apps) = @_; |
| 175 |
|
| 176 |
my @found_apps; |
| 177 |
foreach my $app_req ( @{ $apps || [] } ) { |
| 178 |
if ( is_regexpref($app_req) ) { |
| 179 |
# Asked to find app via regex pattern on its name |
| 180 |
push @found_apps, |
| 181 |
grep +( $_->name =~ $app_req ), @{ $self->apps }; |
| 182 |
} elsif ( ref $app_req eq 'Dancer2::Core::App' ) { |
| 183 |
# Given Dancer2 App instance |
| 184 |
push @found_apps, $app_req; |
| 185 |
} elsif ( !is_ref($app_req) ) { |
| 186 |
# Strings of the app names |
| 187 |
push @found_apps, |
| 188 |
grep +( $_->name eq $app_req ), @{ $self->apps }; |
| 189 |
} else { |
| 190 |
croak "Invalid input to psgi_app: $app_req"; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
# if specific apps, dispatch to them |
| 195 |
# otherwise, dispatch over all apps by default |
| 196 |
$apps = @found_apps ? \@found_apps : $self->apps; |
| 197 |
|
| 198 |
my $dispatcher = Dancer2::Core::Dispatcher->new( apps => $apps ); |
| 199 |
|
| 200 |
# initialize psgi_apps |
| 201 |
# (calls ->finish on the apps and create their PSGI apps) |
| 202 |
# the dispatcher caches that in the attribute |
| 203 |
# so ->finish isn't actually called again if you run this method |
| 204 |
$dispatcher->apps_psgi; |
| 205 |
|
| 206 |
return sub { |
| 207 |
my $env = shift; |
| 208 |
|
| 209 |
# mark it as an old-style dispatching |
| 210 |
$self->{'internal_dispatch'} = 1; |
| 211 |
|
| 212 |
my $response = $dispatcher->dispatch($env); |
| 213 |
|
| 214 |
# unmark it |
| 215 |
delete $self->{'internal_dispatch'}; |
| 216 |
|
| 217 |
# cleanup |
| 218 |
delete $self->{'internal_sessions'}; |
| 219 |
|
| 220 |
return $response; |
| 221 |
}; |
| 222 |
} |
| 223 |
|
| 224 |
sub print_banner { |
| 225 |
my $self = shift; |
| 226 |
my $pid = $$; |
| 227 |
|
| 228 |
# we only print the info if we need to |
| 229 |
$self->config->{'startup_info'} or return; |
| 230 |
|
| 231 |
# bare minimum |
| 232 |
print STDERR ">> Dancer2 v" . Dancer2->VERSION . " server $pid listening " |
| 233 |
. 'on http://' |
| 234 |
. $self->host . ':' |
| 235 |
. $self->port . "\n"; |
| 236 |
|
| 237 |
# all loaded plugins |
| 238 |
foreach my $module ( grep { $_ =~ m{^Dancer2/Plugin/} } keys %INC ) { |
| 239 |
$module =~ s{/}{::}g; # change / to :: |
| 240 |
$module =~ s{\.pm$}{}; # remove .pm at the end |
| 241 |
my $version = $module->VERSION; |
| 242 |
|
| 243 |
defined $version or $version = 'no version number defined'; |
| 244 |
print STDERR ">> $module ($version)\n"; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
1; |
| 249 |
|
| 250 |
__END__ |
| 251 |
|
| 252 |
=pod |
| 253 |
|
| 254 |
=encoding UTF-8 |
| 255 |
|
| 256 |
=head1 NAME |
| 257 |
|
| 258 |
Dancer2::Core::Runner - Top-layer class to start a dancer app |
| 259 |
|
| 260 |
=head1 VERSION |
| 261 |
|
| 262 |
version 2.2.2 |
| 263 |
|
| 264 |
=head1 AUTHOR |
| 265 |
|
| 266 |
Dancer Core Developers |
| 267 |
|
| 268 |
=head1 COPYRIGHT AND LICENSE |
| 269 |
|
| 270 |
This software is copyright (c) 2026 by Alexis Sukrieh. |
| 271 |
|
| 272 |
This is free software; you can redistribute it and/or modify it under |
| 273 |
the same terms as the Perl 5 programming language system itself. |
| 274 |
|
| 275 |
=cut |