relay-legacy/src/request_crawl.rs

44 lines
1.3 KiB
Rust

use std::sync::Arc;
use anyhow::Result;
use atrium_api::com::atproto::sync::request_crawl;
use bytes::Buf;
use http_body_util::BodyExt;
use hyper::{body::Incoming, Request, Response};
use crate::{
http::{body_full, ServerResponse},
indexer::index_server,
RelayServer,
};
pub async fn handle_request_crawl(
server: Arc<RelayServer>,
req: Request<Incoming>,
) -> Result<ServerResponse> {
let body = req.collect().await?.aggregate();
let input = match serde_json::from_reader::<_, request_crawl::Input>(body.reader()) {
Ok(input) => input,
Err(_) => {
// TODO: surely we can build out an XRPC abstraction or something
return Ok(Response::builder()
.status(400)
.header("Content-Type", "application/json")
.body(body_full(
r#"{"error":"InvalidRequest","message":"Failed to parse request body"}"#,
))?);
}
};
let hostname = input.data.hostname;
tokio::task::spawn(async move {
if let Err(e) = index_server(server, hostname.clone()).await {
tracing::warn!(host = %hostname, "encountered error subscribing to PDS: {e:?}");
}
});
Ok(Response::builder()
.status(200)
.header("Content-Type", "application/json")
.body(body_full(r#"{"status":"ok"}"#))?)
}