about summary refs log tree commit diff
path: root/src/admin
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/admin
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/admin')
-rw-r--r--src/admin/mod.rs31
1 files changed, 18 insertions, 13 deletions
diff --git a/src/admin/mod.rs b/src/admin/mod.rs
index abc4515..7f8c9cf 100644
--- a/src/admin/mod.rs
+++ b/src/admin/mod.rs
@@ -10,9 +10,9 @@
 // prevent collisions with future well-known scopes).
 use std::collections::HashSet;
 
-use axum::extract::Host;
+use axum::extract::{State, Host};
 use axum::response::{Response, IntoResponse};
-use axum::{Extension, Form};
+use axum::Form;
 use axum_extra::extract::CookieJar;
 use hyper::StatusCode;
 
@@ -36,19 +36,19 @@ static SESSION_STORE: std::sync::LazyLock<tokio::sync::RwLock<HashSet<uuid::Uuid
 
 async fn set_name<D: Storage + 'static>(
     Host(host): Host,
-    Extension(db): Extension<D>,
+    State(db): State<D>,
     Form(NameChange { name }): Form<NameChange>
 ) -> Result<(), StorageError> {
     db.set_setting::<SiteName>(&host, name).await
 }
 
-async fn get_name<D: Storage + 'static>(Host(host): Host, Extension(db): Extension<D>) -> Result<String, StorageError> {
+async fn get_name<D: Storage + 'static>(Host(host): Host, State(db): State<D>) -> Result<String, StorageError> {
     db.get_setting::<SiteName>(&host).await.map(|name| name.as_ref().to_owned())
 }
 
 async fn change_password<A: AuthBackend>(
     Host(host): Host,
-    Extension(auth): Extension<A>,
+    State(auth): State<A>,
     Form(PasswordChange { old_password, new_password }): Form<PasswordChange>
 ) -> StatusCode {
     let website = url::Url::parse(&format!("https://{host}/")).unwrap();
@@ -84,8 +84,8 @@ impl axum::response::IntoResponse for StorageError {
 async fn dashboard<D: Storage + 'static, A: AuthBackend>(
     Host(host): Host,
     cookies: CookieJar,
-    Extension(db): Extension<D>,
-    Extension(auth): Extension<A>
+    State(db): State<D>,
+    State(auth): State<A>
 ) -> axum::response::Response {
 
     let page = kittybox_frontend_renderer::admin::AdminHome {};
@@ -94,21 +94,26 @@ async fn dashboard<D: Storage + 'static, A: AuthBackend>(
 }
 
 
-pub fn router<D: Storage + 'static, A: AuthBackend>(db: D, auth: A) -> axum::Router {
+pub fn router<St, A, S, M>() -> axum::Router<St>
+where
+    A: AuthBackend + FromRef<St> + 'static,
+    S: Storage + FromRef<St> + 'static,
+    M: MediaStore + FromRef<St> + 'static,
+    Q: crate::webmentions::JobQueue<crate::webmentions::Webmention> + FromRef<St> + 'static,
+    axum_extra::extract::cookie::Key: FromRef<St>
+{
     axum::Router::new()
         .nest("/.kittybox/admin", axum::Router::new()
             // routes go here
             .route(
                 "/",
-                axum::routing::get(dashboard::<D, A>)
+                axum::routing::get(dashboard::<S, A>)
             )
             .route(
                 "/api/settings/name",
-                axum::routing::post(set_name::<D>)
-                    .get(get_name::<D>)
+                axum::routing::post(set_name::<S>)
+                    .get(get_name::<S>)
             )
             .route("/api/settings/password", axum::routing::post(change_password::<A>))
-            .layer(Extension(db))
-            .layer(Extension(auth))
         )
 }