Configuration

Rocket aims to have a flexible and usable configuration system. Rocket applications can be configured via a configuration file, through environment variables, or both. Configurations are separated into three environments: development, staging, and production. The working environment is selected via an environment variable.

Environment

At any point in time, a Rocket application is operating in a given configuration environment. There are three such environments:

  • development (short: dev)
  • staging (short: stage)
  • production (short: prod)

Without any action, Rocket applications run in the development environment. The environment can be changed via the ROCKET_ENV environment variable. For example, to launch an application in the staging environment, we can run:

  1. ROCKET_ENV=stage cargo run

You’ll likely need sudo for the command to succeed since staging defaults to listening on port 80. Note that you can use the short or long form of the environment name to specify the environment, stage or staging here. Rocket tells us the environment we have chosen and its configuration when it launches:

  1. $ sudo ROCKET_ENV=staging cargo run
  2. 🔧 Configured for staging.
  3. => address: 0.0.0.0
  4. => port: 80
  5. => log: normal
  6. => workers: [logical cores * 2]
  7. => secret key: generated
  8. => limits: forms = 32KiB
  9. => tls: disabled
  10. 🛰 Mounting '/':
  11. => GET /
  12. 🚀 Rocket has launched from http://0.0.0.0:80

Rocket.toml

An optional Rocket.toml file can be used to specify the configuration parameters for each environment. If it is not present, the default configuration parameters are used. Rocket searches for the file starting at the current working directory. If it is not found there, Rocket checks the parent directory. Rocket continues checking parent directories until the root is reached.

The file must be a series of TOML tables, at most one for each environment, and an optional “global” table. 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:

  1. [development]
  2. address = "localhost"
  3. port = 8000
  4. workers = [number of cpus * 2]
  5. log = "normal"
  6. secret_key = [randomly generated at launch]
  7. limits = { forms = 32768 }
  8. [staging]
  9. address = "0.0.0.0"
  10. port = 80
  11. workers = [number of cpus * 2]
  12. log = "normal"
  13. secret_key = [randomly generated at launch]
  14. limits = { forms = 32768 }
  15. [production]
  16. address = "0.0.0.0"
  17. port = 80
  18. workers = [number of cpus * 2]
  19. log = "critical"
  20. secret_key = [randomly generated at launch]
  21. limits = { forms = 32768 }

The workers and secret_key default parameters are computed by Rocket automatically; the values above are not valid TOML syntax. When manually specifying the number of workers, the value should be an integer: workers = 10. When manually specifying the secret key, the value should a 256-bit base64 encoded string. Such a string can be generated using a tool such as openssl: openssl rand -base64 32.

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 "1.2.3.4" in every environment:

  1. [global]
  2. address = "1.2.3.4"
  3. [development]
  4. address = "localhost"
  5. [production]
  6. address = "0.0.0.0"

Data Limits

The limits parameter configures the maximum amount of data Rocket will accept for a given data type. The parameter is a table where each key corresponds to a data type and each value corresponds to the maximum size in bytes Rocket should accept for that type.

By default, Rocket limits forms to 32KiB (32768 bytes). To increase the limit, simply set the limits.forms configuration parameter. For example, to increase the forms limit to 128KiB globally, we might write:

  1. [global.limits]
  2. forms = 131072

The limits parameter can contain keys and values that are not endemic to Rocket. For instance, the Json type reads the json limit value to cap incoming JSON data. You should use the limits parameter for your application’s data limits as well. Data limits can be retrieved at runtime via the Request::limits() method.

Extras

In addition to overriding default configuration parameters, a configuration file can also define values for any number of extra configuration parameters. While these parameters aren’t used by Rocket directly, other libraries, or your own application, can use them as they wish. As an example, the Template type accepts a value for the template_dir configuration parameter. The parameter can be set in Rocket.toml as follows:

  1. [development]
  2. template_dir = "dev_templates/"
  3. [production]
  4. template_dir = "prod_templates/"

This sets the template_dir extra configuration parameter to "dev_templates/" when operating in the development environment and "prod_templates/" when operating in the production environment. Rocket will prepend the [extra] tag to extra configuration parameters when launching:

  1. 🔧 Configured for development.
  2. => ...
  3. => [extra] template_dir: "dev_templates/"

To retrieve a custom, extra configuration parameter in your application, we recommend using an ad-hoc attach fairing in combination with managed state. For example, if your application makes use of a custom assets_dir parameter:

  1. [development]
  2. assets_dir = "dev_assets/"
  3. [production]
  4. assets_dir = "prod_assets/"

The following code will:

  1. Read the configuration parameter in an ad-hoc attach fairing.
  2. Store the parsed parameter in an AssertsDir structure in managed state.
  3. Retrieve the parameter in an assets route via the State guard.
  1. struct AssetsDir(String);
  2. #[get("/<asset..>")]
  3. fn assets(asset: PathBuf, assets_dir: State<AssetsDir>) -> Option<NamedFile> {
  4. NamedFile::open(Path::new(&assets_dir.0).join(asset)).ok()
  5. }
  6. fn main() {
  7. rocket::ignite()
  8. .mount("/", routes![assets])
  9. .attach(AdHoc::on_attach(|rocket| {
  10. let assets_dir = rocket.config()
  11. .get_str("assets_dir")
  12. .unwrap_or("assets/")
  13. .to_string();
  14. Ok(rocket.manage(AssetsDir(assets_dir)))
  15. }))
  16. .launch();
  17. }

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:

  1. ROCKET_PORT=3721 ./your_application
  2. 🔧 Configured for development.
  3. => ...
  4. => port: 3721

Environment variables take precedence over all other configuration methods: if the variable is set, it will be used as the value for the parameter. Variable values are parsed as if they were TOML syntax. As illustration, consider the following examples:

  1. ROCKET_INTEGER=1
  2. ROCKET_FLOAT=3.14
  3. ROCKET_STRING=Hello
  4. ROCKET_STRING="Hello"
  5. ROCKET_BOOL=true
  6. ROCKET_ARRAY=[1,"b",3.14]
  7. ROCKET_DICT={key="abc",val=123}

Configuring TLS

Rocket includes built-in, native support for TLS >= 1.2 (Transport Layer Security). In order for TLS support to be enabled, Rocket must be compiled with the "tls" feature. To do this, add the "tls" feature to the rocket dependency in your Cargo.toml file:

  1. [dependencies]
  2. rocket = { version = "0.3.17", features = ["tls"] }

TLS is configured through the tls configuration parameter. The value of tls must be a table with two keys:

  • certs: [string] a path to a certificate chain in PEM format
  • key: [string] a path to a private key file in PEM format for the certificate in certs

The recommended way to specify these parameters is via the global environment:

  1. [global.tls]
  2. certs = "/path/to/certs.pem"
  3. key = "/path/to/key.pem"

Of course, you can always specify the configuration values per environment:

  1. [development]
  2. tls = { certs = "/path/to/certs.pem", key = "/path/to/key.pem" }

Or via environment variables:

  1. ROCKET_TLS={certs="/path/to/certs.pem",key="/path/to/key.pem"} cargo run