chore: reuse app context's http client in conductor obj

This commit is contained in:
Aravinth Manivannan 2022-12-28 04:38:24 +05:30
parent 3d11bfdcfc
commit 8b19a8cac5
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
2 changed files with 13 additions and 7 deletions

View file

@ -30,11 +30,13 @@ pub struct Conductor {
} }
impl Conductor { impl Conductor {
pub fn new(settings: Settings) -> Self { pub fn new(settings: Settings, client: Option<Client>) -> Self {
Self { let client = if let Some(client) = client {
client: Client::new(), client
settings, } else {
} Client::new()
};
Self { client, settings }
} }
async fn tx(&self, e: &EventType) -> ServiceResult<()> { async fn tx(&self, e: &EventType) -> ServiceResult<()> {
for c in self.settings.conductors.iter() { for c in self.settings.conductors.iter() {
@ -79,7 +81,7 @@ mod tests {
#[actix_rt::test] #[actix_rt::test]
pub async fn test_conductor() { pub async fn test_conductor() {
let settings = Settings::new().unwrap(); let settings = Settings::new().unwrap();
let c = Conductor::new(settings.clone()); let c = Conductor::new(settings.clone(), None);
c.delete_site("example.org".into()).await.unwrap(); c.delete_site("example.org".into()).await.unwrap();
let page = Page { let page = Page {
secret: "foo".into(), secret: "foo".into(),

View file

@ -20,6 +20,7 @@ use std::thread;
use crate::db::*; use crate::db::*;
use crate::settings::Settings; use crate::settings::Settings;
use argon2_creds::{Config as ArgonConfig, ConfigBuilder as ArgonConfigBuilder, PasswordPolicy}; use argon2_creds::{Config as ArgonConfig, ConfigBuilder as ArgonConfigBuilder, PasswordPolicy};
use reqwest::Client;
use tracing::info; use tracing::info;
pub mod api; pub mod api;
@ -35,6 +36,7 @@ pub struct Ctx {
pub conductor: Conductor, pub conductor: Conductor,
/// credential-procession policy /// credential-procession policy
pub creds: ArgonConfig, pub creds: ArgonConfig,
client: Client,
} }
impl Ctx { impl Ctx {
@ -52,7 +54,8 @@ impl Ctx {
pub async fn new(settings: Settings) -> Arc<Self> { pub async fn new(settings: Settings) -> Arc<Self> {
let creds = Self::get_creds(); let creds = Self::get_creds();
let c = creds.clone(); let c = creds.clone();
let conductor = Conductor::new(settings.clone()); let client = Client::default();
let conductor = Conductor::new(settings.clone(), Some(client.clone()));
#[allow(unused_variables)] #[allow(unused_variables)]
let init = thread::spawn(move || { let init = thread::spawn(move || {
@ -67,6 +70,7 @@ impl Ctx {
Arc::new(Self { Arc::new(Self {
settings, settings,
client,
db, db,
creds, creds,
conductor, conductor,