From 3c4eb66ca5f96b8cc3289aba6c34373df1dba64a Mon Sep 17 00:00:00 2001 From: Vika Date: Thu, 1 Aug 2024 20:10:21 +0300 Subject: Move Kittybox router composition into the library Since Kittybox router composition is entirely generic, we can move it into the library. I feel like I could also split database types into their own crates, too. --- src/lib.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 55 ++----------------------------------------------------- 2 files changed, 51 insertions(+), 53 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1cc01c2..2d15423 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -206,3 +206,52 @@ pub mod companion { ) } } + +async fn teapot_route() -> impl axum::response::IntoResponse { + use axum::http::{header, StatusCode}; + (StatusCode::IM_A_TEAPOT, [(header::CONTENT_TYPE, "text/plain")], "Sorry, can't brew coffee yet!") +} + +async fn health_check( + axum::extract::State(data): axum::extract::State, +) -> impl axum::response::IntoResponse +where + D: crate::database::Storage +{ + (axum::http::StatusCode::OK, std::borrow::Cow::Borrowed("OK")) +} + +pub async fn compose_kittybox() -> axum::Router +where +A: AuthBackend + 'static + FromRef, +S: Storage + 'static + FromRef, +M: MediaStore + 'static + FromRef, +Q: kittybox_util::queue::JobQueue + FromRef, +reqwest::Client: FromRef, +Arc>>: FromRef, +St: Clone + Send + Sync + 'static +{ + use axum::routing::get; + axum::Router::new() + .route("/", get(crate::frontend::homepage::)) + .fallback(get(crate::frontend::catchall::)) + .route("/.kittybox/micropub", crate::micropub::router::()) + .route("/.kittybox/onboarding", crate::frontend::onboarding::router::()) + .nest("/.kittybox/media", crate::media::router::()) + .merge(crate::indieauth::router::()) + .merge(crate::webmentions::router::()) + .route("/.kittybox/health", get(health_check::)) + .route( + "/.kittybox/static/:path", + axum::routing::get(crate::frontend::statics) + ) + .route("/.kittybox/coffee", get(teapot_route)) + .nest("/.kittybox/micropub/client", crate::companion::router::()) + .layer(tower_http::trace::TraceLayer::new_for_http()) + .layer(tower_http::catch_panic::CatchPanicLayer::new()) + .layer(tower_http::sensitive_headers::SetSensitiveHeadersLayer::new([ + axum::http::header::AUTHORIZATION, + axum::http::header::COOKIE, + axum::http::header::SET_COOKIE, + ])) +} diff --git a/src/main.rs b/src/main.rs index 90fa196..788e765 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,60 +1,9 @@ -use axum::extract::FromRef; -use kittybox::{database::Storage, indieauth::backend::AuthBackend, media::storage::MediaStore, webmentions::Webmention}; +use kittybox::{database::Storage, indieauth::backend::AuthBackend, media::storage::MediaStore, webmentions::Webmention, compose_kittybox}; use tokio::{sync::Mutex, task::JoinSet}; use std::{env, time::Duration, sync::Arc}; use tracing::error; -async fn teapot_route() -> impl axum::response::IntoResponse { - use axum::http::{header, StatusCode}; - (StatusCode::IM_A_TEAPOT, [(header::CONTENT_TYPE, "text/plain")], "Sorry, can't brew coffee yet!") -} - -async fn health_check( - axum::extract::State(data): axum::extract::State, -) -> impl axum::response::IntoResponse -where - D: kittybox::database::Storage -{ - (axum::http::StatusCode::OK, std::borrow::Cow::Borrowed("OK")) -} - - -async fn compose_stateful_kittybox() -> axum::Router -where -A: AuthBackend + 'static + FromRef, -S: Storage + 'static + FromRef, -M: MediaStore + 'static + FromRef, -Q: kittybox_util::queue::JobQueue + FromRef, -reqwest::Client: FromRef, -Arc>>: FromRef, -St: Clone + Send + Sync + 'static -{ - use axum::routing::get; - axum::Router::new() - .route("/", get(kittybox::frontend::homepage::)) - .fallback(get(kittybox::frontend::catchall::)) - .route("/.kittybox/micropub", kittybox::micropub::router::()) - .route("/.kittybox/onboarding", kittybox::frontend::onboarding::router::()) - .nest("/.kittybox/media", kittybox::media::router::()) - .merge(kittybox::indieauth::router::()) - .merge(kittybox::webmentions::router::()) - .route("/.kittybox/health", get(health_check::)) - .route( - "/.kittybox/static/:path", - axum::routing::get(kittybox::frontend::statics) - ) - .route("/.kittybox/coffee", get(teapot_route)) - .nest("/.kittybox/micropub/client", kittybox::companion::router::()) - .layer(tower_http::trace::TraceLayer::new_for_http()) - .layer(tower_http::catch_panic::CatchPanicLayer::new()) - .layer(tower_http::sensitive_headers::SetSensitiveHeadersLayer::new([ - axum::http::header::AUTHORIZATION, - axum::http::header::COOKIE, - axum::http::header::SET_COOKIE, - ])) -} - #[tokio::main] async fn main() { use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Registry}; @@ -221,7 +170,7 @@ async fn main() { }; type St = kittybox::AppState; - let stateful_router = compose_stateful_kittybox::().await; + let stateful_router = compose_kittybox::().await; let task = kittybox::webmentions::supervised_webmentions_task::(&state, cancellation_token.clone()); let router = stateful_router.with_state(state); -- cgit 1.4.1