diff --git a/examples/axum/src/main.rs b/examples/axum/src/main.rs index 6ca69d66..10769d19 100644 --- a/examples/axum/src/main.rs +++ b/examples/axum/src/main.rs @@ -1,7 +1,7 @@ use std::net::Ipv6Addr; use api::invalidation; -use axum::{routing::get, Router}; +use axum::{http::request::Parts, routing::get, Router}; use tokio::sync::broadcast; use tracing::info; @@ -15,7 +15,7 @@ async fn main() { let chat_tx = broadcast::channel(100).0; let invalidation = invalidation::Ctx::new(); - let ctx_fn = move || api::Context { + let ctx_fn = move |_: &Parts| api::Context { chat: api::chat::Ctx::new(chat_tx.clone()), invalidation: invalidation.clone(), }; diff --git a/integrations/axum/src/endpoint.rs b/integrations/axum/src/endpoint.rs index d495c171..0cc03b8c 100644 --- a/integrations/axum/src/endpoint.rs +++ b/integrations/axum/src/endpoint.rs @@ -3,7 +3,7 @@ use std::{collections::HashMap, future::poll_fn, sync::Arc, task::Poll}; use axum::{ body::Bytes, extract::Query, - http::{header, HeaderMap, StatusCode}, + http::{header, request::Parts, HeaderMap, StatusCode}, routing::{get, post}, Json, }; @@ -38,7 +38,8 @@ impl Endpoint { pub fn new( router: BuiltRouter, // TODO: Parse this to `Self::build` -> It will make rustfmt result way nicer - ctx_fn: impl Fn() -> TCtx + Send + Sync + 'static, + // TODO: Make Axum extractors work + ctx_fn: impl Fn(&Parts) -> TCtx + Send + Sync + 'static, ) -> axum::Router where S: Clone + Send + Sync + 'static, @@ -118,9 +119,13 @@ impl Endpoint { } } + // TODO: Make Axum extractors work // TODO: Async or `Result` return type for context function /// Build an [`axum::Router`](axum::Router) with the configured features. - pub fn build(self, ctx_fn: impl Fn() -> TCtx + Send + Sync + 'static) -> axum::Router + pub fn build( + self, + ctx_fn: impl Fn(&Parts) -> TCtx + Send + Sync + 'static, + ) -> axum::Router where S: Clone + Send + Sync + 'static, { @@ -134,36 +139,37 @@ impl Endpoint { r = match procedure.kind() { ProcedureKind::Query => { r.route( - &format!("/{}", key), - // TODO: By moving `procedure` into the closure we hang onto the types for the duration of the program which is probs undesirable. - get( - move |query: Query>, - headers: HeaderMap| async move { - let ctx = (ctx_fn)(); + &format!("/{}", key), + // TODO: By moving `procedure` into the closure we hang onto the types for the duration of the program which is probs undesirable. + get( + move |parts: Parts, + query: Query>, + | async move { + let ctx = (ctx_fn)(&parts); - handle_procedure( - ctx, - &mut serde_json::Deserializer::from_str( - query.get("input").map(|v| &**v).unwrap_or("null"), - ), - headers, - procedure, - ) - .await - }, - ), - ) + handle_procedure( + ctx, + &mut serde_json::Deserializer::from_str( + query.get("input").map(|v| &**v).unwrap_or("null"), + ), + parts.headers, + procedure, + ) + .await + }, + ), + ) } ProcedureKind::Mutation => r.route( &format!("/{}", key), // TODO: By moving `procedure` into the closure we hang onto the types for the duration of the program which is probs undesirable. - post(move |headers: HeaderMap, body: Bytes| async move { - let ctx = (ctx_fn)(); + post(move |parts: Parts, body: Bytes| async move { + let ctx = (ctx_fn)(&parts); handle_procedure( ctx, &mut serde_json::Deserializer::from_slice(&body), - headers, + parts.headers, procedure, ) .await @@ -179,8 +185,8 @@ impl Endpoint { use axum::extract::ws::WebSocketUpgrade; r = r.route( "/ws", - get(move |ws: WebSocketUpgrade| async move { - let ctx = (ctx_fn)(); + get(move |parts: Parts, ws: WebSocketUpgrade| async move { + let ctx = (ctx_fn)(&parts); ws.on_upgrade(move |socket| async move { todo!(); diff --git a/middleware/openapi/src/lib.rs b/middleware/openapi/src/lib.rs index a5d877cb..e047367b 100644 --- a/middleware/openapi/src/lib.rs +++ b/middleware/openapi/src/lib.rs @@ -10,7 +10,7 @@ use std::{borrow::Cow, collections::HashMap, sync::Arc}; use axum::{ body::Bytes, extract::Query, - http::StatusCode, + http::{request::Parts, StatusCode}, response::Html, routing::{get, post}, Json, @@ -105,7 +105,8 @@ struct OpenAPIState(HashMap<(&'static str, Cow<'static, str>), String>); // TODO: Can we decouple webserver from OpenAPI while keeping something maintainable???? pub fn mount( router: BuiltRouter, - ctx_fn: impl Fn() -> TCtx + Clone + Send + Sync + 'static, + // TODO: Make Axum extractors work + ctx_fn: impl Fn(&Parts) -> TCtx + Clone + Send + Sync + 'static, ) -> axum::Router where S: Clone + Send + Sync + 'static, @@ -133,23 +134,25 @@ where match *method { "GET" => { // TODO: By moving `procedure` into the closure we hang onto the types for the duration of the program which is probs undesirable. - get(move |query: Query>| async move { - let ctx = (ctx_fn)(); - - handle_procedure( - ctx, - &mut serde_json::Deserializer::from_str( - query.get("input").map(|v| &**v).unwrap_or("null"), - ), - procedure, - ) - .await - }) + get( + move |parts: Parts, query: Query>| async move { + let ctx = (ctx_fn)(&parts); + + handle_procedure( + ctx, + &mut serde_json::Deserializer::from_str( + query.get("input").map(|v| &**v).unwrap_or("null"), + ), + procedure, + ) + .await + }, + ) } "POST" => { // TODO: By moving `procedure` into the closure we hang onto the types for the duration of the program which is probs undesirable. - post(move |body: Bytes| async move { - let ctx = (ctx_fn)(); + post(move |parts: Parts, body: Bytes| async move { + let ctx = (ctx_fn)(&parts); handle_procedure( ctx,