Skip to content

Accessing the values of the default and custom configuration fields #2709

Discussion options

You must be logged in to vote

You absolutely should not do the following in a route handler:

    let custom_a: String = rocket::Config::figment().extract_inner("custom_a").unwrap_or(String::from("some default"));

Calling figment() is expensive as it reads all of the files and does all of deserialization.

Instead, use AdHoc::config() to extract the configuration in a typed fashion at launch time, then simply read it in a route:

use rocket::{State, fairing::AdHoc};

#[get("/")]
fn custom(config: &State<MyConfig>) -> String {
    config.custom_a.clone()
}

#[derive(Deserialize)]
struct MyConfig {
    custom_a: String,
}

#[launch]
fn rocket() -> _ {
    rocket::build()
        .mount("/", routes![custom])
        .attach(A…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@szabgab
Comment options

Answer selected by szabgab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants