about summary refs log tree commit diff
path: root/src/micropub
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2024-08-01 20:01:12 +0300
committerVika <vika@fireburn.ru>2024-08-01 20:40:30 +0300
commit4ca0c24b1989fcd12c453d428af70f58456f7651 (patch)
tree19f480107cc6491b832a7a2d7198cee48f205b85 /src/micropub
parent7e8e688e2e58f9c944b941e768ab7b034a348a1f (diff)
downloadkittybox-4ca0c24b1989fcd12c453d428af70f58456f7651.tar.zst
Migrate from axum::Extension to axum::extract::State
This somehow allowed me to shrink the construction phase of Kittybox
by a huge amount of code.
Diffstat (limited to 'src/micropub')
-rw-r--r--src/micropub/mod.rs38
1 files changed, 16 insertions, 22 deletions
diff --git a/src/micropub/mod.rs b/src/micropub/mod.rs
index 624c239..fc5dd10 100644
--- a/src/micropub/mod.rs
+++ b/src/micropub/mod.rs
@@ -5,12 +5,13 @@ use std::sync::Arc;
 use crate::database::{MicropubChannel, Storage, StorageError};
 use crate::indieauth::backend::AuthBackend;
 use crate::indieauth::User;
+use crate::media::storage::MediaStore;
 use crate::micropub::util::form_to_mf2_json;
-use axum::extract::{BodyStream, Query, Host};
+use axum::extract::{BodyStream, FromRef, Host, Query, State};
 use axum::headers::ContentType;
 use axum::response::{IntoResponse, Response};
 use axum::TypedHeader;
-use axum::{http::StatusCode, Extension};
+use axum::http::StatusCode;
 use serde::{Deserialize, Serialize};
 use serde_json::json;
 use tokio::sync::Mutex;
@@ -515,9 +516,9 @@ async fn dispatch_body(
 
 #[tracing::instrument(skip(db, http))]
 pub(crate) async fn post<D: Storage + 'static, A: AuthBackend>(
-    Extension(db): Extension<D>,
-    Extension(http): Extension<reqwest::Client>,
-    Extension(jobset): Extension<Arc<Mutex<JoinSet<()>>>>,
+    State(db): State<D>,
+    State(http): State<reqwest::Client>,
+    State(jobset): State<Arc<Mutex<JoinSet<()>>>>,
     TypedHeader(content_type): TypedHeader<ContentType>,
     user: User<A>,
     body: BodyStream,
@@ -540,7 +541,7 @@ pub(crate) async fn post<D: Storage + 'static, A: AuthBackend>(
 
 #[tracing::instrument(skip(db))]
 pub(crate) async fn query<D: Storage, A: AuthBackend>(
-    Extension(db): Extension<D>,
+    State(db): State<D>,
     query: Option<Query<MicropubQuery>>,
     Host(host): Host,
     user: User<A>,
@@ -662,16 +663,13 @@ pub(crate) async fn query<D: Storage, A: AuthBackend>(
     }
 }
 
-#[must_use]
-pub fn router<S, A>(
-    storage: S,
-    http: reqwest::Client,
-    auth: A,
-    jobset: Arc<Mutex<JoinSet<()>>>
-) -> axum::routing::MethodRouter
+
+pub fn router<A, S, St: Send + Sync + Clone + 'static>() -> axum::routing::MethodRouter<St>
 where
-    S: Storage + 'static,
-    A: AuthBackend
+    S: Storage + FromRef<St> + 'static,
+    A: AuthBackend + FromRef<St>,
+    reqwest::Client: FromRef<St>,
+    Arc<Mutex<JoinSet<()>>>: FromRef<St>
 {
     axum::routing::get(query::<S, A>)
         .post(post::<S, A>)
@@ -680,11 +678,7 @@ where
                    axum::http::Method::GET,
                    axum::http::Method::POST,
                ])
-               .allow_origin(tower_http::cors::Any))
-        .layer::<_, _, std::convert::Infallible>(axum::Extension(storage))
-        .layer::<_, _, std::convert::Infallible>(axum::Extension(http))
-        .layer::<_, _, std::convert::Infallible>(axum::Extension(auth))
-        .layer::<_, _, std::convert::Infallible>(axum::Extension(jobset))
+            .allow_origin(tower_http::cors::Any))
 }
 
 #[cfg(test)]
@@ -716,7 +710,7 @@ mod tests {
 
     use super::FetchedPostContext;
     use kittybox_indieauth::{Scopes, Scope, TokenData};
-    use axum::extract::Host;
+    use axum::extract::{Host, State};
 
     #[test]
     fn test_populate_reply_context() {
@@ -850,7 +844,7 @@ mod tests {
     #[tokio::test]
     async fn test_query_foreign_url() {
         let mut res = super::query(
-            axum::Extension(crate::database::MemoryStorage::default()),
+            State(crate::database::MemoryStorage::default()),
             Some(axum::extract::Query(super::MicropubQuery::source(
                 "https://aaronparecki.com/feeds/main",
             ))),