about summary refs log tree commit diff
path: root/src/frontend
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/frontend
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/frontend')
-rw-r--r--src/frontend/mod.rs7
-rw-r--r--src/frontend/onboarding.rs23
2 files changed, 14 insertions, 16 deletions
diff --git a/src/frontend/mod.rs b/src/frontend/mod.rs
index 0292171..42e8754 100644
--- a/src/frontend/mod.rs
+++ b/src/frontend/mod.rs
@@ -1,9 +1,8 @@
 use crate::database::{Storage, StorageError};
 use axum::{
-    extract::{Host, Path, Query},
+    extract::{Host, Query, State},
     http::{StatusCode, Uri},
     response::IntoResponse,
-    Extension,
 };
 use futures_util::FutureExt;
 use serde::Deserialize;
@@ -239,7 +238,7 @@ async fn get_post_from_database<S: Storage>(
 pub async fn homepage<D: Storage>(
     Host(host): Host,
     Query(query): Query<QueryParams>,
-    Extension(db): Extension<D>,
+    State(db): State<D>,
 ) -> impl IntoResponse {
     let user = None; // TODO authentication
     // This is stupid, but there is no other way.
@@ -333,7 +332,7 @@ pub async fn homepage<D: Storage>(
 
 #[tracing::instrument(skip(db))]
 pub async fn catchall<D: Storage>(
-    Extension(db): Extension<D>,
+    State(db): State<D>,
     Host(host): Host,
     Query(query): Query<QueryParams>,
     uri: Uri,
diff --git a/src/frontend/onboarding.rs b/src/frontend/onboarding.rs
index faf8cdd..9f3f36b 100644
--- a/src/frontend/onboarding.rs
+++ b/src/frontend/onboarding.rs
@@ -2,7 +2,7 @@ use std::sync::Arc;
 
 use crate::database::{settings, Storage};
 use axum::{
-    extract::{Extension, Host},
+    extract::{FromRef, Host, State},
     http::StatusCode,
     response::{Html, IntoResponse},
     Json,
@@ -131,10 +131,10 @@ async fn onboard<D: Storage + 'static>(
 }
 
 pub async fn post<D: Storage + 'static>(
-    Extension(db): Extension<D>,
+    State(db): State<D>,
     Host(host): Host,
-    Extension(http): Extension<reqwest::Client>,
-    Extension(jobset): Extension<Arc<Mutex<JoinSet<()>>>>,
+    State(http): State<reqwest::Client>,
+    State(jobset): State<Arc<Mutex<JoinSet<()>>>>,
     Json(data): Json<OnboardingData>,
 ) -> axum::response::Response {
     let user_uid = format!("https://{}/", host.as_str());
@@ -168,14 +168,13 @@ pub async fn post<D: Storage + 'static>(
     }
 }
 
-pub fn router<S: Storage + 'static>(
-    database: S,
-    http: reqwest::Client,
-    jobset: Arc<Mutex<JoinSet<()>>>,
-) -> axum::routing::MethodRouter {
+pub fn router<St, S>() -> axum::routing::MethodRouter<St>
+where
+    S: Storage + FromRef<St> + 'static,
+    Arc<Mutex<JoinSet<()>>>: FromRef<St>,
+    reqwest::Client: FromRef<St>,
+    St: Clone + Send + Sync + 'static,
+{
     axum::routing::get(get)
         .post(post::<S>)
-        .layer::<_, _, std::convert::Infallible>(axum::Extension(database))
-        .layer::<_, _, std::convert::Infallible>(axum::Extension(http))
-        .layer(axum::Extension(jobset))
 }