Struct rocket::config::ConfigBuilder

source ·
pub struct ConfigBuilder {
Show 13 fields pub environment: Environment, pub address: String, pub port: u16, pub workers: u16, pub keep_alive: u32, pub read_timeout: u32, pub write_timeout: u32, pub log_level: LoggingLevel, pub secret_key: Option<String>, pub tls: Option<(String, String)>, pub limits: Limits, pub extras: HashMap<String, Value>, pub root: Option<PathBuf>,
}
Expand description

Structure following the builder pattern for building Config structures.

Fields§

§environment: Environment

The environment that this configuration corresponds to.

§address: String

The address to serve on.

§port: u16

The port to serve on.

§workers: u16

The number of workers to run in parallel.

§keep_alive: u32

Keep-alive timeout in seconds or disabled if 0.

§read_timeout: u32

Number of seconds to wait without receiving data before closing a connection; disabled when None.

§write_timeout: u32

Number of seconds to wait without sending data before closing a connection; disabled when None.

§log_level: LoggingLevel

How much information to log.

§secret_key: Option<String>

The secret key.

§tls: Option<(String, String)>

TLS configuration (path to certificates file, path to private key file).

§limits: Limits

Size limits.

§extras: HashMap<String, Value>

Any extra parameters that aren’t part of Rocket’s config.

§root: Option<PathBuf>

The root directory of this config, if any.

Implementations§

source§

impl ConfigBuilder

source

pub fn new(environment: Environment) -> ConfigBuilder

Create a new ConfigBuilder instance using the default parameters from the given environment.

This method is typically called indirectly via Config::build().

§Example
use rocket::config::{Config, Environment};

let config = Config::build(Environment::Staging)
    .address("127.0.0.1")
    .port(700)
    .workers(12)
    .finalize();
source

pub fn address<A: Into<String>>(self, address: A) -> Self

Sets the address in the configuration being built.

§Example
use rocket::config::{Config, Environment};

let config = Config::build(Environment::Staging)
    .address("127.0.0.1")
    .unwrap();

assert_eq!(config.address.as_str(), "127.0.0.1");
source

pub fn port(self, port: u16) -> Self

Sets the port in the configuration being built.

§Example
use rocket::config::{Config, Environment};

let config = Config::build(Environment::Staging)
    .port(1329)
    .unwrap();

assert_eq!(config.port, 1329);
source

pub fn workers(self, workers: u16) -> Self

Sets workers in the configuration being built.

§Example
use rocket::config::{Config, Environment};

let config = Config::build(Environment::Staging)
    .workers(64)
    .unwrap();

assert_eq!(config.workers, 64);
source

pub fn keep_alive(self, timeout: u32) -> Self

Sets the keep-alive timeout to timeout seconds. If timeout is 0, keep-alive is disabled.

§Example
use rocket::config::{Config, Environment};

let config = Config::build(Environment::Staging)
    .keep_alive(10)
    .unwrap();

assert_eq!(config.keep_alive, Some(10));

let config = Config::build(Environment::Staging)
    .keep_alive(0)
    .unwrap();

assert_eq!(config.keep_alive, None);
source

pub fn read_timeout(self, timeout: u32) -> Self

Sets the read timeout to timeout seconds. If timeout is 0, read timeouts are disabled.

§Example
use rocket::config::{Config, Environment};

let config = Config::build(Environment::Staging)
    .read_timeout(10)
    .unwrap();

assert_eq!(config.read_timeout, Some(10));

let config = Config::build(Environment::Staging)
    .read_timeout(0)
    .unwrap();

assert_eq!(config.read_timeout, None);
source

pub fn write_timeout(self, timeout: u32) -> Self

Sets the write timeout to timeout seconds. If timeout is 0, write timeouts are disabled.

§Example
use rocket::config::{Config, Environment};

let config = Config::build(Environment::Staging)
    .write_timeout(10)
    .unwrap();

assert_eq!(config.write_timeout, Some(10));

let config = Config::build(Environment::Staging)
    .write_timeout(0)
    .unwrap();

assert_eq!(config.write_timeout, None);
source

pub fn log_level(self, log_level: LoggingLevel) -> Self

Sets the log_level in the configuration being built.

§Example
use rocket::config::{Config, Environment, LoggingLevel};

let config = Config::build(Environment::Staging)
    .log_level(LoggingLevel::Critical)
    .unwrap();

assert_eq!(config.log_level, LoggingLevel::Critical);
source

pub fn secret_key<K: Into<String>>(self, key: K) -> Self

Sets the secret_key in the configuration being built.

§Example
use rocket::config::{Config, Environment, LoggingLevel};

let key = "8Xui8SN4mI+7egV/9dlfYYLGQJeEx4+DwmSQLwDVXJg=";
let mut config = Config::build(Environment::Staging)
    .secret_key(key)
    .unwrap();
source

pub fn limits(self, limits: Limits) -> Self

Sets the limits in the configuration being built.

§Example
use rocket::config::{Config, Environment, Limits};

let mut config = Config::build(Environment::Staging)
    .limits(Limits::new().limit("json", 5 * (1 << 20)))
    .unwrap();
source

pub fn tls<C, K>(self, certs_path: C, key_path: K) -> Self
where C: Into<String>, K: Into<String>,

Sets the TLS configuration in the configuration being built.

Certificates are read from certs_path. The certificate chain must be in X.509 PEM format. The private key is read from key_path. The private key must be an RSA key in either PKCS#1 or PKCS#8 PEM format.

§Example
use rocket::config::{Config, Environment};

let mut config = Config::build(Environment::Staging)
    .tls("/path/to/certs.pem", "/path/to/key.pem")
    .unwrap();
source

pub fn environment(self, env: Environment) -> Self

Sets the environment in the configuration being built.

§Example
use rocket::config::{Config, Environment};

let config = Config::build(Environment::Staging)
    .environment(Environment::Production)
    .unwrap();

assert_eq!(config.environment, Environment::Production);
source

pub fn root<P: AsRef<Path>>(self, path: P) -> Self

Sets the root in the configuration being built.

§Example
use rocket::config::{Config, Environment};

let config = Config::build(Environment::Staging)
    .root("/my_app/dir")
    .unwrap();

assert_eq!(config.root().unwrap(), Path::new("/my_app/dir"));
source

pub fn extra<V: Into<Value>>(self, name: &str, value: V) -> Self

Adds an extra configuration parameter with name and value to the configuration being built. The value can be any type that implements Into<Value> including &str, String, Vec<V: Into<Value>>, HashMap<S: Into<String>, V: Into<Value>>, and most integer and float types.

§Example
use rocket::config::{Config, Environment};

let config = Config::build(Environment::Staging)
    .extra("pi", 3.14)
    .extra("custom_dir", "/a/b/c")
    .unwrap();

assert_eq!(config.get_float("pi"), Ok(3.14));
assert_eq!(config.get_str("custom_dir"), Ok("/a/b/c"));
source

pub fn finalize(self) -> Result<Config>

Return the Config structure that was being built by this builder.

§Errors

If the address or secret key fail to parse, returns a BadType error.

§Example
use rocket::config::{Config, Environment};

let config = Config::build(Environment::Staging)
    .address("127.0.0.1")
    .port(700)
    .workers(12)
    .keep_alive(0)
    .finalize();

assert!(config.is_ok());

let config = Config::build(Environment::Staging)
    .address("123.123.123.123.123 whoops!")
    .finalize();

assert!(config.is_err());
source

pub fn unwrap(self) -> Config

Return the Config structure that was being built by this builder.

§Panics

Panics if the supplied address, secret key, or TLS configuration fail to parse.

§Example
use rocket::config::{Config, Environment};

let config = Config::build(Environment::Staging)
    .address("127.0.0.1")
    .unwrap();

assert_eq!(config.address.as_str(), "127.0.0.1");
source

pub fn expect(self, msg: &str) -> Config

Returns the Config structure that was being built by this builder.

§Panics

Panics if the supplied address, secret key, or TLS configuration fail to parse. If a panic occurs, the error message msg is printed.

§Example
use rocket::config::{Config, Environment};

let config = Config::build(Environment::Staging)
    .address("127.0.0.1")
    .expect("the configuration is bad!");

assert_eq!(config.address.as_str(), "127.0.0.1");

Trait Implementations§

source§

impl Clone for ConfigBuilder

source§

fn clone(&self) -> ConfigBuilder

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T, I> AsResult<T, I> for T
where I: Input,

source§

fn as_result(self) -> Result<T, ParseErr<I>>

source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> IntoCollection<T> for T

§

fn into_collection<A>(self) -> SmallVec<A>
where A: Array<Item = T>,

Converts self into a collection.
§

fn mapped<U, F, A>(self, f: F) -> SmallVec<A>
where F: FnMut(T) -> U, A: Array<Item = U>,

source§

impl<T> Same for T

source§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> Typeable for T
where T: Any,

source§

fn get_type(&self) -> TypeId

Get the TypeId of this object.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V