Module rocket::config [] [src]

Application configuration and configuration parameter retrieval.

This module implements configuration handling for Rocket. It implements the parsing and interpretation of the Rocket.toml config file and ROCKET_{PARAM} environment variables. It also allows libraries to access values that have been configured by the user.

Application Configuration


Rocket applications are always running in one of three environments:

Each environment can contain different configuration parameters. By default, Rocket applications run in the development environment. The environment can be changed via the ROCKET_ENV environment variable. For example, to start a Rocket application in the production environment:

ROCKET_ENV=production ./target/release/rocket_app

Configuration Parameters

Each environments consists of several standard configuration parameters as well as an arbitrary number of extra configuration parameters, which are not used by Rocket itself but can be used by external libraries. The standard configuration parameters are:


The Rocket.toml file is used to specify the configuration parameters for each environment. The file is optional. If it is not present, the default configuration parameters are used.

The file must be a series of TOML tables, at most one for each environment and an optional "global" table, where each table contains key-value pairs corresponding to configuration parameters for that environment. If a configuration parameter is missing, the default value is used. The following is a complete Rocket.toml file, where every standard configuration parameter is specified with the default value:

address = "localhost"
port = 8000
workers = max(number_of_cpus, 2)
log = "normal"

address = ""
port = 80
workers = max(number_of_cpus, 2)
log = "normal"
# don't use this key! generate your own and keep it private!
session_key = "VheMwXIBygSmOlZAhuWl2B+zgvTN3WW5"

address = ""
port = 80
workers = max(number_of_cpus, 2)
log = "critical"
# don't use this key! generate your own and keep it private!
session_key = "adL5fFIPmZBrlyHk2YT4NLV3YCk2gFXz"

The workers parameter is computed by Rocket automatically; the value above is not valid TOML syntax. When manually specifying the number of workers, the value should be an integer: workers = 10.

The "global" pseudo-environment can be used to set and/or override configuration parameters globally. A parameter defined in a [global] table sets, or overrides if already present, that parameter in every environment. For example, given the following Rocket.toml file, the value of address will be "" in every environment:

address = ""

address = "localhost"

address = ""

Environment Variables

All configuration parameters, including extras, can be overridden through environment variables. To override the configuration parameter {param}, use an environment variable named ROCKET_{PARAM}. For instance, to override the "port" configuration parameter, you can run your application with:

ROCKET_PORT=3721 ./your_application

Environment variables take precedence over all other configuration methods: if the variable is set, it will be used as the value for the parameter.

Retrieving Configuration Parameters

Configuration parameters for the currently active configuration environment can be retrieved via the active function and methods on the Config structure. The general structure is to call active and then one of the get_ methods on the returned Config structure.

As an example, consider the following code used by the Template type to retrieve the value of the template_dir configuration parameter. If the value isn't present or isn't a string, a default value is used.

use std::path::PathBuf;
use rocket::config::{self, ConfigError};

const DEFAULT_TEMPLATE_DIR: &'static str = "templates";

let template_dir = config::active().ok_or(ConfigError::NotFound)
    .map(|config| config.root().join(DEFAULT_TEMPLATE_DIR))
    .unwrap_or_else(|_| PathBuf::from(DEFAULT_TEMPLATE_DIR));

Libraries should always use a default if a parameter is not defined.



Structure for Rocket application configuration.


Structure following the builder pattern for building Config structures.


The type of a configuration parsing error.



The type of a configuration error.


An enum corresponding to the valid configuration environments.


Representation of a TOML value.



Conversion trait from standard types into TOML Values.



Retrieve the active configuration, if there is one.

Type Definitions


Type representing a TOML array, payload of the Value::Array variant


Wraps std::result with the error type of ConfigError.


Type representing a TOML table, payload of the Value::Table variant