use std::sync::{Arc, Mutex}; use config::SharedAppConfig; use sonic_channel::SonicChannel; #[derive(Clone)] pub struct Context { pub search: Arc>, pub ingest: Arc>, } impl Context { pub fn new(config: SharedAppConfig) -> Option { let enabled = config.lock().search().search_active(); if enabled { let search = { let l = config.lock(); sonic_channel::SearchChannel::start( l.search().sonic_search_addr(), l.search().sonic_search_pass(), ) .unwrap_or_else(|e| panic!("Failed to connect to sonic search channel. {}", e)) }; let ingest = { let l = config.lock(); sonic_channel::IngestChannel::start( l.search().sonic_ingest_addr(), l.search().sonic_ingest_pass(), ) .unwrap_or_else(|e| panic!("Failed to connect to sonic ingest channel. {}", e)) }; Some(Self { search: Arc::new(Mutex::new(search)), ingest: Arc::new(Mutex::new(ingest)), }) } else { None } } }