A better Rust ATProto crate
at lifetimes 2.3 kB view raw
1use axum::{Router, response::IntoResponse}; 2use jacquard::{ 3 IntoStatic, 4 api::com_atproto::identity::resolve_did::{ResolveDid, ResolveDidOutput, ResolveDidRequest}, 5 identity::resolver::IdentityResolver, 6 types::value::Data, 7 xrpc::XrpcEndpoint, 8}; 9use jacquard_axum::ExtractXrpc; 10use jacquard_common::xrpc::XrpcMethod; 11use miette::{IntoDiagnostic, Result}; 12use tracing_subscriber::EnvFilter; 13 14trait IntoRouter { 15 fn into_router<T, S, U>(handler: U) -> Router<S> 16 where 17 T: 'static, 18 S: Clone + Send + Sync + 'static, 19 U: axum::handler::Handler<T, S>; 20} 21 22impl<X> IntoRouter for X 23where 24 X: XrpcEndpoint, 25{ 26 /// Creates an axum router that will invoke `handler` in response to xrpc 27 /// request `X`. 28 fn into_router<T, S, U>(handler: U) -> Router<S> 29 where 30 T: 'static, 31 S: Clone + Send + Sync + 'static, 32 U: axum::handler::Handler<T, S>, 33 { 34 Router::new().route( 35 X::PATH, 36 (match X::METHOD { 37 XrpcMethod::Query => axum::routing::get, 38 XrpcMethod::Procedure(_) => axum::routing::post, 39 })(handler), 40 ) 41 } 42} 43 44#[axum_macros::debug_handler] 45async fn handler(ExtractXrpc(args): ExtractXrpc<ResolveDidRequest>) -> &'static str { 46 "hello world!" 47 // let res = jacquard::identity::slingshot_resolver_default(); 48 // let doc = res.resolve_did_doc(&args.did).await?; 49 // let valid_doc = doc.parse()?; 50 // let doc_value = serde_json::to_value(valid_doc).unwrap(); 51 // Ok(ResolveDidOutput { 52 // did_doc: Data::from_json(&doc_value).unwrap().into_static(), 53 // extra_data: Default::default(), 54 // } 55 // .into()) 56} 57 58#[tokio::main] 59async fn main() -> Result<()> { 60 tracing_subscriber::fmt() 61 .with_timer(tracing_subscriber::fmt::time::UtcTime::rfc_3339()) 62 .with_env_filter(EnvFilter::from_env("QDPDS_LOG")) 63 .init(); 64 let app = Router::new() 65 .route("/", axum::routing::get(|| async { "hello world!" })) 66 .merge(ResolveDidRequest::into_router(handler)) 67 .layer(tower_http::trace::TraceLayer::new_for_http()); 68 let listener = tokio::net::TcpListener::bind("0.0.0.0:3000") 69 .await 70 .into_diagnostic()?; 71 axum::serve(listener, app).await.unwrap(); 72 Ok(()) 73}