From 58827da64740898a0cd3d98b614dae5ee9f09837 Mon Sep 17 00:00:00 2001 From: "naps62-yolo (agent)" Date: Sat, 22 Aug 2026 20:09:52 +0100 Subject: [PATCH] feat(api): axum skeleton with health and OpenAPI (#53) --- .gitea/workflows/ci.yml | 2 +- .gitignore | 2 + Cargo.lock | 224 ++++++++++++++++++++++++++++ Justfile | 18 ++- crates/arr-api/Cargo.toml | 11 ++ crates/arr-api/src/health.rs | 183 +++++++++++++++++++++++ crates/arr-api/src/lib.rs | 270 +++++++++++++++++++++++++++++++++- crates/arr-api/src/state.rs | 89 +++++++++++ crates/arr-daemon/Cargo.toml | 15 +- crates/arr-daemon/src/main.rs | 112 +++++++++++++- 10 files changed, 910 insertions(+), 16 deletions(-) create mode 100644 crates/arr-api/src/health.rs create mode 100644 crates/arr-api/src/state.rs diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index 8691b4c..8f8a202 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -73,7 +73,7 @@ jobs: - name: Skip when the frontend does not exist yet id: probe run: | - if [ -d web ]; then + if [ -f web/package.json ]; then echo "present=true" >> "$GITHUB_OUTPUT" else echo "present=false" >> "$GITHUB_OUTPUT" diff --git a/.gitignore b/.gitignore index 76e21cc..1884ec6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ /target +# Generated by `just gen-client`, never edited and never reviewed. +/web/src/api /web/node_modules /web/dist .env diff --git a/Cargo.lock b/Cargo.lock index 27fab92..8904f19 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -29,6 +29,17 @@ dependencies = [ [[package]] name = "arr-api" version = "0.1.0" +dependencies = [ + "axum", + "reqwest", + "serde", + "serde_json", + "tokio", + "utoipa", + "utoipa-axum", + "utoipa-scalar", + "wiremock", +] [[package]] name = "arr-compat" @@ -42,10 +53,17 @@ version = "0.1.0" name = "arr-daemon" version = "0.1.0" dependencies = [ + "arr-api", + "axum", + "reqwest", "serde", "tempfile", "thiserror", + "tokio", "toml", + "tower-http", + "tracing", + "tracing-subscriber", ] [[package]] @@ -125,6 +143,58 @@ version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" +[[package]] +name = "axum" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31b698c5f9a010f6573133b09e0de5408834d0c82f8d7475a89fc1867a71cd90" +dependencies = [ + "axum-core", + "bytes", + "form_urlencoded", + "futures-util", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-util", + "itoa", + "matchit", + "memchr", + "mime", + "percent-encoding", + "pin-project-lite", + "serde_core", + "serde_json", + "serde_path_to_error", + "serde_urlencoded", + "sync_wrapper", + "tokio", + "tower", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "axum-core" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08c78f31d7b1291f7ee735c1c6780ccde7785daae9a9206026862dab7d8792d1" +dependencies = [ + "bytes", + "futures-core", + "http", + "http-body", + "http-body-util", + "mime", + "pin-project-lite", + "sync_wrapper", + "tower-layer", + "tower-service", + "tracing", +] + [[package]] name = "base64" version = "0.22.1" @@ -904,6 +974,8 @@ checksum = "d466e9454f08e4a911e14806c24e16fba1b4c121d1ea474396f396069cf949d9" dependencies = [ "equivalent", "hashbrown 0.17.1", + "serde", + "serde_core", ] [[package]] @@ -1006,6 +1078,21 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "112b39cec0b298b6c1999fee3e31427f74f676e4cb9879ed1a121b43661a4154" +[[package]] +name = "matchers" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1525a2a28c7f4fa0fc98bb91ae755d1e2d1505079e05539e35bc876b5d65ae9" +dependencies = [ + "regex-automata", +] + +[[package]] +name = "matchit" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47e1ffaa40ddd1f3ed91f717a33c8c0ee23fff369e3aa8772b9605cc1d22f4c3" + [[package]] name = "md-5" version = "0.10.6" @@ -1022,6 +1109,12 @@ version = "2.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" +[[package]] +name = "mime" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" + [[package]] name = "mio" version = "1.2.2" @@ -1033,6 +1126,15 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "nu-ansi-term" +version = "0.50.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" +dependencies = [ + "windows-sys 0.61.2", +] + [[package]] name = "num-bigint-dig" version = "0.8.6" @@ -1123,6 +1225,12 @@ dependencies = [ "windows-link", ] +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + [[package]] name = "pem-rfc7468" version = "0.7.0" @@ -1565,6 +1673,17 @@ dependencies = [ "zmij", ] +[[package]] +name = "serde_path_to_error" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10a9ff822e371bb5403e391ecd83e182e0e77ba7f6fe0160b795797109d1b457" +dependencies = [ + "itoa", + "serde", + "serde_core", +] + [[package]] name = "serde_spanned" version = "0.6.9" @@ -1608,6 +1727,15 @@ dependencies = [ "digest", ] +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + [[package]] name = "shlex" version = "2.0.1" @@ -1968,6 +2096,15 @@ dependencies = [ "syn 3.0.3", ] +[[package]] +name = "thread_local" +version = "1.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ad99c4c6d32803332c548b1af0540b357b3f5fc0be8f6c6bfe8b2e6ae784070" +dependencies = [ + "cfg-if", +] + [[package]] name = "tinystr" version = "0.8.4" @@ -2109,6 +2246,7 @@ dependencies = [ "tokio", "tower-layer", "tower-service", + "tracing", ] [[package]] @@ -2126,6 +2264,7 @@ dependencies = [ "tower", "tower-layer", "tower-service", + "tracing", "url", ] @@ -2171,6 +2310,36 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a" dependencies = [ "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb7f578e5945fb242538965c2d0b04418d38ec25c79d160cd279bf0731c8d319" +dependencies = [ + "matchers", + "nu-ansi-term", + "once_cell", + "regex-automata", + "sharded-slab", + "smallvec", + "thread_local", + "tracing", + "tracing-core", + "tracing-log", ] [[package]] @@ -2236,6 +2405,61 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" +[[package]] +name = "utoipa" +version = "5.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8bde15df68e80b16c7d16b9616e80770ad158988daa56a27dccd1e55558b0160" +dependencies = [ + "indexmap", + "serde", + "serde_json", + "utoipa-gen", +] + +[[package]] +name = "utoipa-axum" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c25bae5bccc842449ec0c5ddc5cbb6a3a1eaeac4503895dc105a1138f8234a0" +dependencies = [ + "axum", + "paste", + "tower-layer", + "tower-service", + "utoipa", +] + +[[package]] +name = "utoipa-gen" +version = "5.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ba0b99ee52df3028635d93840c797102da61f8a7bb3cf751032455895b52ef8" +dependencies = [ + "proc-macro2", + "quote", + "regex", + "syn 2.0.119", +] + +[[package]] +name = "utoipa-scalar" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59559e1509172f6b26c1cdbc7247c4ddd1ac6560fe94b584f81ee489b141f719" +dependencies = [ + "axum", + "serde", + "serde_json", + "utoipa", +] + +[[package]] +name = "valuable" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65" + [[package]] name = "vcpkg" version = "0.2.15" diff --git a/Justfile b/Justfile index 95a436e..f8b2d6b 100644 --- a/Justfile +++ b/Justfile @@ -31,11 +31,13 @@ test: e2e: cargo nextest run -p arr-e2e -# Frontend lint and typecheck. No-op until web/ exists (issue #6). +# Frontend lint and typecheck. No-op until the SPA exists (issue #6). Probes +# for package.json, not the directory: `just gen-client` writes into web/ and +# must not turn the gate on before there is a project to check. web-check: #!/usr/bin/env bash set -euo pipefail - if [ ! -d web ]; then echo "web/ not present yet, skipping"; exit 0; fi + if [ ! -f web/package.json ]; then echo "web/ not present yet, skipping"; exit 0; fi biome ci web/ pnpm -C web install --frozen-lockfile pnpm -C web exec tsc -b --noEmit @@ -44,12 +46,22 @@ web-check: build: #!/usr/bin/env bash set -euo pipefail - if [ -d web ]; then + if [ -f web/package.json ]; then pnpm -C web install --frozen-lockfile pnpm -C web build fi cargo build --release +# Regenerate the TypeScript client from the OpenAPI document (DESIGN.md §9.1). +# The document is dumped from the binary, so nothing has to be running. Both +# outputs are generated: never edit them, and never hand-write the spec. +gen-client: + #!/usr/bin/env bash + set -euo pipefail + mkdir -p web/src/api + cargo run --quiet -p arr-daemon --bin arr -- --openapi > web/src/api/openapi.json + pnpm dlx openapi-typescript@7 web/src/api/openapi.json -o web/src/api/schema.d.ts + # Recreate the development database from an empty file. db-reset: #!/usr/bin/env bash diff --git a/crates/arr-api/Cargo.toml b/crates/arr-api/Cargo.toml index 984c7d2..eb05a31 100644 --- a/crates/arr-api/Cargo.toml +++ b/crates/arr-api/Cargo.toml @@ -7,6 +7,17 @@ repository.workspace = true publish = false [dependencies] +axum = { workspace = true } +reqwest = { workspace = true } +serde = { workspace = true } +serde_json = { workspace = true } +tokio = { workspace = true } +utoipa = { workspace = true } +utoipa-axum = { workspace = true } +utoipa-scalar = { workspace = true } + +[dev-dependencies] +wiremock = { workspace = true } [lints] workspace = true diff --git a/crates/arr-api/src/health.rs b/crates/arr-api/src/health.rs new file mode 100644 index 0000000..53832eb --- /dev/null +++ b/crates/arr-api/src/health.rs @@ -0,0 +1,183 @@ +//! `GET /api/health` — is each of the three upstreams answering. +//! +//! The three probed here are the ones DESIGN.md §9.5 calls "Broken": without +//! Prowlarr nothing is found, without Transmission nothing is fetched, and +//! without TMDB nothing is identified. The endpoint always answers `200` — +//! the body carries the verdict, so a degraded service can still explain +//! itself to the UI instead of looking like a fourth outage. + +use axum::extract::State; +use axum::Json; +use serde::Serialize; +use utoipa::ToSchema; + +use crate::state::AppState; + +/// Whether the service as a whole can do its job. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, ToSchema)] +#[serde(rename_all = "snake_case")] +pub enum Health { + /// Every upstream answered. + Ok, + /// At least one upstream is unreachable or unconfigured. + Degraded, +} + +/// The verdict for a single upstream. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, ToSchema)] +#[serde(rename_all = "snake_case")] +pub enum Status { + /// Answered as expected. + Ok, + /// Did not answer, or answered with an unexpected status. + Unreachable, + /// No API key configured, so it was not probed. + Unconfigured, +} + +/// One upstream's result. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, ToSchema)] +pub struct Check { + pub status: Status, + /// Why, when the status is not `ok`. Never contains the probed URL: it + /// can carry an API key in the query string. + #[serde(skip_serializing_if = "Option::is_none")] + pub detail: Option, +} + +impl Check { + fn ok() -> Self { + Self { + status: Status::Ok, + detail: None, + } + } + + fn unreachable(detail: impl Into) -> Self { + Self { + status: Status::Unreachable, + detail: Some(detail.into()), + } + } + + fn unconfigured(detail: impl Into) -> Self { + Self { + status: Status::Unconfigured, + detail: Some(detail.into()), + } + } +} + +/// The body of `GET /api/health`. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, ToSchema)] +pub struct HealthReport { + pub status: Health, + /// The running binary's version. + #[schema(example = "0.1.0")] + pub version: String, + pub prowlarr: Check, + pub transmission: Check, + pub tmdb: Check, +} + +/// Report reachability of Prowlarr, Transmission and TMDB. +#[utoipa::path( + get, + path = "/api/health", + tag = "system", + responses( + (status = 200, description = "Per-upstream reachability", body = HealthReport), + ), +)] +pub async fn health(State(state): State) -> Json { + // Three independent network probes; serialising them would make the + // endpoint as slow as the sum of the timeouts. + let (prowlarr, transmission, tmdb) = tokio::join!( + probe_prowlarr(&state), + probe_transmission(&state), + probe_tmdb(&state), + ); + + let status = if [prowlarr.status, transmission.status, tmdb.status] + .iter() + .all(|s| *s == Status::Ok) + { + Health::Ok + } else { + Health::Degraded + }; + + Json(HealthReport { + status, + version: env!("CARGO_PKG_VERSION").to_string(), + prowlarr, + transmission, + tmdb, + }) +} + +/// Prowlarr answers `/ping` without a key; the key is sent anyway so a +/// misconfigured one shows up here rather than at the first search. +async fn probe_prowlarr(state: &AppState) -> Check { + let url = format!( + "{}/ping", + state.upstreams().prowlarr_url.trim_end_matches('/') + ); + let mut request = state.http().get(url); + if let Some(key) = &state.upstreams().prowlarr_api_key { + request = request.header("X-Api-Key", key); + } + match request.send().await { + Ok(response) if response.status().is_success() => Check::ok(), + Ok(response) => Check::unreachable(format!("http {}", response.status().as_u16())), + Err(err) => Check::unreachable(describe(err)), + } +} + +/// Transmission answers an RPC call without a session id with `409` plus the +/// id to retry with. That is a live daemon, so it counts as reachable. +async fn probe_transmission(state: &AppState) -> Check { + let request = state + .http() + .post(&state.upstreams().transmission_url) + .json(&serde_json::json!({ "method": "session-get" })); + match request.send().await { + Ok(response) + if response.status().is_success() + || response.status() == reqwest::StatusCode::CONFLICT => + { + Check::ok() + } + Ok(response) => Check::unreachable(format!("http {}", response.status().as_u16())), + Err(err) => Check::unreachable(describe(err)), + } +} + +/// TMDB is the only upstream that cannot be probed at all without a key, so +/// a missing key is reported as its own state rather than as an outage. +async fn probe_tmdb(state: &AppState) -> Check { + let Some(key) = &state.upstreams().tmdb_api_key else { + return Check::unconfigured("no ARR_TMDB_API_KEY set"); + }; + let url = format!( + "{}/configuration", + state.upstreams().tmdb_url.trim_end_matches('/') + ); + match state + .http() + .get(url) + .query(&[("api_key", key)]) + .send() + .await + { + Ok(response) if response.status().is_success() => Check::ok(), + Ok(response) => Check::unreachable(format!("http {}", response.status().as_u16())), + Err(err) => Check::unreachable(describe(err)), + } +} + +/// `reqwest`'s own `Display` includes the URL, and the TMDB URL carries the +/// API key. `without_url` is what keeps the key out of the response body. +fn describe(err: reqwest::Error) -> String { + err.without_url().to_string() +} diff --git a/crates/arr-api/src/lib.rs b/crates/arr-api/src/lib.rs index 8b38675..bf1999d 100644 --- a/crates/arr-api/src/lib.rs +++ b/crates/arr-api/src/lib.rs @@ -1 +1,269 @@ -//! arr-api — see DESIGN.md. +//! arr-api — the HTTP surface. See DESIGN.md §9.1. +//! +//! The API is the product; the web UI is one client of it. So the `OpenAPI` +//! document is not written by hand and not kept in step by review: routes are +//! registered through [`utoipa_axum::routes`], which only accepts a handler +//! carrying a `#[utoipa::path]` annotation. A handler added without one fails +//! to compile, and the gate in DESIGN.md §12 fails with it. + +mod health; +mod state; + +use axum::routing::get; +use axum::{Json, Router}; +use utoipa::OpenApi; +use utoipa_axum::router::OpenApiRouter; +use utoipa_axum::routes; +use utoipa_scalar::{Scalar, Servable}; + +pub use health::{Check, Health, HealthReport, Status}; +pub use state::{AppState, Upstreams, DEFAULT_TMDB_URL}; + +/// Where the generated document is served, and where `just gen-client` reads +/// it back from when it is fetched rather than dumped from the binary. +pub const OPENAPI_PATH: &str = "/api/openapi.json"; + +/// Where the browsable UI lives. +pub const DOCS_PATH: &str = "/api/docs"; + +/// Document-level metadata. Paths and schemas are collected from the router, +/// never listed here — a list is a thing to forget to update. +#[derive(OpenApi)] +#[openapi( + info( + title = "arr", + description = "One service in place of Radarr and Sonarr. No authentication: \ + the perimeter is the VPN (DESIGN.md §2).", + ), + tags((name = "system", description = "Service health and metadata")), +)] +struct ApiDoc; + +/// Every annotated route, still needing state. +fn api_router() -> OpenApiRouter { + OpenApiRouter::with_openapi(ApiDoc::openapi()).routes(routes!(health::health)) +} + +/// The generated `OpenAPI` document. +#[must_use] +pub fn openapi() -> utoipa::openapi::OpenApi { + api_router().split_for_parts().1 +} + +/// The whole application: the API, the served document, and the browsable UI. +pub fn router(state: AppState) -> Router { + let (router, api) = api_router().split_for_parts(); + let document = api.clone(); + + router + .route( + OPENAPI_PATH, + get(move || { + let document = document.clone(); + async move { Json(document) } + }), + ) + .merge(Scalar::with_url(DOCS_PATH, api)) + .with_state(state) +} + +#[cfg(test)] +mod tests { + use super::*; + use wiremock::matchers::{method, path}; + use wiremock::{Mock, MockServer, ResponseTemplate}; + + /// A Prowlarr that answers `/ping`, and a Transmission that answers an + /// RPC call the way a real one does when it has no session id yet. + async fn upstreams_up() -> (MockServer, MockServer) { + let prowlarr = MockServer::start().await; + Mock::given(method("GET")) + .and(path("/ping")) + .respond_with( + ResponseTemplate::new(200).set_body_json(serde_json::json!({ "status": "OK" })), + ) + .mount(&prowlarr) + .await; + + let transmission = MockServer::start().await; + Mock::given(method("POST")) + .and(path("/transmission/rpc")) + .respond_with( + ResponseTemplate::new(409).insert_header("X-Transmission-Session-Id", "abc"), + ) + .mount(&transmission) + .await; + + (prowlarr, transmission) + } + + /// Serve the app on an ephemeral port and return its base URL. The server + /// task dies with the runtime at the end of the test. + async fn serve(state: AppState) -> String { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0") + .await + .expect("bind ephemeral port"); + let addr = listener.local_addr().expect("local addr"); + tokio::spawn(async move { + axum::serve(listener, router(state)).await.expect("serve"); + }); + format!("http://{addr}") + } + + async fn report(state: AppState) -> serde_json::Value { + let base = serve(state).await; + let response = reqwest::get(format!("{base}/api/health")) + .await + .expect("request health"); + assert_eq!(response.status(), 200, "health always answers 200"); + response.json().await.expect("health body is json") + } + + #[tokio::test] + async fn all_upstreams_up_is_ok() { + let (prowlarr, transmission) = upstreams_up().await; + let tmdb = MockServer::start().await; + Mock::given(method("GET")) + .and(path("/configuration")) + .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({}))) + .mount(&tmdb) + .await; + + let state = AppState::new( + Upstreams::new( + prowlarr.uri(), + format!("{}/transmission/rpc", transmission.uri()), + ) + .with_tmdb_url(tmdb.uri()) + .with_tmdb_api_key(Some("key".into())), + ) + .expect("state"); + + let body = report(state).await; + assert_eq!(body["status"], "ok"); + assert_eq!(body["prowlarr"]["status"], "ok"); + assert_eq!(body["transmission"]["status"], "ok"); + assert_eq!(body["tmdb"]["status"], "ok"); + assert_eq!(body["version"], env!("CARGO_PKG_VERSION")); + } + + #[tokio::test] + async fn a_missing_tmdb_key_is_unconfigured_not_an_outage() { + let (prowlarr, transmission) = upstreams_up().await; + let state = AppState::new(Upstreams::new( + prowlarr.uri(), + format!("{}/transmission/rpc", transmission.uri()), + )) + .expect("state"); + + let body = report(state).await; + assert_eq!(body["tmdb"]["status"], "unconfigured"); + assert_eq!(body["status"], "degraded"); + } + + #[tokio::test] + async fn an_unreachable_upstream_degrades_the_service() { + let (_prowlarr, transmission) = upstreams_up().await; + + // Port 1 is privileged and nothing binds it, so the probe gets a + // refused connection immediately instead of waiting out the timeout. + let state = AppState::new(Upstreams::new( + "http://127.0.0.1:1".into(), + format!("{}/transmission/rpc", transmission.uri()), + )) + .expect("state"); + + let body = report(state).await; + assert_eq!(body["status"], "degraded"); + assert_eq!(body["prowlarr"]["status"], "unreachable"); + assert_eq!(body["transmission"]["status"], "ok"); + } + + #[tokio::test] + async fn an_upstream_answering_wrongly_is_unreachable() { + let prowlarr = MockServer::start().await; + Mock::given(method("GET")) + .and(path("/ping")) + .respond_with(ResponseTemplate::new(500)) + .mount(&prowlarr) + .await; + let transmission = MockServer::start().await; + + let state = AppState::new(Upstreams::new( + prowlarr.uri(), + format!("{}/transmission/rpc", transmission.uri()), + )) + .expect("state"); + + let body = report(state).await; + assert_eq!(body["prowlarr"]["status"], "unreachable"); + assert_eq!(body["prowlarr"]["detail"], "http 500"); + } + + #[tokio::test] + async fn a_failed_tmdb_probe_never_echoes_the_api_key() { + let (prowlarr, transmission) = upstreams_up().await; + let tmdb = MockServer::start().await; + Mock::given(method("GET")) + .and(path("/configuration")) + .respond_with(ResponseTemplate::new(401)) + .mount(&tmdb) + .await; + + let state = AppState::new( + Upstreams::new( + prowlarr.uri(), + format!("{}/transmission/rpc", transmission.uri()), + ) + .with_tmdb_url(tmdb.uri()) + .with_tmdb_api_key(Some("super-secret".into())), + ) + .expect("state"); + + let body = report(state).await; + assert_eq!(body["tmdb"]["status"], "unreachable"); + assert!( + !body.to_string().contains("super-secret"), + "the key must not reach the response body: {body}" + ); + } + + #[test] + fn the_document_is_generated_from_the_handler() { + let document = openapi(); + let json = serde_json::to_value(&document).expect("serialise document"); + + assert!( + json["paths"]["/api/health"]["get"].is_object(), + "the health route registered itself: {json}" + ); + assert_eq!(json["paths"]["/api/health"]["get"]["tags"][0], "system"); + assert!( + json["components"]["schemas"]["HealthReport"].is_object(), + "the response body schema came along with it: {json}" + ); + } + + #[tokio::test] + async fn the_document_and_the_ui_are_served() { + let state = AppState::new(Upstreams::new( + "http://127.0.0.1:1".into(), + "http://127.0.0.1:1".into(), + )) + .expect("state"); + let base = serve(state).await; + + let document: serde_json::Value = reqwest::get(format!("{base}{OPENAPI_PATH}")) + .await + .expect("fetch document") + .json() + .await + .expect("document is json"); + assert!(document["paths"]["/api/health"].is_object()); + + let docs = reqwest::get(format!("{base}{DOCS_PATH}")) + .await + .expect("fetch docs"); + assert_eq!(docs.status(), 200); + } +} diff --git a/crates/arr-api/src/state.rs b/crates/arr-api/src/state.rs new file mode 100644 index 0000000..ec53a87 --- /dev/null +++ b/crates/arr-api/src/state.rs @@ -0,0 +1,89 @@ +//! What the API needs to answer a request: one HTTP client and the addresses +//! of the three upstreams the service cannot work without (DESIGN.md §3). + +use std::sync::Arc; +use std::time::Duration; + +/// The TMDB API root. Not a bootstrap setting (DESIGN.md §10) — only the key +/// is configurable, so this is a constant that tests point elsewhere. +pub const DEFAULT_TMDB_URL: &str = "https://api.themoviedb.org/3"; + +/// How long an upstream has to answer a probe before it counts as +/// unreachable. Health is polled by a human waiting on a page. +const PROBE_TIMEOUT: Duration = Duration::from_secs(3); + +/// Where the upstreams live, and the keys for the two that need one. +#[derive(Debug, Clone)] +pub struct Upstreams { + pub prowlarr_url: String, + pub prowlarr_api_key: Option, + pub transmission_url: String, + pub tmdb_url: String, + pub tmdb_api_key: Option, +} + +impl Upstreams { + /// Every upstream at its documented default, no keys. + #[must_use] + pub fn new(prowlarr_url: String, transmission_url: String) -> Self { + Self { + prowlarr_url, + prowlarr_api_key: None, + transmission_url, + tmdb_url: DEFAULT_TMDB_URL.to_string(), + tmdb_api_key: None, + } + } + + /// Set the Prowlarr API key. + #[must_use] + pub fn with_prowlarr_api_key(mut self, key: Option) -> Self { + self.prowlarr_api_key = key; + self + } + + /// Set the TMDB API key. + #[must_use] + pub fn with_tmdb_api_key(mut self, key: Option) -> Self { + self.tmdb_api_key = key; + self + } + + /// Point TMDB somewhere other than the real API. Tests only. + #[must_use] + pub fn with_tmdb_url(mut self, url: String) -> Self { + self.tmdb_url = url; + self + } +} + +/// Shared handler state. Cheap to clone: the client pools internally and the +/// upstream addresses are behind an [`Arc`]. +#[derive(Debug, Clone)] +pub struct AppState { + http: reqwest::Client, + upstreams: Arc, +} + +impl AppState { + /// Build the state, including the shared HTTP client. + /// + /// # Errors + /// + /// If the TLS backend cannot be initialised. + pub fn new(upstreams: Upstreams) -> Result { + let http = reqwest::Client::builder().timeout(PROBE_TIMEOUT).build()?; + Ok(Self { + http, + upstreams: Arc::new(upstreams), + }) + } + + pub(crate) fn http(&self) -> &reqwest::Client { + &self.http + } + + pub(crate) fn upstreams(&self) -> &Upstreams { + &self.upstreams + } +} diff --git a/crates/arr-daemon/Cargo.toml b/crates/arr-daemon/Cargo.toml index 98665ed..8ecd7b1 100644 --- a/crates/arr-daemon/Cargo.toml +++ b/crates/arr-daemon/Cargo.toml @@ -11,12 +11,19 @@ name = "arr" path = "src/main.rs" [dependencies] -serde.workspace = true -thiserror.workspace = true -toml.workspace = true +arr-api = { workspace = true } +axum = { workspace = true } +reqwest = { workspace = true } +serde = { workspace = true } +thiserror = { workspace = true } +tokio = { workspace = true } +toml = { workspace = true } +tower-http = { workspace = true } +tracing = { workspace = true } +tracing-subscriber = { workspace = true } [dev-dependencies] -tempfile.workspace = true +tempfile = { workspace = true } [lints] workspace = true diff --git a/crates/arr-daemon/src/main.rs b/crates/arr-daemon/src/main.rs index c7b5fdf..4774b65 100644 --- a/crates/arr-daemon/src/main.rs +++ b/crates/arr-daemon/src/main.rs @@ -4,15 +4,113 @@ mod config; use std::process::ExitCode; -fn main() -> ExitCode { - match config::Config::load() { - Ok(config) => { - println!("arr starting, bind_addr={}", config.bind_addr); - ExitCode::SUCCESS - } +use arr_api::{AppState, Upstreams}; +use config::Config; +use tower_http::trace::TraceLayer; + +/// Dump the `OpenAPI` document and exit, instead of serving. `just gen-client` +/// uses this so the TypeScript client can be regenerated without a port or a +/// single upstream being up. +const OPENAPI_FLAG: &str = "--openapi"; + +#[tokio::main] +async fn main() -> ExitCode { + if std::env::args().nth(1).as_deref() == Some(OPENAPI_FLAG) { + return dump_openapi(); + } + + tracing_subscriber::fmt() + .with_env_filter( + tracing_subscriber::EnvFilter::try_from_default_env() + .unwrap_or_else(|_| "info,tower_http=debug".into()), + ) + .init(); + + match run().await { + Ok(()) => ExitCode::SUCCESS, Err(err) => { - eprintln!("arr: {err}"); + tracing::error!("{err}"); ExitCode::FAILURE } } } + +fn dump_openapi() -> ExitCode { + match arr_api::openapi().to_pretty_json() { + Ok(json) => { + println!("{json}"); + ExitCode::SUCCESS + } + Err(err) => { + eprintln!("arr: openapi: {err}"); + ExitCode::FAILURE + } + } +} + +#[derive(Debug, thiserror::Error)] +enum Error { + #[error("config: {0}")] + Config(#[from] config::ConfigError), + #[error("http client: {0}")] + HttpClient(#[from] reqwest::Error), + #[error("bind {addr}: {source}")] + Bind { + addr: std::net::SocketAddr, + source: std::io::Error, + }, + #[error("serve: {0}")] + Serve(std::io::Error), +} + +async fn run() -> Result<(), Error> { + let config = Config::load()?; + + let state = AppState::new( + Upstreams::new(config.prowlarr_url, config.transmission_url) + .with_prowlarr_api_key(config.prowlarr_api_key) + .with_tmdb_api_key(config.tmdb_api_key), + )?; + + let app = arr_api::router(state).layer(TraceLayer::new_for_http()); + + let listener = tokio::net::TcpListener::bind(config.bind_addr) + .await + .map_err(|source| Error::Bind { + addr: config.bind_addr, + source, + })?; + + tracing::info!(addr = %config.bind_addr, docs = arr_api::DOCS_PATH, "listening"); + + axum::serve(listener, app) + .with_graceful_shutdown(shutdown()) + .await + .map_err(Error::Serve) +} + +/// Stop accepting on Ctrl-C, or on the SIGTERM a service manager sends. +async fn shutdown() { + let interrupt = async { + let _ = tokio::signal::ctrl_c().await; + }; + + #[cfg(unix)] + let terminate = async { + match tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) { + Ok(mut signal) => { + signal.recv().await; + } + Err(err) => tracing::warn!("no SIGTERM handler: {err}"), + } + }; + #[cfg(not(unix))] + let terminate = std::future::pending::<()>(); + + tokio::select! { + () = interrupt => {}, + () = terminate => {}, + } + + tracing::info!("shutting down"); +}