#[allow(unused_imports)]
pub mod routes;
use crate::gateway::Gateway;
use axum::Router;
use std::net::SocketAddr;
use std::sync::Arc;
pub struct Server {
gateway: Arc<Gateway>,
addr: SocketAddr,
}
impl Server {
pub fn new(gateway: Gateway, host: &str, port: u16) -> Self {
let addr: SocketAddr = format!("{}:{}", host, port)
.parse()
.expect("Invalid address");
Self {
gateway: Arc::new(gateway),
addr,
}
}
pub fn build_router(&self) -> Router {
let channels = self.gateway.channels();
let http_channel = channels.http();
http_channel.build_router()
}
pub async fn start(self) -> anyhow::Result<()> {
let router = self.build_router();
tracing::info!("Starting server on {}", self.addr);
println!("Starting rsclaw gateway on {}", self.addr);
let listener = tokio::net::TcpListener::bind(self.addr).await?;
axum::serve(listener, router).await?;
Ok(())
}
pub fn addr(&self) -> SocketAddr {
self.addr
}
}