use std::{hash::RandomState, sync::Arc};
use host_container::HostContainer;
use once_cell::sync::Lazy;
use papaya::HashMapRef;
use routes::RouteStore;
pub mod cache;
pub mod certificates;
pub mod global;
pub mod host_container;
pub mod lookup;
pub mod memory_store;
pub mod redis_store;
pub mod routes;
#[cfg(any(feature = "store-sqlite", feature = "store-postgres"))]
pub mod sql_store;
pub mod store_trait;
pub mod stored_route;
#[cfg(test)]
pub(crate) mod contract_tests;
pub use memory_store::MemoryStore;
pub use redis_store::RedisStore;
pub fn replica_id() -> &'static str {
static REPLICA_ID: std::sync::OnceLock<String> = std::sync::OnceLock::new();
REPLICA_ID.get_or_init(|| {
let suffix = uuid::Uuid::new_v4().simple().to_string();
format!("{}-{}", hostname(), suffix.get(..8).unwrap_or(&suffix))
})
}
pub fn hostname() -> String {
nix::unistd::gethostname()
.ok()
.and_then(|name| name.into_string().ok())
.unwrap_or_else(|| "proksi".to_string())
}
static ROUTE_STORE: Lazy<RouteStore> = Lazy::new(papaya::HashMap::new);
pub fn get_route_by_key(key: &str) -> Option<Arc<HostContainer>> {
ROUTE_STORE.pin().get(key).cloned()
}
pub fn get_routes(
) -> HashMapRef<'static, String, Arc<HostContainer>, RandomState, seize::OwnedGuard<'static>> {
ROUTE_STORE.pin_owned()
}
pub fn insert_route(key: String, value: Arc<HostContainer>) {
ROUTE_STORE.pin().insert(key, value);
}
pub fn remove_route(key: &str) {
ROUTE_STORE.pin().remove(key);
}
static CACHE_ROUTING_STORE: Lazy<cache::PathCacheStorage> = Lazy::new(papaya::HashMap::new);
pub fn get_cache_routing_by_key(key: &str) -> Option<String> {
CACHE_ROUTING_STORE.pin().get(key).cloned()
}
pub fn insert_cache_routing(key: &str, value: &str) {
let store = CACHE_ROUTING_STORE.pin();
if store.get(key).is_none() {
store.insert(key.to_string(), value.to_string());
}
}