summaryrefslogtreecommitdiff
path: root/lib/Dancer2/Core/Runner.pm
blob: e587b088164995937acd4065e75119cf3f09c0e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package Dancer2::Core::Runner;
# ABSTRACT: Top-layer class to start a dancer app
$Dancer2::Core::Runner::VERSION = '1.0.0';
use Moo;
use Carp 'croak';
use Module::Runtime 'require_module';
use Dancer2::Core::MIME;
use Dancer2::Core::Types;
use Dancer2::Core::Dispatcher;
use Plack::Builder qw();
use Ref::Util qw< is_ref is_regexpref >;

# Hashref of configurable items for the runner.
# Defaults come from ENV vars. Updated via global triggers
# from app configs.
has config => (
    is      => 'ro',
    isa     => HashRef,
    lazy    => 1,
    builder => '_build_config',
);

# FIXME: i hate this
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;

    # Enable traces if set by ENV var.
    if (my $traces = $self->config->{traces} ) {
        require_module('Carp');
        $Carp::Verbose = $traces ? 1 : 0;
    };

    # set the global runner object if one doesn't exist yet
    # this can happen if you create one without going through Dancer2
    # which doesn't trigger the import that creates it
    defined $Dancer2::runner
        or $Dancer2::runner = $self;
}

sub register_application {
    my $self = shift;
    my $app  = shift;

    push @{ $self->apps }, $app;

    # add postponed hooks to our psgi app
    $self->add_postponed_hooks( $app->name, $app->postponed_hooks );
}

sub add_postponed_hooks {
    my $self  = shift;
    my $name  = shift;
    my $hooks = shift;

    # merge postponed hooks
    @{ $self->{'postponed_hooks'}{$name} }{ keys %{$hooks} } = values %{$hooks};
}

# decide what to start
# do we just return a PSGI app
# or do we actually start a development standalone server?
sub start {
    my $self = shift;
    my $app  = $self->psgi_app;

    # we decide whether we return a PSGI coderef
    # or spin a local development PSGI server
    $self->config->{'apphandler'} eq 'PSGI'
        and return $app;

    # FIXME: this should not include the server tokens
    # since those are already added to the server itself
    $self->start_server($app);
}

sub start_server {
    my $self = shift;
    my $app  = shift;

    # does not return
    $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) ) {
                # find it in the apps registry
                push @found_apps,
                    grep +( $_->name =~ $app_req ), @{ $self->apps };
            } elsif ( ref $app_req eq 'Dancer2::Core::App' ) {
                # use it directly
                push @found_apps, $app_req;
            } elsif ( !is_ref($app_req) ) {
                # find it in the apps registry
                push @found_apps,
                    grep +( $_->name eq $app_req ), @{ $self->apps };
            } else {
                croak "Invalid input to psgi_app: $app_req";
            }
        }

        $apps = \@found_apps;
    } else {
        # dispatch over all apps by default
        $apps = $self->apps;
    }

    my $dispatcher = Dancer2::Core::Dispatcher->new( apps => $apps );

    # initialize psgi_apps
    # (calls ->finish on the apps and create their PSGI apps)
    # the dispatcher caches that in the attribute
    # so ->finish isn't actually called again if you run this method
    $dispatcher->apps_psgi;

    return sub {
        my $env = shift;

        # mark it as an old-style dispatching
        $self->{'internal_dispatch'} = 1;

        my $response = $dispatcher->dispatch($env);

        # unmark it
        delete $self->{'internal_dispatch'};

        # cleanup
        delete $self->{'internal_sessions'};

        return $response;
    };
}

sub print_banner {
    my $self = shift;
    my $pid  = $$;

    # we only print the info if we need to
    $self->config->{'startup_info'} or return;

    # bare minimum
    print STDERR ">> Dancer2 v" . Dancer2->VERSION . " server $pid listening "
      . 'on http://'
      . $self->host . ':'
      . $self->port . "\n";

    # all loaded plugins
    foreach my $module ( grep { $_ =~ m{^Dancer2/Plugin/} } keys %INC ) {
        $module =~ s{/}{::}g;     # change / to ::
        $module =~ s{\.pm$}{};    # remove .pm at the end
        my $version = $module->VERSION;

        defined $version or $version = 'no version number defined';
        print STDERR ">> $module ($version)\n";
    }
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Dancer2::Core::Runner - Top-layer class to start a dancer app

=head1 VERSION

version 1.0.0

=head1 AUTHOR

Dancer Core Developers

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2023 by Alexis Sukrieh.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut