use std::{
error::Error,
result::Result,
sync::mpsc::{Sender, channel},
};
mod config;
mod error;
mod files;
mod log_middleware;
mod server;
use actix_web::dev::ServerHandle;
pub use config::Config;
pub use server::read_config;
pub use server::server;
pub use server::server_internal;
use tracing::error;
#[cfg(test)]
mod tests;
pub struct Vacuna {
server: ServerHandle,
}
impl Vacuna {
pub fn start_background_from_file<P: AsRef<std::path::Path>>(path: P) -> Result<Self, Box<dyn Error>> {
let config = Config::read(path)?;
Self::start_background(config)
}
pub fn start_background_from_string_config(text: &str) -> Result<Self, Box<dyn Error>> {
let config = Config::from_str(text)?;
Self::start_background(config)
}
pub fn start_background(config: Config) -> Result<Self, Box<dyn Error>> {
let (started_tx, started_rx) = channel();
std::thread::spawn(move || {
actix_rt::System::new().block_on(async move {
if let Err(e) = Self::background_start(config, started_tx).await {
error!("fail background start {}", e)
}
});
});
let handle = started_rx.recv()?;
Ok(Self { server: handle })
}
async fn background_start(config: Config, started_tx: Sender<ServerHandle>) -> Result<(), Box<dyn Error>> {
let server = server(config)?;
started_tx.send(server.handle())?;
server.await?;
Ok(())
}
pub async fn run_from_file<P: AsRef<std::path::Path>>(path: P) -> Result<(), Box<dyn Error>> {
let config = Config::read(path)?;
Self::run(config).await?;
Ok(())
}
pub async fn run(config: Config) -> Result<(), Box<dyn Error>> {
let server = server(config)?;
server.await?;
Ok(())
}
pub async fn stop(&self) {
self.server.stop(true).await;
}
}