diff --git a/Cargo.lock b/Cargo.lock index a8d60ca..7af219e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,6 +11,56 @@ dependencies = [ "memchr", ] +[[package]] +name = "anstream" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000" + +[[package]] +name = "anstyle-parse" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys 0.61.2", +] + [[package]] name = "anyhow" version = "1.0.102" @@ -207,6 +257,52 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" +[[package]] +name = "clap" +version = "4.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ddb117e43bbf7dacf0a4190fef4d345b9bad68dfc649cb349e7d17d28428e51" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "714a53001bf66416adb0e2ef5ac857140e7dc3a0c48fb28b2f10762fc4b5069f" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2ce8604710f6733aa641a2b3731eaa1e8b3d9973d5e3565da11800813f997a9" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8d4a3bb8b1e0c1050499d1815f5ab16d04f0959b233085fb31653fbfc9d98f9" + +[[package]] +name = "colorchoice" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570" + [[package]] name = "cookie" version = "0.18.1" @@ -717,6 +813,12 @@ dependencies = [ "serde", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + [[package]] name = "itertools" version = "0.14.0" @@ -859,6 +961,7 @@ dependencies = [ "async-trait", "axum 0.8.9", "base64", + "clap", "prost", "reqwest", "roxmltree", @@ -897,6 +1000,12 @@ version = "1.21.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" +[[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + [[package]] name = "percent-encoding" version = "2.3.2" @@ -1480,6 +1589,12 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + [[package]] name = "subtle" version = "2.6.1" @@ -1902,6 +2017,12 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + [[package]] name = "uuid" version = "1.23.1" diff --git a/Cargo.toml b/Cargo.toml index 7ae4bbf..c7fbf25 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,6 +23,7 @@ thiserror = "2" url = "2" roxmltree = "0.20" base64 = "0.22" +clap = { version = "4", features = ["derive"] } tonic = "0.12" prost = "0.13" diff --git a/config.example.yaml b/config.example.yaml index 4c23007..58fe2ac 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -1,3 +1,6 @@ +app: + port: 3000 + database: url: "postgresql://music:music@localhost:5433/music_aggregator" @@ -6,6 +9,7 @@ metadata: indexers: - name: "Jackett" + indexer_type: jackett # jackett, prowlarr, or torznab url: "http://localhost:9117" api_key: "your-jackett-api-key" diff --git a/src/api/indexer_controller.rs b/src/api/indexer_controller.rs index b3aa6b3..03289f0 100644 --- a/src/api/indexer_controller.rs +++ b/src/api/indexer_controller.rs @@ -14,7 +14,7 @@ pub fn routes() -> Router { Router::new() .route("/", get(list_indexers)) .route("/search", post(search)) - .route("/:name/test", get(test_indexer)) + .route("/{name}/test", get(test_indexer)) } async fn list_indexers(State(state): State) -> Json> { diff --git a/src/api/metadata_controller.rs b/src/api/metadata_controller.rs index 9959037..e9de5ed 100644 --- a/src/api/metadata_controller.rs +++ b/src/api/metadata_controller.rs @@ -12,11 +12,11 @@ use crate::AppState; pub fn routes() -> Router { Router::new() .route("/artists/search", get(search_artists)) - .route("/artists/:id", get(get_artist)) - .route("/artists/:id/albums", get(get_artist_albums)) + .route("/artists/{id}", get(get_artist)) + .route("/artists/{id}/albums", get(get_artist_albums)) .route("/artists/sync", post(sync_artist)) - .route("/albums/:id", get(get_album)) - .route("/albums/:id/tracks", get(get_album_tracks)) + .route("/albums/{id}", get(get_album)) + .route("/albums/{id}/tracks", get(get_album_tracks)) .route("/status", get(connection_status)) } diff --git a/src/api/mod.rs b/src/api/mod.rs index 2e13715..36ed209 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -16,10 +16,11 @@ use crate::AppState; pub fn routes(state: AppState) -> Router { Router::new() + .route("/health", get(health)) .route("/tracks", get(list_tracks)) .route("/tracks", post(create_track)) - .route("/tracks/:id", get(get_track)) - .route("/tracks/:id", delete(delete_track)) + .route("/tracks/{id}", get(get_track)) + .route("/tracks/{id}", delete(delete_track)) .route("/tracks/search", get(search_tracks)) .route("/stats", get(get_stats)) .nest("/indexers", indexer_controller::routes()) @@ -28,6 +29,37 @@ pub fn routes(state: AppState) -> Router { .with_state(state) } +#[derive(serde::Serialize)] +struct Health { + status: &'static str, + services: ServiceStatus, +} + +#[derive(serde::Serialize)] +struct ServiceStatus { + torrent: bool, + metadata: bool, + indexers: Vec, +} + +async fn health(State(state): State) -> Json { + let state = state.read().await; + let indexers = state + .indexer_service + .list_indexers() + .into_iter() + .map(|i| i.name) + .collect(); + Json(Health { + status: "ok", + services: ServiceStatus { + torrent: state.torrent_service.is_connected().await, + metadata: state.metadata_service.is_connected(), + indexers, + }, + }) +} + async fn list_tracks(State(state): State) -> Json> { let state = state.read().await; Json(state.aggregator.get_all().to_vec()) diff --git a/src/api/torrent_controller.rs b/src/api/torrent_controller.rs index a7ffca7..3d6c880 100644 --- a/src/api/torrent_controller.rs +++ b/src/api/torrent_controller.rs @@ -12,10 +12,10 @@ use crate::AppState; pub fn routes() -> Router { Router::new() .route("/", get(list_torrents)) - .route("/:hash", get(get_torrent)) - .route("/:hash", delete(remove_torrent)) - .route("/:hash/pause", post(pause_torrent)) - .route("/:hash/resume", post(resume_torrent)) + .route("/{hash}", get(get_torrent)) + .route("/{hash}", delete(remove_torrent)) + .route("/{hash}/pause", post(pause_torrent)) + .route("/{hash}/resume", post(resume_torrent)) .route("/add/url", post(add_torrent_url)) .route("/add/file", post(add_torrent_file)) .route("/status", get(connection_status)) diff --git a/src/config/mod.rs b/src/config/mod.rs index 5e30969..581496d 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -14,12 +14,32 @@ pub enum ConfigError { #[derive(Debug, Clone, Deserialize)] pub struct Config { + #[serde(default)] + pub app: AppConfig, pub database: DatabaseConfig, pub metadata: MetadataConfig, pub indexers: Vec, pub torrent: TorrentConfig, } +#[derive(Debug, Clone, Deserialize)] +pub struct AppConfig { + #[serde(default = "default_port")] + pub port: u16, +} + +impl Default for AppConfig { + fn default() -> Self { + Self { + port: default_port(), + } + } +} + +fn default_port() -> u16 { + 3000 +} + #[derive(Debug, Clone, Deserialize)] pub struct MetadataConfig { pub endpoint: String, @@ -30,9 +50,20 @@ pub struct DatabaseConfig { pub url: String, } +#[derive(Debug, Clone, Copy, Default, Deserialize, PartialEq, Eq)] +#[serde(rename_all = "lowercase")] +pub enum IndexerType { + #[default] + Jackett, + Prowlarr, + Torznab, +} + #[derive(Debug, Clone, Deserialize)] pub struct IndexerConfig { pub name: String, + #[serde(default)] + pub indexer_type: IndexerType, pub url: String, pub api_key: String, } diff --git a/src/indexer/torznab.rs b/src/indexer/torznab.rs index ec2d117..c650691 100644 --- a/src/indexer/torznab.rs +++ b/src/indexer/torznab.rs @@ -53,15 +53,14 @@ impl TorznabIndexer { .join(","); query.append_pair("cat", &cats); - query.append_pair("artist", &criteria.clean_artist()); - + let mut q_parts = vec![criteria.clean_artist()]; if let Some(album) = criteria.clean_album() { - query.append_pair("album", &album); + q_parts.push(album); } - if let Some(year) = criteria.year { - query.append_pair("year", &year.to_string()); + q_parts.push(year.to_string()); } + query.append_pair("q", &q_parts.join(" ")); query.append_pair("limit", &criteria.limit.to_string()); query.append_pair("offset", &criteria.offset.to_string()); diff --git a/src/main.rs b/src/main.rs index 016a820..ed0c32a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ use std::sync::Arc; use tokio::sync::RwLock; use axum::Router; +use clap::Parser; use music_agregator::{ api, config, services::{IndexerService, MetadataService, TorrentService}, @@ -11,6 +12,17 @@ use tower_http::cors::{Any, CorsLayer}; use tower_http::trace::TraceLayer; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; +#[derive(Parser)] +#[command(name = "music-agregator")] +#[command(about = "Music aggregation service with torrent and metadata integration")] +struct Args { + #[arg(short, long, default_value = "config.yaml")] + config: String, + + #[arg(short, long)] + port: Option, +} + #[tokio::main] async fn main() { tracing_subscriber::registry() @@ -18,10 +30,11 @@ async fn main() { .with(tracing_subscriber::EnvFilter::from_default_env()) .init(); - let config_path = std::env::var("CONFIG_PATH").unwrap_or_else(|_| "config.yaml".to_string()); - let config = match config::Config::load(&config_path) { + let args = Args::parse(); + + let config = match config::Config::load(&args.config) { Ok(cfg) => { - tracing::info!("loaded config from {}", config_path); + tracing::info!("loaded config from {}", args.config); cfg } Err(e) => { @@ -92,7 +105,9 @@ async fn main() { .layer(cors) .layer(TraceLayer::new_for_http()); - let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); + let port = args.port.unwrap_or(config.app.port); + let addr = format!("0.0.0.0:{}", port); + let listener = tokio::net::TcpListener::bind(&addr).await.unwrap(); tracing::info!("listening on {}", listener.local_addr().unwrap()); axum::serve(listener, app).await.unwrap(); } diff --git a/src/services/indexer_service.rs b/src/services/indexer_service.rs index 6521c4a..ca0d0b7 100644 --- a/src/services/indexer_service.rs +++ b/src/services/indexer_service.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use std::sync::Arc; -use crate::config::IndexerConfig; +use crate::config::{IndexerConfig, IndexerType}; use crate::indexer::{Indexer, IndexerError, MusicSearchCriteria, SearchResult, TorznabIndexer}; pub struct IndexerService { @@ -19,13 +19,23 @@ impl IndexerService { let mut service = Self::new(); for config in configs { - let indexer = TorznabIndexer::new(&config.name, &config.url, &config.api_key)?; + let torznab_url = Self::build_torznab_url(&config.url, config.indexer_type); + let indexer = TorznabIndexer::new(&config.name, &torznab_url, &config.api_key)?; service.add_indexer(Arc::new(indexer)); } Ok(service) } + fn build_torznab_url(base_url: &str, indexer_type: IndexerType) -> String { + let base = base_url.trim_end_matches('/'); + match indexer_type { + IndexerType::Jackett => format!("{}/api/v2.0/indexers/all/results/torznab/", base), + IndexerType::Prowlarr => format!("{}/api/v1/indexer/all/torznab", base), + IndexerType::Torznab => base_url.to_string(), + } + } + pub fn add_indexer(&mut self, indexer: Arc) { self.indexers.insert(indexer.name().to_string(), indexer); }