about summary refs log tree commit diff
path: root/src/webmentions
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/webmentions
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/webmentions')
-rw-r--r--src/webmentions/mod.rs39
1 files changed, 17 insertions, 22 deletions
diff --git a/src/webmentions/mod.rs b/src/webmentions/mod.rs
index 3e9b094..d5a617e 100644
--- a/src/webmentions/mod.rs
+++ b/src/webmentions/mod.rs
@@ -1,4 +1,4 @@
-use axum::{Form, response::{IntoResponse, Response}, Extension};
+use axum::{extract::{FromRef, State}, response::{IntoResponse, Response}, routing::post, Form};
 use axum::http::StatusCode;
 use tracing::error;
 
@@ -20,7 +20,7 @@ impl queue::PostgresJobItem for Webmention {
 }
 
 async fn accept_webmention<Q: JobQueue<Webmention>>(
-    Extension(queue): Extension<Q>,
+    State(queue): State<Q>,
     Form(webmention): Form<Webmention>,
 ) -> Response {
     if let Err(err) = webmention.source.parse::<url::Url>() {
@@ -31,27 +31,16 @@ async fn accept_webmention<Q: JobQueue<Webmention>>(
     }
 
     match queue.put(&webmention).await {
-        Ok(id) => StatusCode::ACCEPTED.into_response(),
+        Ok(_id) => StatusCode::ACCEPTED.into_response(),
         Err(err) => (StatusCode::INTERNAL_SERVER_ERROR, [
             ("Content-Type", "text/plain")
         ], err.to_string()).into_response()
     }
 }
 
-pub fn router<Q: JobQueue<Webmention>, S: Storage + 'static>(
-    queue: Q, db: S, http: reqwest::Client,
-    cancellation_token: tokio_util::sync::CancellationToken
-) -> (axum::Router, SupervisedTask) {
-    // Automatically spawn a background task to handle webmentions
-    let bgtask_handle = supervised_webmentions_task(queue.clone(), db, http, cancellation_token);
-
-    let router = axum::Router::new()
-        .route("/.kittybox/webmention",
-            axum::routing::post(accept_webmention::<Q>)
-        )
-        .layer(Extension(queue));
-
-    (router, bgtask_handle)
+pub fn router<St: Clone + Send + Sync + 'static, Q: JobQueue<Webmention> + FromRef<St>>() -> axum::Router<St> {
+    axum::Router::new()
+        .route("/.kittybox/webmention", post(accept_webmention::<Q>))
 }
 
 #[derive(thiserror::Error, Debug)]
@@ -184,10 +173,16 @@ async fn process_webmentions_from_queue<Q: JobQueue<Webmention>, S: Storage + 's
     unreachable!()
 }
 
-fn supervised_webmentions_task<Q: JobQueue<Webmention>, S: Storage + 'static>(
-    queue: Q, db: S,
-    http: reqwest::Client,
+pub fn supervised_webmentions_task<St: Send + Sync + 'static, S: Storage + FromRef<St> + 'static, Q: JobQueue<Webmention> + FromRef<St> + 'static>(
+    state: &St,
     cancellation_token: tokio_util::sync::CancellationToken
-) -> SupervisedTask {
-    supervisor::<Error<Q::Error>, _, _>(move || process_webmentions_from_queue(queue.clone(), db.clone(), http.clone()), cancellation_token)
+) -> SupervisedTask
+where reqwest::Client: FromRef<St>
+{
+    let queue = Q::from_ref(state);
+    let storage = S::from_ref(state);
+    let http = reqwest::Client::from_ref(state);
+    supervisor::<Error<Q::Error>, _, _>(move || process_webmentions_from_queue(
+        queue.clone(), storage.clone(), http.clone()
+    ), cancellation_token)
 }