pub mod delegated_http_routing;
pub mod http_gateway_routing;
pub mod libp2p_routing;
use async_trait::async_trait;
use cid::Cid;
use helia_interface::Helia;
use libp2p::PeerId;
use std::sync::Arc;
#[derive(Debug, thiserror::Error)]
pub enum RoutingError {
#[error("Content not found: {0}")]
ContentNotFound(Cid),
#[error("Peer not found: {0}")]
PeerNotFound(PeerId),
#[error("Routing failed: {0}")]
RoutingFailed(String),
#[error("Timeout")]
Timeout,
}
#[derive(Debug, Clone)]
pub struct ProviderInfo {
pub peer_id: PeerId,
pub addrs: Vec<libp2p::Multiaddr>,
}
#[derive(Debug, Clone)]
pub struct PeerInfo {
pub peer_id: PeerId,
pub addrs: Vec<libp2p::Multiaddr>,
pub protocols: Vec<String>,
}
#[async_trait]
pub trait ContentRouting: Send + Sync {
async fn find_providers(&self, cid: &Cid) -> Result<Vec<ProviderInfo>, RoutingError>;
async fn provide(&self, cid: &Cid) -> Result<(), RoutingError>;
}
#[async_trait]
pub trait PeerRouting: Send + Sync {
async fn find_peer(&self, peer_id: &PeerId) -> Result<PeerInfo, RoutingError>;
}
pub struct Routers {
_helia: Arc<dyn Helia>,
}
impl Routers {
pub fn new(helia: Arc<dyn Helia>) -> Self {
Self { _helia: helia }
}
}
#[async_trait]
impl ContentRouting for Routers {
async fn find_providers(&self, _cid: &Cid) -> Result<Vec<ProviderInfo>, RoutingError> {
Ok(vec![])
}
async fn provide(&self, _cid: &Cid) -> Result<(), RoutingError> {
Ok(())
}
}
#[async_trait]
impl PeerRouting for Routers {
async fn find_peer(&self, peer_id: &PeerId) -> Result<PeerInfo, RoutingError> {
Err(RoutingError::PeerNotFound(*peer_id))
}
}
pub fn routers(helia: Arc<dyn Helia>) -> Routers {
Routers::new(helia)
}
pub use libp2p_routing::{libp2p_routing, Libp2pRouting};
pub use http_gateway_routing::{http_gateway_routing, HTTPGatewayRouter, HTTPGatewayRoutingInit};