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 {
pub fn new(settings: Settings) -> Self {
Self {
client: Client::new(),
settings,
}
pub fn new(settings: Settings, client: Option<Client>) -> Self {
let client = if let Some(client) = client {
client
} else {
Client::new()
};
Self { client, settings }
}
async fn tx(&self, e: &EventType) -> ServiceResult<()> {
for c in self.settings.conductors.iter() {
@ -79,7 +81,7 @@ mod tests {
#[actix_rt::test]
pub async fn test_conductor() {
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();
let page = Page {
secret: "foo".into(),

View File

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