From ef97448b7271ab7686442ec1a529e8bb8da2041f Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Thu, 13 Jan 2022 19:47:19 -0600 Subject: [PATCH] web: context: Own the Config The `server::context::Context` struct now owns the `server::config::Config` struct. Consumers who need to access configuration properties can obtain a reference to the configuration using the `config()` method. This change will ultimately support the "service" structures, which will be constructed and owned by the context. They will need to reference the config as well. --- backend/src/server/context.rs | 21 ++++++++++++++------- backend/src/server/mod.rs | 11 ++--------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/backend/src/server/context.rs b/backend/src/server/context.rs index 8015a55..95973d5 100644 --- a/backend/src/server/context.rs +++ b/backend/src/server/context.rs @@ -1,23 +1,25 @@ +use super::config::Config; use crate::rpc::gen_client::Client; use crate::server::error::{ApiError, ErrorResponse}; use jsonrpc_core_client::transports::ipc::connect; use rocket::http::Status as HttpStatus; use rocket::serde::json::Json; -use std::path::PathBuf; pub struct Context { - socket_path: PathBuf, + config: Config, } impl Context { - pub fn new>(socket: P) -> Self { - Self { - socket_path: socket.into(), - } + /// Construct a new Context + /// + /// The context takes ownership of the configuration. To access it, + /// use [`Self::config()`]. + pub fn new(config: Config) -> Self { + Self { config } } pub async fn client(&self) -> Result { - match connect(&self.socket_path).await { + match connect(&self.config.socket).await { Ok(client) => Ok(client), Err(e) => Err(( HttpStatus::ServiceUnavailable, @@ -28,4 +30,9 @@ impl Context { )), } } + + /// Get a reference to the web server application configuration + pub fn config(&self) -> &Config { + &self.config + } } diff --git a/backend/src/server/mod.rs b/backend/src/server/mod.rs index a9937ba..e43bec6 100644 --- a/backend/src/server/mod.rs +++ b/backend/src/server/mod.rs @@ -7,7 +7,6 @@ use config::Config; use argh::FromArgs; use context::Context; use rocket; -use rocket::fairing::AdHoc; use rocket::figment::providers::{Env, Format, Serialized, Toml}; use rocket::figment::Figment; @@ -53,20 +52,14 @@ pub async fn main() -> Result<(), rocket::Error> { figment = figment.merge(("socket", socket)); } - let context = Context::new( - figment - .find_value("socket") - .expect("No daemon socket path configured") - .as_str() - .expect("Invalid daemon socket path"), - ); + let config: Config = figment.extract().unwrap(); + let context = Context::new(config); rocket::custom(figment) .mount( "/", rocket::routes![routes::status::get_status, routes::auth::login], ) - .attach(AdHoc::config::()) .manage(context) .ignite() .await?