about summary refs log tree commit diff
path: root/src/media
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/media
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/media')
-rw-r--r--src/media/mod.rs26
1 files changed, 11 insertions, 15 deletions
diff --git a/src/media/mod.rs b/src/media/mod.rs
index 71f875e..47f456a 100644
--- a/src/media/mod.rs
+++ b/src/media/mod.rs
@@ -1,14 +1,9 @@
-use std::convert::TryFrom;
-
 use axum::{
-    extract::{Extension, Host, multipart::Multipart, Path},
-    response::{IntoResponse, Response},
-    headers::{Header, HeaderValue, IfNoneMatch, HeaderMapExt},
-    TypedHeader,
+    extract::{multipart::Multipart, FromRef, Host, Path, State}, headers::{HeaderMapExt, HeaderValue, IfNoneMatch}, response::{IntoResponse, Response}, TypedHeader
 };
 use kittybox_util::error::{MicropubError, ErrorType};
 use kittybox_indieauth::Scope;
-use crate::indieauth::{User, backend::AuthBackend};
+use crate::indieauth::{backend::AuthBackend, User};
 
 pub mod storage;
 use storage::{MediaStore, MediaStoreError, Metadata, ErrorKind};
@@ -25,7 +20,7 @@ impl From<MediaStoreError> for MicropubError {
 
 #[tracing::instrument(skip(blobstore))]
 pub(crate) async fn upload<S: MediaStore, A: AuthBackend>(
-    Extension(blobstore): Extension<S>,
+    State(blobstore): State<S>,
     user: User<A>,
     mut upload: Multipart
 ) -> Response {
@@ -70,7 +65,7 @@ pub(crate) async fn serve<S: MediaStore>(
     Host(host): Host,
     Path(path): Path<String>,
     if_none_match: Option<TypedHeader<IfNoneMatch>>,
-    Extension(blobstore): Extension<S>
+    State(blobstore): State<S>
 ) -> Response {
     use axum::http::StatusCode;
     tracing::debug!("Searching for file...");
@@ -131,11 +126,12 @@ pub(crate) async fn serve<S: MediaStore>(
     }
 }
 
-#[must_use]
-pub fn router<S: MediaStore, A: AuthBackend>(blobstore: S, auth: A) -> axum::Router {
+pub fn router<St, A, M>() -> axum::Router<St> where
+    A: AuthBackend + FromRef<St>,
+    M: MediaStore + FromRef<St>,
+    St: Clone + Send + Sync + 'static
+{
     axum::Router::new()
-        .route("/", axum::routing::post(upload::<S, A>))
-        .route("/uploads/*file", axum::routing::get(serve::<S>))
-        .layer(axum::Extension(blobstore))
-        .layer(axum::Extension(auth))
+        .route("/", axum::routing::post(upload::<M, A>))
+        .route("/uploads/*file", axum::routing::get(serve::<M>))
 }