Debian Codegraph @

lib/Dancer2/Manual/Config.pod

raw
1 package Dancer2::Config;
2 # ABSTRACT: Dancer2 Configuration Reference
3
4 __END__
5
6 =pod
7
8 =encoding UTF-8
9
10 =head1 NAME
11
12 Dancer2::Config - Dancer2 Configuration Reference
13
14 =head1 VERSION
15
16 version 2.2.2
17
18 =head1 DESCRIPTION
19
20 The Dancer2 default configuration system (as implemented by
21 L<Dancer2::ConfigReader::Config::Any>) handles reading and changing the
22 configuration of your Dancer2 apps. This document describes how to
23 manipulate Dancer2's configuration settings (through code or by file), and
24 to document the various settings that are available in Dancer2.
25
26 =head1 MANIPULATING SETTINGS VIA CODE
27
28 You can change a setting with the keyword C<set>:
29
30 use Dancer2;
31
32 # changing default settings
33 set port => 8080;
34 set content_type => 'text/plain';
35 set startup_info => 0;
36
37 =head1 MANIPULATING SETTINGS VIA CONFIGURATION FILES
38
39 There's nothing wrong with using C<set> to configure your application. In
40 fact you might have some great reasons for doing so. For greater
41 flexibility, ease of deployment, etc., you should also consider extracting
42 those settings into a configuration file.
43
44 =head2 Configuration file path and file names
45
46 Dancer2 will first look for the file F<config.EXT> (where F<EXT> is the
47 type of configuration file you are using; e.g. F<ini> or F<json> or
48 F<yml>) in the root directory of your application. This is considered
49 your global Dancer2 config file. If you do not care to have separate
50 settings for production and development environments (B<not> a
51 recommended practice!), then this file is all you need.
52
53 Next, Dancer2 will look for a file called F<config_local.EXT>. This file
54 is typically useful for deployment-specific configuration that should
55 not be checked into source control. For instance, database credentials
56 could be stored in this file. Any settings in this file are merged into
57 the existing configuration such that those with the same name in your
58 local configuration file will take precedence over those settings in
59 the global file.
60
61 Next, Dancer2 will look in the F<environments> directory for a configuration
62 file specific to the platform you are deploying to (F<production.EXT> or
63 F<development.EXT>, for example). Again, the configuration from the environment
64 is merged with the existing configuration with the deployment config taking
65 precedence.
66
67 Finally, Dancer2 will look in the F<environments> directory for a
68 local configuration for the specific platform you are deploying to
69 (e.g. F<production_local.EXT> or F<development_local.EXT>)
70 The configuration in this file is merged as before.
71
72 Much like F<config_local.EXT>, this file would be useful for environment-
73 specific configuration that would not be checked into source control.
74 For instance, when developing an application that talks to multiple services,
75 each developer could have their own URLs to those services stored within
76 their F<environments/development_local.yaml> file.
77
78 Note, if there is no F<config.EXT>, Dancer2 will not look for a
79 F<config_local.EXT>. The same is true for the local environment
80 configuration.
81
82 =head2 Supported configuration file formats
83
84 Dancer2 supports any configuration file format that is supported by
85 L<Config::Any>. At the time of this writing, that includes YAML (.yml and
86 .yaml), JSON (.jsn and .json), INI (.ini), Apache-style configurations (.cnf
87 and .conf), XML (.xml), and Perl-style hashes (.pl and .perl).
88
89 Dancer2 iterates over these file extensions in the order provided by
90 L<Config::Any> and loads any config files that it finds with later
91 configuration information overriding earlier config information. To
92 restrict which file extension Dancer2 looks for, you may set the
93 C<DANCER_CONFIG_EXT> envinroment variable to a specific extension and
94 Dancer2 will only look for config files with that extension.
95
96 Make sure you pick the appropriate extension for your configuration file
97 name, as Dancer2 guesses the type of format based on the file extension.
98
99 =head2 Sample configuration files
100
101 Note: Not all possibilities are covered here, only the most common options.
102
103 If you prefer YAML, a sample YAML based config file might look like this:
104
105 appname: "Hello"
106 charset: "UTF-8"
107 auto_page: 1
108
109 session: "YAML"
110 serializer: "JSON"
111
112 plugins:
113 DBIC:
114 default:
115 dsn: dbi:SQLite:db/mydata.db
116 schema_class: Hello::Schema
117
118 If JSON is more your thing, your file might look more like this:
119
120 {
121 "appname": "Hello",
122 "charset": "UTF-8",
123 "auto_page": "1",
124 "session": "YAML",
125 "serializer": "JSON",
126 "plugins": {
127 "DBIC": {
128 "default": {
129 "dsn": "dbi:SQLite:db/mydata.db",
130 "schema_class": "Hello::Schema"
131 }
132 }
133 }
134 }
135
136 If you like Apache configuration files, try something similar to:
137
138 appname = Hello
139 charset = UTF-8
140 auto_page = 1
141 session = YAML
142 serializer = JSON
143 <plugins>
144 <DBIC>
145 <default>
146 dsn = dbi =SQLite =db/mydata.db
147 schema_class = Hello = =Schema
148 </default>
149 </DBIC>
150 </plugins>
151
152 INI-style files are deliberately simplistic and not recommended for use in
153 your Dancer2 applications.
154
155 =head1 USING ALTERNATE CONFIG READERS
156
157 Dancer2 allows you to use your own configuration system too. See the
158 L<Dancer2::ConfigReader> documentation for more information.
159
160 =head2 Bootstrapping other config readers
161
162 You can use the default Dancer2 configuration reader to load additional
163 configuration/configuration readers:
164
165 additional_config_readers:
166 - Dancer2::ConfigReader::SQLite:
167 path: /path/to/sqlite.db
168 table: config
169
170 See the L<Dancer2::ConfigReader> documentation for more information and
171 examples.
172
173 =head1 SUPPORTED SETTINGS
174
175 =head2 Run mode and listening interface/port
176
177 =head3 host (string)
178
179 The IP address that the Dancer2 app should bind to. Default is 0.0.0.0,
180 i.e. bind to all available interfaces.
181
182 =head3 port (int)
183
184 The port Dancer2 will listen to.
185
186 Default value is 3000. This setting can be changed on the command-line with
187 the B<--port> switch.
188
189 =head3 behind_proxy (boolean)
190
191 If set to true, Dancer2 will look to C<X-Forwarded-Protocol> and
192 C<X-Forwarded-host> when constructing URLs (for example, when using C<redirect>
193 or C<host>). This is useful if your application is behind a proxy.
194
195 B<Note>: If either of these are missing, the values of the proxy server will be
196 used instead. For example, if the client sends a HTTP/1.0 request to a proxy
197 that is hosted locally, then C<host> will return the value "localhost". In a
198 similar vein, if the client makes a secure connection to the proxy, but the
199 proxy does not pass C<X-Forwarded-Protocol>, then C<base> will return
200 "http://...". For these reasons, it is recommended that the values are
201 hard-configured in the proxy if possible. For Apache this would be:
202
203 RequestHeader set X_FORWARDED_PROTO "https"
204 RequestHeader set X_FORWARDED_HOST "www.example.com"
205
206 =head3 no_default_middleware (boolean)
207
208 If set to true, your Dancer2 application will B<NOT> be wrapped with the default
209 PSGI middleware. The default middleware wrappers are:
210
211 =over 4
212
213 =item * L<Plack::Middleware::FixMissingBodyInRedirect>
214
215 =item * L<Plack::Middleware::Head>
216
217 =back
218
219 =head3 additional_config_readers
220
221 L<Dancer2::ConfigReader> instances to bootstrap the application's
222 configuration. See the ConfigReader's POD for more information.
223
224 =head2 Content type / character set
225
226 =head3 content_type (string)
227
228 The default content type of outgoing content. Default value is 'text/html'.
229
230 =head3 charset (string)
231
232 This setting has multiple effects:
233
234 =over
235
236 =item *
237
238 It sets the default charset of outgoing content. C<charset=> item will be
239 added to Content-Type response header.
240
241 =item *
242
243 It makes Unicode bodies in HTTP responses of C<text/*> types to be encoded
244 to this charset.
245
246 =item *
247
248 It also indicates to Dancer2 in which charset the static files and templates
249 are encoded.
250
251 =item *
252
253 If you're using L<Dancer2::Plugin::Database>, UTF-8 support will
254 automatically be enabled for your database - see
255 L<Dancer2::Plugin::Database/"AUTOMATIC UTF-8 SUPPORT">
256
257 =back
258
259 Default value is C<UTF-8>. HTTP responses without charset will be interpreted
260 as ISO-8859-1 by most clients.
261
262 If C<charset> is empty, Dancer2 will not encode response bodies. Handlers
263 should return already-encoded byte strings.
264
265 You can cancel any charset processing by specifying your own charset in
266 Content-Type header or by ensuring that response body leaves your handler
267 without Unicode flag set (by encoding it into some 8bit charset, for
268 example).
269
270 Serialized JSON responses are UTF-8 encoded by the JSON serializer. Pass
271 character strings to the serializer so it can do the encoding once.
272
273 =head3 strict_utf8 (boolean)
274
275 Controls how invalid UTF-8 bytes are handled at decoding boundaries (request
276 path/parameters) and in JSON serialization.
277
278 =over
279
280 =item * If false (default), invalid UTF-8 is left as bytes and a warning is
281 emitted.
282
283 =item * If true, invalid UTF-8 causes an immediate error.
284
285 =back
286
287 =head3 default_mime_type (string)
288
289 Dancer2's L<Dancer2::Core::MIME> module uses C<application/data> as a
290 default mime type. This setting lets the user change it. For example, if you
291 have a lot of files being served in the B<public> folder that do not have an
292 extension, and are text files, set the C<default_mime_type> to
293 C<text/plain>.
294
295 =head2 Serializing responses
296
297 =head3 serializer (string)
298
299 When writing a webservice, data serialization/deserialization is a common
300 issue to deal with. Dancer2 can automatically handle that for you, via a
301 serializer.
302
303 =head3 Available serializer engines
304
305 The following serializers are available, be aware they dynamically depend on
306 Perl modules you may not have on your system.
307
308 =over 4
309
310 =item * B<JSON>
311
312 Requires L<JSON>.
313
314 =item * B<YAML>
315
316 Requires L<YAML>,
317
318 =item * B<XML>
319
320 Requires L<XML::Simple>.
321
322 =item * B<Mutable>
323
324 Will try to find the appropriate serializer using the B<Content-Type> and
325 B<Accept-type> header of the request.
326
327 =back
328
329 =head2 Serializer engine
330
331 The serializer can be configured in a separate C<engines> section, like so:
332
333 serializer: "JSON"
334
335 engines:
336 serializer:
337 JSON:
338 pretty: 1
339
340 See documentation for a particular serializer for supported options.
341
342 =head2 File / directory locations
343
344 =head3 environment (string)
345
346 This is the name of the environment that should be used. Standard Dancer2
347 applications have a C<environments> folder with specific configuration files
348 for different environments (usually development and production
349 environments). They specify different kind of error reporting, deployment
350 details, etc. These files are read after the generic C<config.yml>
351 configuration file.
352
353 =head3 appdir (directory)
354
355 This is the path where your application will live. It's where Dancer2 will
356 look by default for your config files, templates and static content.
357
358 It is typically set by C<use Dancer2> to use the same directory as your
359 script.
360
361 =head3 public_dir (directory)
362
363 This is the directory, where static files are stored. Any existing file in
364 that directory will be served as a static file, before matching any route.
365
366 See also B<static_handler>.
367
368 Default: B<< C<$appdir/public> >>.
369
370 =head3 static_handler (boolean)
371
372 This setting have to be declared and set to true if you modify standard
373 C<public_dir> location.
374
375 Default: true if C<$ENV{DANCER_PUBLIC}> is set or C<public_dir> is set to
376 B<< C<$appdir/public> >>.
377
378 =head3 views (directory)
379
380 This is the directory where your templates and layouts live. It's the
381 "view" part of MVC (model, view, controller).
382
383 Default: B<< C<$appdir/views> >>.
384
385 C<views> can only be set once, preferably via config. Once the template
386 engine is initialized, changing the views setting has no effect.
387
388 =head2 Templating & layouts
389
390 =head3 template
391
392 Allows you to configure which template engine should be used. For instance,
393 to use Template Toolkit, add the following to C<config.yml>:
394
395 template: template_toolkit
396
397 =head3 layout (string)
398
399 The name of the layout to use when rendering view. Dancer2 will look for a
400 matching template in the directory C<$views/layouts>.
401
402 Your can override the default layout using the third argument of the
403 C<template> keyword. Check C<Dancer2> manpage for details.
404
405 =head3 layout_dir (string)
406
407 A relative path where the layouts reside inside the C<views> directory.
408
409 layout_dir: actual_layouts
410
411 Default: B<layouts>.
412
413 =head2 Logging, debugging and error handling
414
415 =head3 startup_info (boolean)
416
417 If set to true, prints a banner at the server start with information such as
418 versions and the environment (or "dancefloor").
419
420 Conforms to the environment variable C<DANCER_STARTUP_INFO>.
421
422 =head3 traces (boolean)
423
424 If set to true, Dancer2 will display full stack traces when a warning or a
425 die occurs. (Internally sets Carp::Verbose). Default to false.
426
427 =head3 strict_config (boolean)
428
429 If set to true, Dancer2 will warn about unknown configuration
430 keys at the top level and about unknown keys in built-in engine sections where
431 the options are known. Set this to false if you intentionally store
432 application-specific settings at the top level or use custom engines with
433 additional configuration keys.
434
435 =head3 strict_config_allow (array)
436
437 List of top-level configuration keys that should be ignored by strict config
438 warnings. This allows you to keep C<strict_config> enabled while permitting
439 application-specific keys.
440
441 strict_config: 1
442 strict_config_allow:
443 - my_custom_key
444 - feature_flags
445
446 =head3 no_server_tokens (boolean)
447
448 If set to true, Dancer2 will B<not> add an C<X-Powered-By> header, and it
449 will avoid setting the "Server" header when using Dancer2's standalone
450 server.
451
452 Default to B<false>.
453
454 You can also use the environment variable C<DANCER_NO_SERVER_TOKENS>.
455
456 =head3 logger (enum)
457
458 Select which logger to use. For example, to write to log files with
459 L<Dancer2::Logger::File>:
460
461 logger: File
462
463 Or to direct log messages to the console from which you started your Dancer2
464 app with L<Dancer2::Logger::Console>:
465
466 logger: Console
467
468 Loggers are configured with a corresponding L</Logger engine> section, as
469 shown below.
470
471 =head3 session (enum)
472
473 This setting lets you enable a session engine for your web application. By
474 default, sessions are disabled in Dancer2, you must choose a session engine
475 to use them.
476
477 Sessions are configured with a corresponding L</Session engine> section, as
478 shown below.
479
480 =head3 show_errors (boolean)
481
482 Deprecated configuration.
483
484 Use C<show_stacktrace> described below instead.
485
486 =head3 show_stacktrace (boolean)
487
488 If set to true, Dancer2 will render a detailed debug screen whenever an
489 error is caught. If set to false, Dancer2 will render the default error
490 page, using C<$public/$error_code.html> if it exists, C<$views/$error_code.tt> or the template specified
491 by the C<error_template> setting.
492
493 The error screen attempts to sanitise sensitive looking information
494 (passwords / card numbers in the request, etc) but you still should not have
495 show_stacktrace enabled whilst in production, as there is still a risk of
496 divulging details.
497
498 =head3 error_template (template path)
499
500 This setting lets you specify a template to be used in case of runtime
501 error. At the present moment the template (as well as C<$views/$error_code.tt> templates) can use four variables:
502
503 =over 4
504
505 =item B<title>
506
507 The error title.
508
509 =item B<content>
510
511 The error specific content (if any).
512
513 =item B<status>
514
515 The HTTP status code throwing that error.
516
517 =item B<exception>
518
519 The stringified exception (e.g. $@) if any.
520
521 =back
522
523 Keep in mind that 'content' and 'exception' can vary depending on the problem.
524
525 For example:
526
527 A 404 has an empty 'exception' and 'content' contains the URI that was not found. Unless you do the 404 yourself via C<send_error("You chose ... poorly!", 404);>, then 'content' is 'You chose ... poorly!'.
528
529 A 500 because of, say, dividing 0 by 0 will have an empty 'content' and 'exception like 'Illegal division by zero at ...'.
530
531 A 401 from C<send_error("You can not know the secret until you sign in grasshopper!", 401);> will have an empty 'exception' and 'content' will contain 'You can not know the secret until you sign in grasshopper!'.
532
533 =head2 Logger engine
534
535 The logger must be configured in a separate C<engines> section, like so:
536
537 logger: Console
538
539 engines:
540 logger:
541 Console:
542 log_level: core
543
544 All loggers support the configuration options below. See documentation for
545 a particular logger for other supported options.
546
547 =head3 log_level
548
549 This option tells which log messages should be actually logged. Possible
550 values are B<core>, B<info>, B<debug>, B<warning> or B<error>.
551
552 =over 4
553
554 =item B<core> : all messages are logged, including some from Dancer2 itself
555
556 =item B<debug> : all messages are logged
557
558 =item B<info> : only info, warning and error messages are logged
559
560 =item B<warning> : only warning and error messages are logged
561
562 =item B<error> : only error messages are logged
563
564 =back
565
566 During development, you'll probably want to use C<debug> to see your own
567 debug messages, and C<core> if you need to see what Dancer2 is doing. In
568 production, you'll likely want C<error> or C<warning> only, for less-chatty
569 logs.
570
571 =head2 Session engine
572
573 The session engine is configured in the C<engines> section.
574
575 session: Simple
576
577 engines:
578 session:
579 Simple:
580 cookie_name: dance.set
581 cookie_duration: '24 hours'
582 cookie_same_site: Lax
583 is_secure: 1
584 is_http_only: 1
585
586 See L<Dancer2::Core::Role::SessionFactory> for more detailed documentation
587 for these options, or the particular session engine for other supported
588 options.
589
590 =head3 cookie_name
591
592 The name of the cookie to store the session ID in. Defaults to
593 C<dancer.session>. This can be overridden by certain session engines.
594
595 =head3 cookie_domain
596
597 The domain of the cookie. By default there is no domain defined for the
598 cookie.
599
600 =head3 cookie_path
601
602 The path of the cookie. By default there is no path defined for the cookie.
603
604 =head3 cookie_duration
605
606 The session expiry time in seconds, or as e.g. "2 hours" (see
607 L<Dancer2::Core::Cookie/expires>. By default, there is no specific expiry
608 time.
609
610 =head3 cookie_same_site
611
612 Restricts the session cookie to a first-party or same-site context. Valid
613 values are C<Strict>, C<Lax>, or C<None>.
614
615 Refer to
616 L<RFC6265bis|https://tools.ietf.org/html/draft-ietf-httpbis-cookie-same-site>
617 for further details regarding same-site context.
618
619 =head3 is_secure
620
621 The user's session ID is stored in a cookie. If the C<is_secure> setting is
622 set to a true value, the cookie will be marked as secure, meaning it should
623 only be sent over HTTPS connections.
624
625 =head3 is_http_only
626
627 This setting defaults to 1 and instructs the session cookie to be created
628 with the C<HttpOnly> option active, meaning that JavaScript will not be able
629 to access to its value.
630
631 =head2 auto_page (boolean)
632
633 For simple pages where you're not doing anything dynamic, but still want to
634 use the template engine to provide headers etc, you can use the auto_page
635 feature to avoid the need to create a route for each page.
636
637 With C<auto_page> enabled, if the requested path does not match any specific
638 route, Dancer2 will check in the views directory for a matching template,
639 and use it to satisfy the request if found.
640
641 Simply enable auto_page in your config:
642
643 auto_page: 1
644
645 Then, if you request C</foo/bar>, Dancer2 will look in the views dir for
646 C</foo/bar.tt>.
647
648 Dancer2 will honor your C<before_template_render> code, and all default
649 variables. They will be accessible and interpolated on automatic served
650 pages.
651
652 =head2 dsl_class
653
654 For complex applications that require extended DSL keywords or other
655 functionality the DSL class used can be specified at import time or in the
656 config settings.
657
658 dsl_class: 'My::DSL'
659
660 This is the same as specifying
661
662 use Dancer2 dsl => 'My::DSL'
663
664 in your module. dsl_class defaults to L<Dancer2::Core::DSL> if not specified
665
666 =head2 Environment variables
667
668 =head3 DANCER_CONFIG_READERS
669
670 Specifies which configuration reader classes can be used to load
671 Dancer2 configuration.
672
673 =head3 DANCER_CONFDIR
674
675 Sets the configuration directory. This is only guaranteed to work with
676 the default config reader.
677
678 =head3 DANCER_CONFIG_VERBOSE
679
680 Outputs a lot of debugging information when reading the configuration of
681 the application. This is only guaranteed to work with the default config
682 reader.
683
684 =head3 DANCER_ENVDIR
685
686 Sets the environment directory.
687
688 This correlates to the C<envdir> config option. This is only guaranteed
689 to work with the default config reader.
690
691 =head3 PLACK_ENV
692
693 Sets the given environment. This can be overridden by
694 C<DANCER_ENVIRONMENT>.
695
696 =head3 DANCER_ENVIRONMENT
697
698 Sets the given environment. This takes higher precedence over
699 C<PLACK_ENV>.
700
701 If neither C<PLACK_ENV> or C<DANCER_ENVIRONMENT> is set, the environment
702 defaults to B<development>.
703
704 =head3 DANCER_APPHANDLER
705
706 The C<DANCER_APPHANDLER> configuration controls what the C<dance> keyword
707 does.
708
709 If is set to C<PSGI> (which will automatically be set if C<PLACK_ENV> is
710 set), C<dance> will return the PSGI application coderef.
711
712 Otherwise (which is the default is - C<Standalone>), it runs the Plack
713 standalone server with the application.
714
715 =head3 DANCER_PORT
716
717 Sets the port which will be used by the development server (if not run
718 by L<plackup>).
719
720 =head3 DANCER_SERVER
721
722 Sets the host the development server will be used by the development
723 server (if not run by L<plackup>).
724
725 B<Note>: this might change in the future.
726
727 =head3 DANCER_STARTUP_INFO
728
729 Controls whether to display start up info.
730
731 =head3 DANCER_NO_SERVER_TOKENS
732
733 Controls whether to display the server tokens.
734
735 =head3 DANCER_PUBLIC
736
737 Sets the public directory location.
738
739 =head3 DANCER_TRACES
740
741 Sets the tracing flag which sets L<Carp>'s C<$Verbose> flag.
742
743 =head3 DANCER_VIEWS
744
745 Sets the views (templates) directory.
746
747 =head3 DANCER_LOGGER
748
749 Sets the logger engine.
750
751 =head3 DANCER_CHARSET
752
753 Sets the default charset.
754
755 =head3 DANCER_CONTENT_TYPE
756
757 Sets the default content type.
758
759 If not set, defaults to B<text/html>.
760
761 =head1 SEE ALSO
762
763 L<Dancer2>
764
765 =head1 AUTHOR
766
767 Dancer Core Developers
768
769 =head1 COPYRIGHT AND LICENSE
770
771 This software is copyright (c) 2026 by Alexis Sukrieh.
772
773 This is free software; you can redistribute it and/or modify it under
774 the same terms as the Perl 5 programming language system itself.
775
776 =cut