Skip to content

Commit

Permalink
temporary workaround - Parts as arg to TCtxFn
Browse files Browse the repository at this point in the history
  • Loading branch information
oscartbeaumont committed Aug 26, 2024
1 parent b9e3fc6 commit 0cea6ef
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 44 deletions.
4 changes: 2 additions & 2 deletions examples/axum/src/main.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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(),
};
Expand Down
58 changes: 32 additions & 26 deletions integrations/axum/src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -38,7 +38,8 @@ impl<TCtx: Send + Sync + 'static> Endpoint<TCtx> {
pub fn new<S>(
router: BuiltRouter<TCtx>,
// 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<S>
where
S: Clone + Send + Sync + 'static,
Expand Down Expand Up @@ -118,9 +119,13 @@ impl<TCtx: Send + Sync + 'static> Endpoint<TCtx> {
}
}

// 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<S>(self, ctx_fn: impl Fn() -> TCtx + Send + Sync + 'static) -> axum::Router<S>
pub fn build<S>(
self,
ctx_fn: impl Fn(&Parts) -> TCtx + Send + Sync + 'static,
) -> axum::Router<S>
where
S: Clone + Send + Sync + 'static,
{
Expand All @@ -134,36 +139,37 @@ impl<TCtx: Send + Sync + 'static> Endpoint<TCtx> {
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<HashMap<String, String>>,
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<HashMap<String, String>>,
| 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
Expand All @@ -179,8 +185,8 @@ impl<TCtx: Send + Sync + 'static> Endpoint<TCtx> {
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!();
Expand Down
35 changes: 19 additions & 16 deletions middleware/openapi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<TCtx, S>(
router: BuiltRouter<TCtx>,
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<S>
where
S: Clone + Send + Sync + 'static,
Expand Down Expand Up @@ -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<HashMap<String, String>>| 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<HashMap<String, String>>| 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,
Expand Down

0 comments on commit 0cea6ef

Please sign in to comment.