Rircd/src/state.rs

52 lines
1.4 KiB
Rust

use eyre::Result;
use async_trait::async_trait;
use std::time::Instant;
use std::sync::Arc;
use tokio::sync::oneshot;
use crate::endpoint::Endpoint;
pub struct RircdState {
endpoints: Vec<Arc<dyn Endpoint>>,
//to determine program runtime
creation_timestamp: Option<Instant>,
plainlog: Option<()>,
sqlite_log: Option<()>,
postgres_log: Option<()>,
}
impl RircdState {
pub fn new() -> Result<Self> {
//TODO impl
Ok(RircdState {
endpoints: vec![],
creation_timestamp: Some(Instant::now()),
plainlog: None,
sqlite_log: None,
postgres_log: None,
})
}
//TODO impl
pub fn from_config(config: String) -> Result<Self> {
Self::new()
}
pub async fn run(mut self) -> Result<()> {
//try to start each endpoint
self.endpoints.iter().filter(|ep| ep.is_active()).for_each(|endpoint| {
let endpoint_name = endpoint.name();
let (success_status_send,success_status_recv) = oneshot::channel::<Result<()>>();
tokio::spawn(endpoint.listen(success_status_send));
//TODO color err msg
println!("starting {} | {}", endpoint_name,
match success_status_recv.await {
Ok(_) => format!("SUCCEEDED |"),
Err(e) => format!("FAILED| <{}>", e),
});
});
Ok(())
}
}