package Dancer2::Config; # ABSTRACT: Configure Dancer2 to suit your needs __END__ =pod =encoding UTF-8 =head1 NAME Dancer2::Config - Configure Dancer2 to suit your needs =head1 VERSION version 1.1.0 =head1 DESCRIPTION The Dancer2 configuration (as implemented by L) handles reading and changing the configuration of your Dancer2 apps. This document describes how to manipulate Dancer2's configuration settings (through code or by file), and to document the various settings that are available in Dancer2. =head1 MANIPULATING SETTINGS VIA CODE You can change a setting with the keyword C: use Dancer2; # changing default settings set port => 8080; set content_type => 'text/plain'; set startup_info => 0; =head1 MANIPULATING SETTINGS VIA CONFIGURATION FILES There's nothing wrong with using C to configure your application. In fact you might have some great reasons for doing so. For greater flexibility, ease of deployment, etc., you should also consider extracting those settings into a configuration file. =head2 Configuration file path and file names Dancer2 will first look for the file F (where F is the type of configuration file you are using; e.g. F or F or F) in the root directory of your application. This is considered your global Dancer2 config file. If you do not care to have separate settings for production and development environments (B a recommended practice!), then this file is all you need. Next, Dancer2 will look for a file called F. This file is typically useful for deployment-specific configuration that should not be checked into source control. For instance, database credentials could be stored in this file. Any settings in this file are merged into the existing configuration such that those with the same name in your local configuration file will take precedence over those settings in the global file. Next, Dancer2 will look in the F directory for a configuration file specific to the platform you are deploying to (F or F, for example). Again, the configuration from the environment is merged with the existing configuration with the deployment config taking precedence. Finally, Dancer2 will look in the F directory for a local configuration for the specific platform you are deploying to (e.g. F or F) The configuration in this file is merged as before. Much like F, this file would be useful for environment- specific configuration that would not be checked into source control. For instance, when developing an application that talks to multiple services, each developer could have their own URLs to those services stored within their F file. Note, if there is no F, Dancer2 will not look for a F. The same is true for the local environment configuration. =head2 Supported configuration file formats Dancer2 supports any configuration file format that is supported by L. At the time of this writing, that includes YAML (.yml and .yaml), JSON (.jsn and .json), INI (.ini), Apache-style configurations (.cnf and .conf), XML (.xml), and Perl-style hashes (.pl and .perl). Dancer2 iterates over these file extensions in the order provided by L and loads any config files that it finds with later configuration information overriding earlier config information. To restrict which file extension Dancer2 looks for, you may set the C envinroment variable to a specific extension and Dancer2 will only look for config files with that extension. Make sure you pick the appropriate extension for your configuration file name, as Dancer2 guesses the type of format based on the file extension. =head2 Sample configuration files Note: Not all possibilities are covered here, only the most common options. If you prefer YAML, a sample YAML based config file might look like this: appname: "Hello" charset: "UTF-8" auto_page: 1 session: "YAML" serializer: "JSON" plugins: DBIC: default: dsn: dbi:SQLite:db/mydata.db schema_class: Hello::Schema If JSON is more your thing, your file might look more like this: { "appname": "Hello", "charset": "UTF-8", "auto_page": "1", "session": "YAML", "serializer": "JSON", "plugins": { "DBIC": { "default": { "dsn": "dbi:SQLite:db/mydata.db", "schema_class": "Hello::Schema" } } } } If you like Apache configuration files, try something similar to: appname = Hello charset = UTF-8 auto_page = 1 session = YAML serializer = JSON dsn = dbi =SQLite =db/mydata.db schema_class = Hello = =Schema INI-style files are deliberately simplistic and not recommended for use in your Dancer2 applications. =head1 SUPPORTED SETTINGS =head2 Run mode and listening interface/port =head3 host (string) The IP address that the Dancer2 app should bind to. Default is 0.0.0.0, i.e. bind to all available interfaces. =head3 port (int) The port Dancer2 will listen to. Default value is 3000. This setting can be changed on the command-line with the B<--port> switch. =head3 behind_proxy (boolean) If set to true, Dancer2 will look to C and C when constructing URLs (for example, when using C or C). This is useful if your application is behind a proxy. B: If either of these are missing, the values of the proxy server will be used instead. For example, if the client sends a HTTP/1.0 request to a proxy that is hosted locally, then C will return the value "localhost". In a similar vein, if the client makes a secure connection to the proxy, but the proxy does not pass C, then C will return "http://...". For these reasons, it is recommended that the values are hard-configured in the proxy if possible. For Apache this would be: RequestHeader set X_FORWARDED_PROTO "https" RequestHeader set X_FORWARDED_HOST "www.example.com" =head3 no_default_middleware (boolean) If set to true, your Dancer2 application will B be wrapped with the default PSGI middleware. The default middleware wrappers are: =over 4 =item * L =item * L =back =head2 Content type / character set =head3 content_type (string) The default content type of outgoing content. Default value is 'text/html'. =head3 charset (string) This setting has multiple effects: =over =item * It sets the default charset of outgoing content. C item will be added to Content-Type response header. =item * It makes Unicode bodies in HTTP responses of C types to be encoded to this charset. =item * It also indicates to Dancer2 in which charset the static files and templates are encoded. =item * If you're using L, UTF-8 support will automatically be enabled for your database - see L =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 Content-Type, you should always encode them by hand. =head3 default_mime_type (string) Dancer2's L module uses C as a default mime type. This setting lets the user change it. For example, if you have a lot of files being served in the B folder that do not have an extension, and are text files, set the C to C. =head2 Serializing responses =head3 serializer (string) When writing a webservice, data serialization/deserialization is a common issue to deal with. Dancer2 can automatically handle that for you, via a serializer. =head3 Available serializer engines The following serializers are available, be aware they dynamically depend on Perl modules you may not have on your system. =over 4 =item * B Requires L. =item * B Requires L, =item * B Requires L. =item * B Will try to find the appropriate serializer using the B and B header of the request. =back =head2 Serializer engine The serializer can be configured in a separate C section, like so: serializer: "JSON" engines: serializer: JSON: pretty: 1 See documentation for a particular serializer for supported options. =head2 File / directory locations =head3 environment (string) This is the name of the environment that should be used. Standard Dancer2 applications have a C folder with specific configuration files for different environments (usually development and production environments). They specify different kind of error reporting, deployment details, etc. These files are read after the generic C configuration file. =head3 appdir (directory) This is the path where your application will live. It's where Dancer2 will look by default for your config files, templates and static content. It is typically set by C to use the same directory as your script. =head3 public_dir (directory) This is the directory, where static files are stored. Any existing file in that directory will be served as a static file, before matching any route. See also B. Default: B<< C<$appdir/public> >>. =head3 static_handler (boolean) This setting have to be declared and set to true if you modify standard C location. Default: true if C<$ENV{DANCER_PUBLIC}> is set or C is set to B<< C<$appdir/public> >>. =head3 views (directory) This is the directory where your templates and layouts live. It's the "view" part of MVC (model, view, controller). Default: B<< C<$appdir/views> >>. =head2 Templating & layouts =head3 template Allows you to configure which template engine should be used. For instance, to use Template Toolkit, add the following to C: template: template_toolkit =head3 layout (string) The name of the layout to use when rendering view. Dancer2 will look for a matching template in the directory C<$views/layouts>. Your can override the default layout using the third argument of the C