From 4ca0c24b1989fcd12c453d428af70f58456f7651 Mon Sep 17 00:00:00 2001 From: Vika Date: Thu, 1 Aug 2024 20:01:12 +0300 Subject: 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. --- src/admin/mod.rs | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) (limited to 'src/admin/mod.rs') 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( Host(host): Host, - Extension(db): Extension, + State(db): State, Form(NameChange { name }): Form ) -> Result<(), StorageError> { db.set_setting::(&host, name).await } -async fn get_name(Host(host): Host, Extension(db): Extension) -> Result { +async fn get_name(Host(host): Host, State(db): State) -> Result { db.get_setting::(&host).await.map(|name| name.as_ref().to_owned()) } async fn change_password( Host(host): Host, - Extension(auth): Extension, + State(auth): State, Form(PasswordChange { old_password, new_password }): Form ) -> StatusCode { let website = url::Url::parse(&format!("https://{host}/")).unwrap(); @@ -84,8 +84,8 @@ impl axum::response::IntoResponse for StorageError { async fn dashboard( Host(host): Host, cookies: CookieJar, - Extension(db): Extension, - Extension(auth): Extension + State(db): State, + State(auth): State ) -> axum::response::Response { let page = kittybox_frontend_renderer::admin::AdminHome {}; @@ -94,21 +94,26 @@ async fn dashboard( } -pub fn router(db: D, auth: A) -> axum::Router { +pub fn router() -> axum::Router +where + A: AuthBackend + FromRef + 'static, + S: Storage + FromRef + 'static, + M: MediaStore + FromRef + 'static, + Q: crate::webmentions::JobQueue + FromRef + 'static, + axum_extra::extract::cookie::Key: FromRef +{ axum::Router::new() .nest("/.kittybox/admin", axum::Router::new() // routes go here .route( "/", - axum::routing::get(dashboard::) + axum::routing::get(dashboard::) ) .route( "/api/settings/name", - axum::routing::post(set_name::) - .get(get_name::) + axum::routing::post(set_name::) + .get(get_name::) ) .route("/api/settings/password", axum::routing::post(change_password::)) - .layer(Extension(db)) - .layer(Extension(auth)) ) } -- cgit 1.4.1