From 6bc2dc2a42043707fa7adeb02b57a3cc3e4522db Mon Sep 17 00:00:00 2001 From: Aleksei Voronov Date: Mon, 2 Oct 2023 16:44:03 +0200 Subject: [PATCH] Simplify states a little in the server Use the sub-states directly, as suggested by axum's docs --- .../endpoints/describe_feed_generator.rs | 14 +++++++------ .../feed_server/endpoints/did_json.rs | 10 ++++++---- .../endpoints/get_feed_skeleton.rs | 15 +++++++------- src/processes/feed_server/state.rs | 20 +++++++++++++++++++ 4 files changed, 42 insertions(+), 17 deletions(-) diff --git a/src/processes/feed_server/endpoints/describe_feed_generator.rs b/src/processes/feed_server/endpoints/describe_feed_generator.rs index a00497a..fbbd3bf 100644 --- a/src/processes/feed_server/endpoints/describe_feed_generator.rs +++ b/src/processes/feed_server/endpoints/describe_feed_generator.rs @@ -1,22 +1,24 @@ +use std::sync::Arc; + use atrium_api::app::bsky::feed::describe_feed_generator::{ Feed, Output as FeedGeneratorDescription, }; use axum::{extract::State, Json}; -use crate::processes::feed_server::state::FeedServerState; +use crate::{algos::Algos, config::Config}; pub async fn describe_feed_generator( - State(state): State, + State(config): State>, + State(algos): State>, ) -> Json { Json(FeedGeneratorDescription { - did: state.config.feed_generator_did.clone(), - feeds: state - .algos + did: config.feed_generator_did.clone(), + feeds: algos .iter_names() .map(|name| Feed { uri: format!( "at://{}/app.bsky.feed.generator/{}", - state.config.publisher_did, name + config.publisher_did, name ), }) .collect(), diff --git a/src/processes/feed_server/endpoints/did_json.rs b/src/processes/feed_server/endpoints/did_json.rs index e14b3bd..c03858f 100644 --- a/src/processes/feed_server/endpoints/did_json.rs +++ b/src/processes/feed_server/endpoints/did_json.rs @@ -1,7 +1,9 @@ +use std::sync::Arc; + use axum::{extract::State, Json}; use serde::Serialize; -use crate::processes::feed_server::state::FeedServerState; +use crate::config::Config; #[derive(Serialize)] pub struct Did { @@ -19,14 +21,14 @@ pub struct Service { service_endpoint: String, } -pub async fn did_json(State(state): State) -> Json { +pub async fn did_json(State(config): State>) -> Json { Json(Did { context: vec!["https://www.w3.org/ns/did/v1".to_owned()], - id: state.config.feed_generator_did.clone(), + id: config.feed_generator_did.clone(), service: vec![Service { id: "#bsky_fg".to_owned(), type_: "BskyFeedGenerator".to_owned(), - service_endpoint: format!("https://{}", state.config.feed_generator_hostname), + service_endpoint: format!("https://{}", config.feed_generator_hostname), }], }) } diff --git a/src/processes/feed_server/endpoints/get_feed_skeleton.rs b/src/processes/feed_server/endpoints/get_feed_skeleton.rs index 2649a66..79daf1b 100644 --- a/src/processes/feed_server/endpoints/get_feed_skeleton.rs +++ b/src/processes/feed_server/endpoints/get_feed_skeleton.rs @@ -1,3 +1,5 @@ +use std::sync::Arc; + use anyhow::anyhow; use atrium_api::app::bsky::feed::defs::SkeletonFeedPost; use atrium_api::app::bsky::feed::get_feed_skeleton::{ @@ -7,24 +9,23 @@ use axum::extract::{Query, State}; use axum::Json; use chrono::{DateTime, TimeZone, Utc}; +use crate::algos::Algos; use crate::processes::feed_server::errors::AppError; -use crate::processes::feed_server::state::FeedServerState; +use crate::services::Database; pub async fn get_feed_skeleton( - State(state): State, + State(algos): State>, + State(database): State>, query: Query, ) -> Result, AppError> { - let algo = state - .algos + let algo = algos .get_by_name(&query.feed) .ok_or_else(|| AppError::FeedNotFound(query.feed.clone()))?; let limit = query.limit.unwrap_or(20); let earlier_than = query.cursor.as_deref().map(parse_cursor).transpose()?; - let posts = algo - .fetch_posts(&state.database, limit, earlier_than) - .await?; + let posts = algo.fetch_posts(&database, limit, earlier_than).await?; let feed = posts .iter() diff --git a/src/processes/feed_server/state.rs b/src/processes/feed_server/state.rs index 2cc8a44..4214a61 100644 --- a/src/processes/feed_server/state.rs +++ b/src/processes/feed_server/state.rs @@ -1,5 +1,7 @@ use std::sync::Arc; +use axum::extract::FromRef; + use crate::algos::Algos; use crate::config::Config; use crate::services::Database; @@ -10,3 +12,21 @@ pub struct FeedServerState { pub config: Arc, pub algos: Arc, } + +impl FromRef for Arc { + fn from_ref(state: &FeedServerState) -> Arc { + state.database.clone() + } +} + +impl FromRef for Arc { + fn from_ref(state: &FeedServerState) -> Arc { + state.config.clone() + } +} + +impl FromRef for Arc { + fn from_ref(state: &FeedServerState) -> Arc { + state.algos.clone() + } +}