···
1
-
use crate::{CachedRecord, Repo, error::ServerError};
1
+
use crate::{CachedRecord, Identity, Repo, error::ServerError};
2
+
use atrium_api::types::string::{Cid, Did, Handle, Nsid, RecordKey};
5
+
use std::str::FromStr;
use tokio_util::sync::CancellationToken;
···
message: "This record was deleted".to_string(),
49
+
type XrpcError = Json<XrpcErrorResponseObject>;
50
+
fn xrpc_error(error: impl AsRef<str>, message: impl AsRef<str>) -> XrpcError {
51
+
Json(XrpcErrorResponseObject {
52
+
error: error.as_ref().to_string(),
53
+
message: message.as_ref().to_string(),
fn bad_request_handler(err: poem::Error) -> GetRecordResponse {
···
/// also list `InvalidRequest`, `ExpiredToken`, and `InvalidToken`. Of
/// these, slingshot will only return `RecordNotFound` or `InvalidRequest`.
103
-
BadRequest(Json<XrpcErrorResponseObject>),
112
+
BadRequest(XrpcError),
113
+
/// Just using 500 for potentially upstream errors for now
114
+
#[oai(status = 500)]
115
+
ServerError(XrpcError),
cache: HybridCache<String, CachedRecord>,
120
+
identity: Identity,
···
Query(cid): Query<Option<String>>,
144
-
let at_uri = format!("at://{repo}/{collection}/{rkey}");
156
+
let did = match Did::new(repo.clone()) {
159
+
let Ok(handle) = Handle::new(repo) else {
160
+
return GetRecordResponse::BadRequest(xrpc_error(
162
+
"repo was not a valid DID or handle",
165
+
if let Ok(res) = self.identity.handle_to_did(handle).await {
166
+
if let Some(did) = res {
169
+
return GetRecordResponse::BadRequest(xrpc_error(
171
+
"Could not resolve handle repo to a DID",
175
+
return GetRecordResponse::ServerError(xrpc_error(
176
+
"ResolutionFailed",
177
+
"errored while trying to resolve handle to DID",
183
+
let Ok(collection) = Nsid::new(collection) else {
184
+
return GetRecordResponse::BadRequest(xrpc_error(
186
+
"invalid NSID for collection",
190
+
let Ok(rkey) = RecordKey::new(rkey) else {
191
+
return GetRecordResponse::BadRequest(xrpc_error("InvalidRequest", "invalid rkey"));
194
+
let cid: Option<Cid> = if let Some(cid) = cid {
195
+
let Ok(cid) = Cid::from_str(&cid) else {
196
+
return GetRecordResponse::BadRequest(xrpc_error("InvalidRequest", "invalid CID"));
203
+
let at_uri = format!("at://{}/{}/{}", &*did, &*collection, &*rkey);
···
let repo_api = self.repo.clone();
153
-
.get_record(repo, collection, rkey, cid)
212
+
.get_record(&did, &collection, &rkey, &cid)
.map_err(|e| foyer::Error::Other(Box::new(e)))
···
CachedRecord::Found(ref raw) => {
let (found_cid, raw_value) = raw.into();
166
-
let found_cid = found_cid.as_ref().to_string();
if cid.clone().map(|c| c != found_cid).unwrap_or(false) {
return GetRecordResponse::BadRequest(Json(XrpcErrorResponseObject {
error: "RecordNotFound".to_string(),
···
serde_json::from_str(raw_value.get()).expect("RawValue to be valid json");
GetRecordResponse::Ok(Json(FoundRecordResponseObject {
179
-
cid: Some(found_cid),
237
+
cid: Some(found_cid.as_ref().to_string()),
···
cache: HybridCache<String, CachedRecord>,
295
+
identity: Identity,
_shutdown: CancellationToken,
) -> Result<(), ServerError> {
let repo = Arc::new(repo);
243
-
OpenApiService::new(Xrpc { cache, repo }, "Slingshot", env!("CARGO_PKG_VERSION"))
244
-
.server("http://localhost:3000")
245
-
.url_prefix("/xrpc");
301
+
let api_service = OpenApiService::new(
308
+
env!("CARGO_PKG_VERSION"),
310
+
.server("http://localhost:3000")
311
+
.url_prefix("/xrpc");
let mut app = Route::new()
.nest("/", api_service.scalar())
···
.expect("alskfjalksdjf");
258
-
.at("/.well-known/did.json", get_did_doc(&host));
323
+
app = app.at("/.well-known/did.json", get_did_doc(&host));
let auto_cert = AutoCert::builder()
.directory_url(LETS_ENCRYPT_PRODUCTION)
···
async fn run<L>(listener: L, app: Route) -> Result<(), ServerError>
274
-
L: Listener + 'static
339
+
L: Listener + 'static,
278
-
.allow_method(Method::GET)
279
-
.allow_credentials(false))
345
+
.allow_methods([Method::GET])
346
+
.allow_credentials(false),