refactor(arr): split api_state out of run

Both #195 and #214 added a builder call to the AppState chain, which pushed
run past the too-many-lines limit. The chain grows a line per upstream the
API learns to talk to, so it gets its own function.
This commit is contained in:
Miguel Palhas
2026-08-25 01:50:17 +01:00
parent 999cb8b08c
commit f1a58c0810
+29 -15
View File
@@ -107,6 +107,34 @@ enum Error {
BackgroundTask(#[from] tokio::task::JoinError),
}
/// Everything the HTTP layer needs, assembled from config.
///
/// Split out of [`run`] because it grows a line per upstream the API learns
/// to talk to, and `run` is already at the too-many-lines limit.
fn api_state(
config: &Config,
database: &Db,
jellyfin: arr_api::jellyfin::JellyfinClient,
) -> Result<AppState, Error> {
let mut upstreams =
Upstreams::new(config.prowlarr_url.clone(), config.transmission_url.clone())
.with_prowlarr_api_key(config.prowlarr_api_key.clone())
.with_tmdb_api_key(config.tmdb_api_key.clone());
if let Some(tmdb_url) = config.tmdb_url.clone() {
upstreams = upstreams.with_tmdb_url(tmdb_url);
}
Ok(AppState::new(upstreams)?
.with_database(database.clone())
.with_subtitle_providers(subtitle_providers(
config.opensubtitles_api_key.clone(),
config.opensubtitles_username.clone(),
config.opensubtitles_password.clone(),
))
.with_jellyfin(jellyfin)
.with_syncer(arr_subs::Syncer::new().with_binary(config.alass_path.clone())))
}
async fn run() -> Result<(), Error> {
let config = Config::load()?;
let database = Db::connect(&config.database_path).await?;
@@ -138,21 +166,7 @@ async fn run() -> Result<(), Error> {
compat = compat.with_tmdb(tmdb);
}
let mut upstreams = Upstreams::new(config.prowlarr_url, config.transmission_url)
.with_prowlarr_api_key(config.prowlarr_api_key)
.with_tmdb_api_key(config.tmdb_api_key);
if let Some(tmdb_url) = config.tmdb_url {
upstreams = upstreams.with_tmdb_url(tmdb_url);
}
let state = AppState::new(upstreams)?
.with_database(database.clone())
.with_subtitle_providers(subtitle_providers(
config.opensubtitles_api_key,
config.opensubtitles_username,
config.opensubtitles_password,
))
.with_jellyfin(api_jellyfin)
.with_syncer(arr_subs::Syncer::new().with_binary(config.alass_path.clone()));
let state = api_state(&config, &database, api_jellyfin)?;
let app = arr_api::router(state.clone())
.merge(arr_compat::router(compat))