From 6e70996f3fb3067e8006adade0ae20f73425fa98 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Wed, 5 Oct 2022 16:58:49 +0530 Subject: [PATCH] feat: load Conductor into SUMMARY Conductor type is received from configuration file. Type is defined in src/settings.rs. ctx::Ctx initializes Conductor object based on the type specified in the configuration. --- Cargo.lock | 32 ++++++++++++++++++++++++++++++++ Cargo.toml | 5 +++++ config/default.toml | 1 + src/ctx.rs | 16 +++++++++++++++- src/settings.rs | 9 +++++++++ 5 files changed, 62 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index e8fad3e..16b82db 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -240,6 +240,17 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" +[[package]] +name = "async-trait" +version = "0.1.57" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76464446b8bc32758d7e88ee1a804d9914cd9b1cb264c029899680b0be29826f" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "atoi" version = "1.0.0" @@ -399,8 +410,10 @@ dependencies = [ "config", "derive_builder", "derive_more", + "dummy_conductor", "futures-util", "lazy_static", + "libconductor", "log", "pretty_env_logger", "serde 1.0.145", @@ -623,6 +636,16 @@ dependencies = [ "dirs", ] +[[package]] +name = "dummy_conductor" +version = "0.1.0" +dependencies = [ + "async-trait", + "libconductor", + "serde 1.0.145", + "serde_json", +] + [[package]] name = "either" version = "1.8.0" @@ -969,6 +992,15 @@ version = "0.2.134" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "329c933548736bc49fd575ee68c89e8be4d260064184389a5b77517cddd99ffb" +[[package]] +name = "libconductor" +version = "0.1.0" +dependencies = [ + "async-trait", + "serde 1.0.145", + "serde_json", +] + [[package]] name = "linked-hash-map" version = "0.5.6" diff --git a/Cargo.toml b/Cargo.toml index 3b6a77b..d74deab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,11 @@ url = { version = "2.2.2", features = ["serde"]} serde_json = { version ="1", features = ["raw_value"]} clap = { vesrion = "3.2.20", features = ["derive"]} +[dependencies.libconductor] +path = "./env/libconductor" + +[dependencies.dummy_conductor] +path = "./env/dummy_conductor" [build-dependencies] serde_json = "1" diff --git a/config/default.toml b/config/default.toml index 8bee3f1..303de8a 100644 --- a/config/default.toml +++ b/config/default.toml @@ -1,5 +1,6 @@ debug = true source_code = "https://git.batsense.net/librepages/conductor" +conductor = "dummy" [server] # Please set a unique value, your mCaptcha instance's security depends on this being diff --git a/src/ctx.rs b/src/ctx.rs index 21e74ca..0064466 100644 --- a/src/ctx.rs +++ b/src/ctx.rs @@ -16,8 +16,10 @@ //! App data: database connections, etc. use std::sync::Arc; +use libconductor::Conductor; + //use crate::errors::ServiceResult; -use crate::settings::Settings; +use crate::settings::{ConductorType, Settings}; /// App data pub struct Ctx { @@ -26,6 +28,7 @@ pub struct Ctx { /// app settings pub settings: Settings, pub source_code: String, + pub conductor: Arc>, } impl Ctx { @@ -43,10 +46,21 @@ impl Ctx { base.into() }; + let conductor: Box = match s.conductor { + ConductorType::Dummy => { + use dummy_conductor::DummyConductor; + + Box::new(DummyConductor {}) + } + }; + + let conductor = Arc::new(conductor); + let data = Ctx { // db, settings: s.clone(), source_code, + conductor, }; Arc::new(data) diff --git a/src/settings.rs b/src/settings.rs index 0f6826c..2d4ef25 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -40,11 +40,20 @@ impl Server { } } +#[derive(Deserialize, Serialize, Display, Eq, PartialEq, Clone, Debug)] +#[serde(rename_all = "lowercase")] +pub enum ConductorType { + /// doesn't do anything + #[display(fmt = "dummy")] + Dummy, +} + #[derive(Debug, Clone, Deserialize)] pub struct Settings { pub debug: bool, pub server: Server, pub source_code: String, + pub conductor: ConductorType, } #[cfg(not(tarpaulin_include))]