relay/src/main.rs

47 lines
1.3 KiB
Rust

use anyhow::Result;
use std::{net::SocketAddr, str::FromStr, sync::Arc};
use tokio::sync::mpsc;
use tracing_subscriber::{fmt, prelude::*, EnvFilter};
use cerulea_relay::{
http::{self},
indexer::index_servers,
sequencer::start_sequencer,
RelayServer,
};
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::registry()
.with(fmt::layer())
.with(EnvFilter::from_str("cerulea_relay=debug").unwrap())
.init();
let db = sled::Config::default()
.path("data")
// TODO: configurable cache capacity
.cache_capacity(1024 * 1024 * 1024)
.use_compression(true)
.open()
.expect("Failed to open database");
let (event_tx, event_rx) = mpsc::channel(128);
let server = Arc::new(RelayServer::new(db, event_tx));
let hosts = server
.db
.get("hosts")?
.and_then(|v| serde_ipld_dagcbor::from_slice::<Vec<String>>(&v).ok())
.unwrap_or_default();
tracing::debug!(?hosts, "got list of hosts");
index_servers(Arc::clone(&server), &hosts);
start_sequencer(Arc::clone(&server), event_rx);
// TODO: configurable bind address
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
http::listen(server, addr).await?;
Ok(())
}