#![allow(clippy::module_inception)]
#![allow(clippy::doc_overindented_list_items)]
#![allow(clippy::incompatible_msrv)]
use std::path::PathBuf;
#[cfg(test)]
pub(crate) mod test_support {
use std::sync::{Mutex, MutexGuard, OnceLock};
pub(crate) fn env_cache_lock() -> &'static Mutex<()> {
static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
LOCK.get_or_init(|| Mutex::new(()))
}
pub(crate) fn env_cache_lock_acquire() -> MutexGuard<'static, ()> {
env_cache_lock()
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
}
}
pub mod error;
pub mod agent;
pub mod commands;
pub use bamboo_server as server;
pub use bamboo_tools as tools;
pub use bamboo_infrastructure as core;
pub use agent::{Agent, AgentBuilder};
pub use bamboo_infrastructure as infrastructure;
pub use bamboo_infrastructure::config::ServerConfig;
pub use bamboo_infrastructure::Config;
pub use error::{BambooError, Result};
pub struct BambooServer {
config: bamboo_infrastructure::Config,
data_dir: PathBuf,
}
impl BambooServer {
pub fn new(config: bamboo_infrastructure::Config) -> Self {
Self {
config,
data_dir: bamboo_infrastructure::paths::bamboo_dir(),
}
}
pub fn new_with_data_dir(config: bamboo_infrastructure::Config, data_dir: PathBuf) -> Self {
Self { config, data_dir }
}
pub async fn start(self) -> Result<()> {
bamboo_infrastructure::paths::init_bamboo_dir(self.data_dir.clone());
let result = if self.config.server.static_dir.is_some() {
server::run_with_bind_and_static(
self.data_dir,
self.config.server.port,
&self.config.server.bind,
self.config.server.static_dir.clone(),
)
.await
} else if self.config.server.bind == "127.0.0.1" {
server::run(self.data_dir, self.config.server.port).await
} else {
server::run_with_bind(
self.data_dir,
self.config.server.port,
&self.config.server.bind,
)
.await
};
result.map_err(BambooError::HttpServer)
}
pub fn server_addr(&self) -> String {
self.config.server_addr()
}
}
pub struct BambooBuilder {
config: bamboo_infrastructure::Config,
data_dir: PathBuf,
}
impl BambooBuilder {
pub fn new() -> Self {
Self {
config: bamboo_infrastructure::Config::new(),
data_dir: bamboo_infrastructure::paths::bamboo_dir(),
}
}
pub fn port(mut self, port: u16) -> Self {
self.config.server.port = port;
self
}
pub fn bind(mut self, addr: impl Into<String>) -> Self {
self.config.server.bind = addr.into();
self
}
pub fn data_dir(mut self, dir: PathBuf) -> Self {
self.data_dir = dir;
self
}
pub fn static_dir(mut self, dir: PathBuf) -> Self {
self.config.server.static_dir = Some(dir);
self
}
pub fn workers(mut self, workers: usize) -> Self {
self.config.server.workers = workers;
self
}
pub fn build(self) -> Result<BambooServer> {
Ok(BambooServer::new_with_data_dir(self.config, self.data_dir))
}
}
impl Default for BambooBuilder {
fn default() -> Self {
Self::new()
}
}