/
Config.pm
393 lines (285 loc) · 10.2 KB
/
Config.pm
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package Dancer::Config;
use strict;
use warnings;
use base 'Exporter';
use vars '@EXPORT_OK';
use Dancer::Template;
use Dancer::ModuleLoader;
use Dancer::FileUtils 'path';
use Carp;
use Encode;
@EXPORT_OK = qw(setting mime_types);
my $SETTINGS = {};
# mergeable settings
my %MERGEABLE = map { ($_ => 1) } qw( plugins handlers );
sub settings {$SETTINGS}
my $setters = {
logger => sub {
my ($setting, $value) = @_;
Dancer::Logger->init($value, settings());
},
session => sub {
my ($setting, $value) = @_;
Dancer::Session->init($value, settings());
},
template => sub {
my ($setting, $value) = @_;
Dancer::Template->init($value, settings());
},
route_cache => sub {
my ($setting, $value) = @_;
require Dancer::Route::Cache;
Dancer::Route::Cache->reset();
},
serializer => sub {
my ($setting, $value) = @_;
require Dancer::Serializer;
Dancer::Serializer->init($value);
},
import_warnings => sub {
my ($setting, $value) = @_;
$^W = $value ? 1 : 0;
},
auto_page => sub {
my ($setting, $auto_page) = @_;
if ($auto_page) {
require Dancer::App;
Dancer::App->current->registry->universal_add(
'get', '/:page',
sub {
my $params = Dancer::SharedData->request->params;
Dancer::Helpers::template($params->{'page'});
}
);
}
},
traces => sub {
my ($setting, $traces) = @_;
$Carp::Verbose = $traces ? 1 : 0;
},
};
my $normalizers = {
charset => sub {
my ($setting, $charset) = @_;
length($charset || '')
or return $charset;
my $encoding = Encode::find_encoding($charset);
defined $encoding
or croak "Charset defined in configuration is wrong : couldn't identify '$charset'";
my $name = $encoding->name;
# Perl makes a distinction between the usual perl utf8, and the strict
# utf8 charset. But we don't want to make this distinction
$name eq 'utf-8-strict'
and $name = 'utf-8';
return $name;
},
};
sub mime_types {
carp "DEPRECATED: use 'mime_type' from Dancer.pm";
my $mime = Dancer::MIME->instance();
if (scalar(@_)==2) { $mime->add_mime_type(@_) }
elsif (scalar(@_)==1) { $mime->mime_type_for(@_) }
else { $mime->aliases }
}
sub normalize_setting {
my ($class, $setting, $value) = @_;
$value = $normalizers->{$setting}->($setting, $value)
if exists $normalizers->{$setting};
return $value;
}
# public accessor for get/set
sub setting {
my ($setting, $value) = @_;
if (@_ == 2) {
$value = _set_setting($setting, $value);
_trigger_hooks($setting, $value);
return $value;
}
else {
return _get_setting($setting);
}
}
sub _trigger_hooks {
my ($setting, $value) = @_;
$setters->{$setting}->(@_) if defined $setters->{$setting};
}
sub _set_setting {
my ($setting, $value) = @_;
return unless @_ == 2;
# normalize the value if needed
$value = Dancer::Config->normalize_setting($setting, $value);
$SETTINGS->{$setting} = $value;
return $value;
}
sub _get_setting {
my $setting = shift;
return $SETTINGS->{$setting};
}
sub conffile { path(setting('confdir') || setting('appdir'), 'config.yml') }
sub environment_file {
my $env = setting('environment');
return path(setting('appdir'), 'environments', "$env.yml");
}
sub init_confdir {
return setting('confdir') if setting('confdir');
setting confdir => $ENV{DANCER_CONFDIR} || setting('appdir');
}
sub load {
init_confdir();
# look for the conffile
return 1 unless -f conffile;
# load YAML
confess "Configuration file found but YAML is not installed"
unless Dancer::ModuleLoader->load('YAML');
load_settings_from_yaml(conffile);
my $env = environment_file;
load_settings_from_yaml($env) if -f $env;
foreach my $key (grep { $setters->{$_} } keys %$SETTINGS) {
$setters->{$key}->($key, $SETTINGS->{$key});
}
return 1;
}
sub load_settings_from_yaml {
my ($file) = @_;
my $config;
eval { $config = YAML::LoadFile($file) };
if (my $err = $@ || (!$config)) {
confess "Unable to parse the configuration file: $file: $@";
}
for my $key (keys %{$config}) {
if ($MERGEABLE{$key}) {
my $setting = setting($key);
$setting->{$_} = $config->{$key}{$_} for keys %{$config->{$key}};
}
else {
_set_setting($key, $config->{$key});
}
}
return scalar(keys %$config);
}
sub load_default_settings {
$SETTINGS->{server} ||= $ENV{DANCER_SERVER} || '0.0.0.0';
$SETTINGS->{port} ||= $ENV{DANCER_PORT} || '3000';
$SETTINGS->{content_type} ||= $ENV{DANCER_CONTENT_TYPE} || 'text/html';
$SETTINGS->{charset} ||= $ENV{DANCER_CHARSET} || '';
$SETTINGS->{access_log} ||= $ENV{DANCER_ACCESS_LOG} || 1;
$SETTINGS->{daemon} ||= $ENV{DANCER_DAEMON} || 0;
$SETTINGS->{apphandler} ||= $ENV{DANCER_APPHANDLER} || 'Standalone';
$SETTINGS->{warnings} ||= $ENV{DANCER_WARNINGS} || 0;
$SETTINGS->{auto_reload} ||= $ENV{DANCER_AUTO_RELOAD} || 0;
$SETTINGS->{traces} ||= $ENV{DANCER_TRACES} || 0;
$SETTINGS->{environment}
||= $ENV{DANCER_ENVIRONMENT}
|| $ENV{PLACK_ENV}
|| 'development';
setting $_ => {} for keys %MERGEABLE;
setting template => 'simple';
setting import_warnings => 1;
}
load_default_settings();
1;
__END__
=pod
=head1 NAME
Dancer::Config - setting registry for Dancer
=head1 DESCRIPTION
Setting registry for Dancer
=head1 SETTINGS
You can change a setting with the keyword B<set>, like the following:
use Dancer;
# changing default settings
set port => 8080;
set content_type => 'text/plain';
set access_log => 0;
A better way of defining settings exists: using YAML file. For this to be
possible, you have to install the L<YAML> module. If a file named B<config.yml>
exists in the application directory, it will be loaded, as a setting group.
The same is done for the environment file located in the B<environments>
directory.
=head1 SUPPORTED SETTINGS
=head2 server (string)
The IP address that the Dancer app should bind to. Default is 0.0.0.0, i.e.
bind to all available interfaces.
=head2 port (int)
The port Dancer will listen to.
Default value is 3000. This setting can be changed on the command-line with the
B<--port> switch.
=head2 daemon (boolean)
If set to true, runs the standalone webserver in the background.
This setting can be changed on the command-line with the B<--daemon> flag.
=head2 content_type (string)
The default content type of outgoing content.
Default value is 'text/html'.
=head2 charset (string)
This setting does 3 things:
=over
=item *
It sets the default charset of outgoing content. C<charset=> item will be
added to Content-Type response header.
=item *
It makes Unicode bodies in HTTP responses of C<text/*> types to be encoded to
this charset.
=item *
It also indicates to Dancer in which charset the static files and templates are
encoded.
=back
Default value is empty which means don't do anything. HTTP responses
without charset will be interpreted as ISO-8859-1 by most clients.
You can cancel any charset processing by specifying your own charset
in Content-Type header or by ensuring that response body leaves your
handler without Unicode flag set (by encoding it into some 8bit
charset, for example).
Also, since automatically serialized JSON responses have
C<application/json> Content-Type, you should always encode them by
hand.
=head2 public (string)
This is the path of the public directory, where static files are stored. Any
existing file in that directory will be served as a static file, before
mathcing any route.
By default, it points to APPDIR/public where APPDIR is the directory that
contains your Dancer script.
=head2 layout (string)
name of the layout to use when rendering view. Dancer will look for
a matching template in the directory $appdir/views/layout.
=head2 warnings (boolean)
If set to true, tells Dancer to consider all warnings as blocking errors.
=head2 traces (boolean)
If set to true, Dancer will display full stack traces when a warning or a die
occurs. (Internally sets Carp::Verbose). Default to false.
=head2 log (enum)
Tells which log messages should be actullay logged. Possible values are
B<debug>, B<warning> or B<error>.
=over 4
=item B<debug> : all messages are logged
=item B<warning> : only warning and error messages are logged
=item B<error> : only error messages are logged
=back
=head2 show_errors (boolean)
If set to true, Dancer will render a detailed debug screen whenever an error is
catched. If set to false, Dancer will render the default error page, using
$public/$error_code.html if it exists.
=head2 auto_reload (boolean)
Requires L<Module::Refresh> and L<Clone>.
If set to true, Dancer will reload the route handlers whenever the file where
they are defined is changed. This is very useful in development environment but
B<should not be enabled in production>. Enabling this flag in production yields
a major negative effect on performance because of L<Module::Refresh>.
When this flag is set, you don't have to restart your webserver whenever you
make a change in a route handler.
Note that L<Module::Refresh> only operates on files in C<%INC>, so if the script
your Dancer app is started from changes, even with auto_reload enabled, you will
still not see the changes reflected until you start your app.
=head2 session (enum)
This setting lets you enable a session engine for your web application. Be
default, sessions are disabled in Dancer, you must choose a session engine to
use them.
See L<Dancer::Session> for supported engines and their respective configuration.
=head1 AUTHOR
This module has been written by Alexis Sukrieh <sukria@cpan.org> and others,
see the AUTHORS file that comes with this distribution for details.
=head1 LICENSE
This module is free software and is released under the same terms as Perl
itself.
=head1 SEE ALSO
L<Dancer>
=cut