Simplify states a little in the server

Use the sub-states directly, as suggested by axum's docs
This commit is contained in:
Aleksei Voronov 2023-10-02 16:44:03 +02:00
parent 4a08a283d2
commit 6bc2dc2a42
4 changed files with 42 additions and 17 deletions

View File

@ -1,22 +1,24 @@
use std::sync::Arc;
use atrium_api::app::bsky::feed::describe_feed_generator::{ use atrium_api::app::bsky::feed::describe_feed_generator::{
Feed, Output as FeedGeneratorDescription, Feed, Output as FeedGeneratorDescription,
}; };
use axum::{extract::State, Json}; use axum::{extract::State, Json};
use crate::processes::feed_server::state::FeedServerState; use crate::{algos::Algos, config::Config};
pub async fn describe_feed_generator( pub async fn describe_feed_generator(
State(state): State<FeedServerState>, State(config): State<Arc<Config>>,
State(algos): State<Arc<Algos>>,
) -> Json<FeedGeneratorDescription> { ) -> Json<FeedGeneratorDescription> {
Json(FeedGeneratorDescription { Json(FeedGeneratorDescription {
did: state.config.feed_generator_did.clone(), did: config.feed_generator_did.clone(),
feeds: state feeds: algos
.algos
.iter_names() .iter_names()
.map(|name| Feed { .map(|name| Feed {
uri: format!( uri: format!(
"at://{}/app.bsky.feed.generator/{}", "at://{}/app.bsky.feed.generator/{}",
state.config.publisher_did, name config.publisher_did, name
), ),
}) })
.collect(), .collect(),

View File

@ -1,7 +1,9 @@
use std::sync::Arc;
use axum::{extract::State, Json}; use axum::{extract::State, Json};
use serde::Serialize; use serde::Serialize;
use crate::processes::feed_server::state::FeedServerState; use crate::config::Config;
#[derive(Serialize)] #[derive(Serialize)]
pub struct Did { pub struct Did {
@ -19,14 +21,14 @@ pub struct Service {
service_endpoint: String, service_endpoint: String,
} }
pub async fn did_json(State(state): State<FeedServerState>) -> Json<Did> { pub async fn did_json(State(config): State<Arc<Config>>) -> Json<Did> {
Json(Did { Json(Did {
context: vec!["https://www.w3.org/ns/did/v1".to_owned()], 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 { service: vec![Service {
id: "#bsky_fg".to_owned(), id: "#bsky_fg".to_owned(),
type_: "BskyFeedGenerator".to_owned(), type_: "BskyFeedGenerator".to_owned(),
service_endpoint: format!("https://{}", state.config.feed_generator_hostname), service_endpoint: format!("https://{}", config.feed_generator_hostname),
}], }],
}) })
} }

View File

@ -1,3 +1,5 @@
use std::sync::Arc;
use anyhow::anyhow; use anyhow::anyhow;
use atrium_api::app::bsky::feed::defs::SkeletonFeedPost; use atrium_api::app::bsky::feed::defs::SkeletonFeedPost;
use atrium_api::app::bsky::feed::get_feed_skeleton::{ use atrium_api::app::bsky::feed::get_feed_skeleton::{
@ -7,24 +9,23 @@ use axum::extract::{Query, State};
use axum::Json; use axum::Json;
use chrono::{DateTime, TimeZone, Utc}; use chrono::{DateTime, TimeZone, Utc};
use crate::algos::Algos;
use crate::processes::feed_server::errors::AppError; use crate::processes::feed_server::errors::AppError;
use crate::processes::feed_server::state::FeedServerState; use crate::services::Database;
pub async fn get_feed_skeleton( pub async fn get_feed_skeleton(
State(state): State<FeedServerState>, State(algos): State<Arc<Algos>>,
State(database): State<Arc<Database>>,
query: Query<FeedSkeletonQuery>, query: Query<FeedSkeletonQuery>,
) -> Result<Json<FeedSkeleton>, AppError> { ) -> Result<Json<FeedSkeleton>, AppError> {
let algo = state let algo = algos
.algos
.get_by_name(&query.feed) .get_by_name(&query.feed)
.ok_or_else(|| AppError::FeedNotFound(query.feed.clone()))?; .ok_or_else(|| AppError::FeedNotFound(query.feed.clone()))?;
let limit = query.limit.unwrap_or(20); let limit = query.limit.unwrap_or(20);
let earlier_than = query.cursor.as_deref().map(parse_cursor).transpose()?; let earlier_than = query.cursor.as_deref().map(parse_cursor).transpose()?;
let posts = algo let posts = algo.fetch_posts(&database, limit, earlier_than).await?;
.fetch_posts(&state.database, limit, earlier_than)
.await?;
let feed = posts let feed = posts
.iter() .iter()

View File

@ -1,5 +1,7 @@
use std::sync::Arc; use std::sync::Arc;
use axum::extract::FromRef;
use crate::algos::Algos; use crate::algos::Algos;
use crate::config::Config; use crate::config::Config;
use crate::services::Database; use crate::services::Database;
@ -10,3 +12,21 @@ pub struct FeedServerState {
pub config: Arc<Config>, pub config: Arc<Config>,
pub algos: Arc<Algos>, pub algos: Arc<Algos>,
} }
impl FromRef<FeedServerState> for Arc<Database> {
fn from_ref(state: &FeedServerState) -> Arc<Database> {
state.database.clone()
}
}
impl FromRef<FeedServerState> for Arc<Config> {
fn from_ref(state: &FeedServerState) -> Arc<Config> {
state.config.clone()
}
}
impl FromRef<FeedServerState> for Arc<Algos> {
fn from_ref(state: &FeedServerState) -> Arc<Algos> {
state.algos.clone()
}
}