use crate::api_ws::api_ws_handlers::ws_handler;
use hanzo_messages::hanzo_utils::hanzo_logging::hanzo_log;
use hanzo_messages::hanzo_utils::hanzo_logging::HanzoLogLevel;
use hanzo_messages::hanzo_utils::hanzo_logging::HanzoLogOption;
use warp::{http::StatusCode, Filter, Rejection, Reply};
async fn handle_rejection(err: Rejection) -> Result<impl Reply, Rejection> {
if err.is_not_found() {
return Ok(warp::reply::with_status("Not Found", StatusCode::NOT_FOUND));
}
hanzo_log(
HanzoLogOption::WsAPI,
HanzoLogLevel::Error,
&format!("unhandled rejection: {:?}", err),
);
Ok(warp::reply::with_status(
"Internal Server Error",
StatusCode::INTERNAL_SERVER_ERROR,
))
}
pub fn ws_routes(
ws_address: std::net::SocketAddr,
) -> impl Filter<Extract = impl warp::Reply, Error = Rejection> + Clone {
tracing::info!("Setting up WebSocket routes");
let root_ws = warp::path::end().and(warp::ws()).map(move |ws| {
let ws: warp::ws::Ws = ws;
ws.on_upgrade(move |socket| ws_handler(socket, ws_address))
});
root_ws
.with(warp::cors().allow_any_origin())
.with(warp::log("websocket"))
.recover(handle_rejection)
}