The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Dancer::Config - Configure Dancer to suit your needs

VERSION

version 1.9999_01

DESCRIPTION

The Dancer configuration (as implemented by " Dancer::Core::Role::Config ") handles reading and changing the configuration of your Dancer apps. This document aims to describe how to manipulate Dancer's configuration settings (through code or by file), and to document the various settings that are available in Dancer.

MANIPULATING SETTINGS VIA CODE

You can change a setting with the keyword set, like the following:

    use Dancer;

    # changing default settings
    set port         => 8080;
    set content_type => 'text/plain';
    set startup_info => 0;

MANIPULATING SETTINGS VIA CONFIGURATION FILES

There's nothing wrong with using set to configure your application. In fact you might have some great reasons for doing so. For greater flexibilty, ease of deployment, etc., you should consider extracting those settings into a configuration file.

Configuration file path and file names

Dancer will first look for the file config.ext (where .ext is the type of configuration file you are using) in the root directory of your application. This is considered your global Dancer config file. If you do not care to have separate settings for production and development environments ( not a recommended practice!), then this file is all you need.

Next, Dancer will look in the environments directory for a configuration file specific to the platform you are deploying to ( production.ext and development.ext , for example). Any settings in these files that are named the same as settings in your global configuration file will take precedence over those settings in the global file. The rest of the settings are merged and Dancer uses the combination of settings from the two files as its operating configuration.

Dancer will not guess what type of file format you are using - it will attempt to read your configuration file using a parser that is applicable to the file extension you use for your configuration file.

Supported configuration file formats

Dancer supports any configuration file format that is supported by " Config::Any ". 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).

Make sure you pick the appropriate extension for your configuration file name, as Dancer will not guess the type of format you are using.

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
        <plugins>
            <DBIC>
                <default>
                    dsn = dbi =SQLite =db/mydata.db
                    schema_class = Hello = =Schema
                </default>
            </DBIC>
        </plugins>

INI-style files are deliberately simplistic and not recommended for use in your Dancer applications.

SUPPORTED SETTINGS

Run mode and listening interface/port

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.

port (int)

The port Dancer will listen to.

Default value is 3000. This setting can be changed on the command-line with the --port switch.

daemon (boolean)

If set to true, runs the standalone webserver in the background. This setting can be changed on the command-line with the --daemon flag.

behind_proxy (boolean)

If set to true, Dancer will look to X-Forwarded-Protocol and X-Forwarded-host when constructing URLs (for example, when using redirect). This is useful if your application is behind a proxy.

Content type / character set

content_type (string)

The default content type of outgoing content. Default value is 'text/html'.

charset (string)

This setting has multiple effects:

  • It sets the default charset of outgoing content. charset= item will be added to Content-Type response header.

  • It makes Unicode bodies in HTTP responses of text/* types to be encoded to this charset.

  • It also indicates to Dancer in which charset the static files and templates are encoded.

  • If you're using Dancer::Plugin::Database, UTF-8 support will automatically be enabled for your database - see "AUTOMATIC UTF-8 SUPPORT" in Dancer::Plugin::Database

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 application/json Content-Type, you should always encode them by hand.

default_mime_type (string)

Dancer's Dancer::MIME module uses application/data 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 public folder that do not have an extension, and are text files, set the default_mime_type to text/plain.

File / directory locations

environment (string)

This is the name of the environment that should be used. Standard Dancer applications have a environments 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 config.yml configuration file.

The running environment can be set with:

   set environment => "production";

Note that this variable is also used as a default value if other values are not defined.

appdir (directory)

This is the path where your application will live. It's where Dancer will look by default for your config files, templates and static content.

It is typically set by use Dancer to use the same directory as your script.

public (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.

By default, it points to $appdir/public.

views (directory)

This is the directory where your templates and layouts live. It's the "view" part of MVC (model, view, controller).

This defaults to $appdir/views.

Templating & layouts

template

Allows you to configure which template engine should be used. For instance, to use Template Toolkit, add the following to config.yml:

    template: template_toolkit

layout (string)

The name of the layout to use when rendering view. Dancer will look for a matching template in the directory $views/layout.

Your can override the default layout using the third argument of the template keyword. Check Dancer manpage for details.

Logging, debugging and error handling

strict_config (boolean, default: false)

If true, config will return an object instead of a hash reference. See Dancer::Config::Object for more information.

import_warnings (boolean, default: enabled)

If true, or not present, use warnings will be in effect in scripts in which you import Dancer. Set to a false value to disable this.

startup_info (boolean)

If set to true, prints a banner at the server start with information such as versions and the environment (or "dancerfloor").

Conforms to the environment variable DANCER_STARTUP_INFO.

warnings (boolean)

If set to true, tells Dancer to consider all warnings as blocking errors.

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.

server_tokens (boolean)

If set to true, Dancer will add an "X-Powered-By" header and also append the Dancer version to the "Server" header. Default to true.

You can also use the environment variable DANCER_SERVER_TOKENS.

log_path (string)

Folder where the ``file logger'' saves logfiles.

log_file (string)

Name of the file to create when ``file logger'' is active. It defaults to the environment setting contents.

logger (enum)

Select which logger to use. For example, to write to log files in log_path:

    logger: file

Or to direct log messages to the console from which you started your Dancer app in standalone mode,

    logger: console

Various other logger backends are available on CPAN, including Dancer::Logger::Syslog, Dancer::Logger::Log4perl, Dancer::Logger::PSGI (which can, with the aid of Plack middlewares, send log messages to a browser's console window) and others.

log (enum)

See the Logger engine bellow.

show_errors (boolean)

If set to true, Dancer will render a detailed debug screen whenever an error is caught. If set to false, Dancer will render the default error page, using $public/$error_code.html if it exists or the template specified by the error_template setting.

The error screen attempts to sanitise sensitive looking information (passwords / card numbers in the request, etc) but you still should not have show_errors enabled whilst in production, as there is still a risk of divulging details.

error_template (template path)

This setting lets you specify a template to be used in case of runtime error. At the present moment the template can use three variables:

title

The error title.

message

The error message.

code

The code throwing that error.

auto_reload (boolean)

Requires Module::Refresh and 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 should not be enabled in production. Enabling this flag in production yields a major negative effect on performance because of 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 Module::Refresh only operates on files in %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.

Logger engine

log_level

This is the old top-level log entry that is still supported. With Dancer 2 we suggest you use log_level under the Loger engine configuration:

   engines:
     logger:
       console:
         log_level: core

With this approach you can set different log levels accordingly with the logger you use.

This option tells which log messages should be actually logged. Possible values are core, debug, warning or error.

core : all messages are logged, including some from Dancer itself
debug : all messages are logged
info : only info, warning and error messages are logged
warning : only warning and error messages are logged
error : only error messages are logged

During development, you'll probably want to use debug to see your own debug messages, and core if you need to see what Dancer is doing. In production, you'll likely want error or warning only, for less-chatty logs.

Session engine

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 Dancer::Session for supported engines and their respective configuration.

session_expires

The session expiry time in seconds, or as e.g. "2 hours" (see "expires" in Dancer::Cookie. By default, there is no specific expiry time.

session_name

The name of the cookie to store the session ID in. Defaults to dancer.session. This can be overridden by certain session engines.

session_secure

The user's session ID is stored in a cookie. If the session_secure setting is set to a true value, the cookie will be marked as secure, meaning it should only be sent over HTTPS connections.

session_is_http_only

This setting defaults to 1 and instructs the session cookie to be created with the HttpOnly option active, meaning that JavaScript will not be able to access to its value.

auto_page (boolean)

For simple pages where you're not doing anything dynamic, but still want to use the template engine to provide headers etc, you can use the auto_page feature to avoid the need to create a route for each page.

With auto_page enabled, if the requested path does not match any specific route, Dancer will check in the views directory for a matching template, and use it to satisfy the request if found.

Simply enable auto_page in your config:

    auto_page: 1

Then, if you request /foo/bar, Dancer will look in the views dir for /foo/bar.tt.

Dancer will honor your before_template_render code, and all default variables. They will be accessible and interpolated on automatic served pages.

DANCER_CONFDIR and DANCER_ENVDIR

It's possible to set the configuration directory and environment directory using this two environment variables. Setting `DANCER_CONFDIR` will have the same effect as doing

    set confdir => '/path/to/confdir'

and setting `DANCER_ENVDIR` will be similar to:

    set envdir => '/path/to/environments'

SEE ALSO

Dancer

AUTHOR

Dancer Core Developers

COPYRIGHT AND LICENSE

This software is copyright (c) 2012 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.

2 POD Errors

The following errors were encountered while parsing the POD:

Around line 18:

L<> starts or ends with whitespace

Around line 64:

L<> starts or ends with whitespace