use std::sync::Arc;
use osproxy_config::Config;
use osproxy_core::SystemClock;
use osproxy_observe::{DirectiveStore, InMemoryDirectiveStore};
use osproxy_server::handler::AppHandler;
pub(crate) fn with_directive_admin<A: osproxy_spi::Authenticator>(
handler: AppHandler<A>,
store: Option<Arc<InMemoryDirectiveStore>>,
token: Option<&str>,
) -> AppHandler<A> {
let (Some(store), Some(token)) = (store, token) else {
return handler;
};
println!("osproxy fleet directive admin: on (POST /admin/directives)");
handler.with_directive_admin(store, token.to_owned(), Arc::new(SystemClock))
}
#[allow(clippy::unused_async)] pub(crate) async fn directive_store(
cfg: &Config,
) -> Result<(Arc<dyn DirectiveStore>, Option<Arc<InMemoryDirectiveStore>>), String> {
let Some(etcd) = &cfg.etcd else {
let store = Arc::new(InMemoryDirectiveStore::new());
return Ok((store.clone(), Some(store)));
};
#[cfg(feature = "etcd")]
{
let store = osproxy_etcd::EtcdDirectiveStore::connect(
&etcd.endpoints,
etcd.directives_key.clone(),
Arc::new(SystemClock),
)
.await
.map_err(|e| format!("etcd directive store: {e}"))?;
println!(
"osproxy fleet directive store: etcd ({}, key '{}')",
etcd.endpoints.join(","),
etcd.directives_key
);
let read: Arc<dyn DirectiveStore> = Arc::new(store);
Ok((read, None))
}
#[cfg(not(feature = "etcd"))]
{
let _ = etcd; Err(
"etcd directive store configured but this binary was built without \
the `etcd` feature"
.to_owned(),
)
}
}