The trait bound problem I'm encountering
issue.rs
54 lines 1.8 kB view raw
1use axum::Router; 2use axum::extract::Query; 3use jacquard_api::com_atproto::identity::resolve_did::ResolveDid; 4use jacquard_common::types::xrpc::{XrpcMethod, XrpcRequest}; 5use miette::{IntoDiagnostic, Result}; 6use tracing_subscriber::EnvFilter; 7 8trait IntoRouter { 9 fn into_router<T, S, U>(handler: U) -> Router<S> 10 where 11 T: 'static, 12 S: Clone + Send + Sync + 'static, 13 U: axum::handler::Handler<T, S>; 14} 15 16impl<X: XrpcRequest> IntoRouter for X { 17 /// Creates an axum router that will invoke `handler` in response to xrpc 18 /// request `X`. 19 fn into_router<T, S, U>(handler: U) -> Router<S> 20 where 21 T: 'static, 22 S: Clone + Send + Sync + 'static, 23 U: axum::handler::Handler<T, S>, 24 { 25 Router::new().route( 26 // this should really be a compile time operation, but I cannot 27 // figure out how to make that happen 28 format!("/xrpc/{}", X::NSID).as_str(), 29 (match X::METHOD { 30 XrpcMethod::Query => axum::routing::get, 31 XrpcMethod::Procedure(_) => axum::routing::put, 32 })(handler), 33 ) 34 } 35} 36 37#[tokio::main] 38async fn main() -> Result<()> { 39 tracing_subscriber::fmt() 40 .with_timer(tracing_subscriber::fmt::time::UtcTime::rfc_3339()) 41 .with_env_filter(EnvFilter::from_env("QPDS_LOG")) 42 .init(); 43 let app = Router::new() 44 .route("/", axum::routing::get(|| async { "hello world!" })) 45 .merge(ResolveDid::into_router( 46 |Query(args): Query<ResolveDid>| async { "DID resolver goes here eventually!" }, 47 )) 48 .layer(tower_http::trace::TraceLayer::new_for_http()); 49 let listener = tokio::net::TcpListener::bind("0.0.0.0:3000") 50 .await 51 .into_diagnostic()?; 52 axum::serve(listener, app).await.unwrap(); 53 Ok(()) 54}