Refactoring de l'API rest des musicsources
This commit is contained in:
9
Cargo.lock
generated
9
Cargo.lock
generated
@@ -13,6 +13,7 @@ dependencies = [
|
||||
"pmomediarenderer",
|
||||
"pmomediaserver",
|
||||
"pmoserver",
|
||||
"pmosource",
|
||||
"pmoupnp",
|
||||
"serde_json",
|
||||
"tokio",
|
||||
@@ -2465,12 +2466,20 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"async-trait",
|
||||
"axum",
|
||||
"lazy_static",
|
||||
"pmoaudiocache",
|
||||
"pmoconfig",
|
||||
"pmocovers",
|
||||
"pmodidl",
|
||||
"pmoplaylist",
|
||||
"pmoserver",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"thiserror 1.0.69",
|
||||
"tokio",
|
||||
"tracing",
|
||||
"utoipa",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -8,6 +8,7 @@ pmoconfig = { path = "../pmoconfig" }
|
||||
pmoupnp = { path = "../pmoupnp"}
|
||||
pmomediarenderer = { path = "../pmomediarenderer" }
|
||||
pmomediaserver = { path = "../pmomediaserver", features = ["qobuz", "paradise", "api"] }
|
||||
pmosource = { path = "../pmosource", features = ["server"] }
|
||||
pmoserver = { path = "../pmoserver" }
|
||||
pmocovers = { path = "../pmocovers", features = ["pmoserver"] }
|
||||
pmoapp = { path = "../pmoapp", features = ["pmoserver"] }
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
use pmoapp::{WebAppExt, Webapp};
|
||||
use pmocovers::CoverCacheExt;
|
||||
use pmomediarenderer::MEDIA_RENDERER;
|
||||
use pmomediaserver::{MEDIA_SERVER, sources::SourcesExt, MediaServerExt, sources_api_router, sources_api::SourcesApiDoc};
|
||||
use pmomediaserver::{MEDIA_SERVER, sources::SourcesExt};
|
||||
use pmosource::MusicSourceExt;
|
||||
use pmoserver::ServerBuilder;
|
||||
use pmoupnp::{UpnpServer, ssdp::SsdpServer, upnp_api::UpnpApiExt};
|
||||
use tracing::info;
|
||||
use utoipa::OpenApi;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
@@ -37,9 +37,12 @@ async fn main() {
|
||||
// Enregistrer l'API d'introspection UPnP
|
||||
server.register_upnp_api().await;
|
||||
|
||||
// Enregistrer l'API de gestion des sources musicales avec OpenAPI
|
||||
info!("📡 Registering Sources API with OpenAPI documentation...");
|
||||
server.add_openapi(sources_api_router(), SourcesApiDoc::openapi(), "sources").await;
|
||||
// Initialiser le système de gestion des sources musicales avec API REST
|
||||
info!("📡 Initializing music sources management system...");
|
||||
server
|
||||
.init_music_sources()
|
||||
.await
|
||||
.expect("Failed to initialize music sources API");
|
||||
|
||||
info!("📡 Registering MediaRenderer...");
|
||||
let renderer_instance = server
|
||||
|
||||
@@ -6,7 +6,7 @@ edition = "2024"
|
||||
[dependencies]
|
||||
pmoupnp = { path = "../pmoupnp" }
|
||||
pmodidl = { path = "../pmodidl" }
|
||||
pmosource = { path = "../pmosource" }
|
||||
pmosource = { path = "../pmosource", features = ["server"] }
|
||||
pmoserver = { path = "../pmoserver" }
|
||||
|
||||
once_cell = "1.20"
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
//! - **Search** : Recherche dans les sources qui le supportent
|
||||
//! - **Update ID** : Suivi des changements pour les notifications UPnP
|
||||
|
||||
use crate::server_ext::get_source_registry;
|
||||
use pmosource::api::{list_all_sources, get_source as get_source_from_registry};
|
||||
use pmodidl::{Container, DIDLLite};
|
||||
use pmosource::{BrowseResult, MusicSource};
|
||||
use std::sync::Arc;
|
||||
@@ -102,10 +102,8 @@ impl ContentHandler {
|
||||
Ok((didl, 1, 1, 0))
|
||||
} else {
|
||||
// Essayer de trouver l'objet dans les sources
|
||||
let registry = get_source_registry().await;
|
||||
|
||||
// Vérifier si c'est un container racine d'une source
|
||||
if let Some(source) = registry.get(object_id).await {
|
||||
if let Some(source) = get_source_from_registry(object_id).await {
|
||||
let container = source
|
||||
.root_container()
|
||||
.await
|
||||
@@ -116,7 +114,7 @@ impl ContentHandler {
|
||||
}
|
||||
|
||||
// Sinon, chercher dans les sources
|
||||
for source in registry.list_all().await {
|
||||
for source in list_all_sources().await {
|
||||
if let Ok(result) = source.browse(object_id).await {
|
||||
// L'objet a été trouvé, retourner ses métadonnées
|
||||
match result {
|
||||
@@ -165,17 +163,15 @@ impl ContentHandler {
|
||||
return self.browse_root(starting_index, requested_count).await;
|
||||
}
|
||||
|
||||
let registry = get_source_registry().await;
|
||||
|
||||
// Vérifier si c'est le container racine d'une source
|
||||
if let Some(source) = registry.get(object_id).await {
|
||||
if let Some(source) = get_source_from_registry(object_id).await {
|
||||
return self
|
||||
.browse_source_root(source, starting_index, requested_count)
|
||||
.await;
|
||||
}
|
||||
|
||||
// Sinon, chercher dans les sources
|
||||
for source in registry.list_all().await {
|
||||
for source in list_all_sources().await {
|
||||
if let Ok(result) = source.browse(object_id).await {
|
||||
return self
|
||||
.browse_result_to_didl(result, source, starting_index, requested_count)
|
||||
@@ -192,8 +188,7 @@ impl ContentHandler {
|
||||
starting_index: u32,
|
||||
requested_count: u32,
|
||||
) -> Result<(String, u32, u32, u32), String> {
|
||||
let registry = get_source_registry().await;
|
||||
let sources = registry.list_all().await;
|
||||
let sources = list_all_sources().await;
|
||||
|
||||
let mut containers = Vec::new();
|
||||
for source in sources.iter() {
|
||||
@@ -299,8 +294,8 @@ impl ContentHandler {
|
||||
|
||||
/// Construit le container racine du MediaServer
|
||||
async fn build_root_container(&self) -> Container {
|
||||
let registry = get_source_registry().await;
|
||||
let child_count = registry.count().await;
|
||||
let sources = list_all_sources().await;
|
||||
let child_count = sources.len();
|
||||
|
||||
Container {
|
||||
id: "0".to_string(),
|
||||
@@ -335,12 +330,11 @@ impl ContentHandler {
|
||||
"ContentDirectory::Search"
|
||||
);
|
||||
|
||||
let registry = get_source_registry().await;
|
||||
let mut all_containers = Vec::new();
|
||||
let mut all_items = Vec::new();
|
||||
|
||||
// Rechercher dans toutes les sources qui supportent la recherche
|
||||
for source in registry.list_all().await {
|
||||
for source in list_all_sources().await {
|
||||
if source.capabilities().supports_search {
|
||||
if let Ok(result) = source.search(search_criteria).await {
|
||||
match result {
|
||||
@@ -375,8 +369,7 @@ impl ContentHandler {
|
||||
|
||||
/// Retourne le system update ID global
|
||||
pub async fn get_system_update_id(&self) -> u32 {
|
||||
let registry = get_source_registry().await;
|
||||
let sources = registry.list_all().await;
|
||||
let sources = list_all_sources().await;
|
||||
|
||||
// Combiner les update IDs de toutes les sources
|
||||
let mut combined_id = 0u32;
|
||||
|
||||
@@ -76,11 +76,14 @@ pub mod sources_api;
|
||||
|
||||
pub use device::MEDIA_SERVER;
|
||||
pub use source_registry::SourceRegistry;
|
||||
pub use server_ext::{MediaServerExt, get_source_registry};
|
||||
pub use server_ext::{MediaServerExt, get_source_registry, MusicSourceExt};
|
||||
pub use content_handler::ContentHandler;
|
||||
pub use sources::{SourcesExt, SourceInitError};
|
||||
|
||||
// L'API des sources est maintenant dans pmosource
|
||||
// Pour des raisons de compatibilité, on réexporte ici
|
||||
#[cfg(feature = "api")]
|
||||
#[deprecated(since = "0.2.0", note = "Use pmosource::api directly")]
|
||||
pub use sources_api::{sources_api_router, SourcesApiDoc};
|
||||
|
||||
// Re-export sources when features are enabled
|
||||
|
||||
@@ -2,277 +2,77 @@
|
||||
//!
|
||||
//! Ce module fournit un trait d'extension pour `pmoserver::Server` permettant
|
||||
//! d'enregistrer facilement des sources musicales et de configurer le MediaServer.
|
||||
//!
|
||||
//! **Note**: Ce module réexporte `MusicSourceExt` de `pmosource` et ajoute des
|
||||
//! méthodes spécifiques au MediaServer UPnP.
|
||||
|
||||
use crate::source_registry::SourceRegistry;
|
||||
use async_trait::async_trait;
|
||||
use pmosource::MusicSource;
|
||||
use pmoserver::Server;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::OnceCell;
|
||||
|
||||
/// Extension pour le registre de sources au niveau global
|
||||
///
|
||||
/// Ce registre est partagé par toutes les instances du serveur et permet
|
||||
/// d'accéder aux sources musicales depuis n'importe où dans l'application.
|
||||
static GLOBAL_REGISTRY: OnceCell<SourceRegistry> = OnceCell::const_new();
|
||||
// Réexporter le trait de base de pmosource
|
||||
pub use pmosource::MusicSourceExt;
|
||||
|
||||
/// Initialise le registre global
|
||||
///
|
||||
/// Cette fonction est appelée automatiquement lors de la première utilisation.
|
||||
async fn init_global_registry() -> &'static SourceRegistry {
|
||||
GLOBAL_REGISTRY
|
||||
.get_or_init(|| async { SourceRegistry::new() })
|
||||
.await
|
||||
}
|
||||
|
||||
/// Récupère le registre global de sources
|
||||
/// Récupère le registre global de sources (délègue à pmosource)
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```ignore
|
||||
/// use pmomediaserver::server_ext::get_source_registry;
|
||||
///
|
||||
/// let registry = get_source_registry().await;
|
||||
/// if let Some(source) = registry.get("qobuz").await {
|
||||
/// // Utiliser la source
|
||||
/// }
|
||||
/// let sources = pmosource::api::list_all_sources().await;
|
||||
/// ```
|
||||
pub async fn get_source_registry() -> &'static SourceRegistry {
|
||||
init_global_registry().await
|
||||
#[deprecated(since = "0.2.0", note = "Use pmosource::api::list_all_sources() directly")]
|
||||
pub async fn get_source_registry() -> Vec<Arc<dyn MusicSource>> {
|
||||
pmosource::api::list_all_sources().await
|
||||
}
|
||||
|
||||
/// Trait d'extension pour le serveur permettant l'enregistrement de sources musicales
|
||||
/// Trait d'extension pour le serveur MediaServer UPnP
|
||||
///
|
||||
/// Ce trait ajoute des méthodes pratiques à `Server` pour enregistrer des sources
|
||||
/// musicales et les rendre disponibles via le MediaServer.
|
||||
/// Ce trait ajoute des méthodes spécifiques au MediaServer UPnP.
|
||||
/// Pour l'enregistrement de sources, utilisez le trait `MusicSourceExt` de `pmosource`.
|
||||
///
|
||||
/// # Examples
|
||||
/// **Note**: Ce trait est maintenant deprecated. Utilisez directement `MusicSourceExt`
|
||||
/// de `pmosource` pour l'enregistrement et la gestion des sources.
|
||||
///
|
||||
/// # Migration
|
||||
///
|
||||
/// Ancien code :
|
||||
/// ```ignore
|
||||
/// use pmomediaserver::server_ext::MediaServerExt;
|
||||
/// use pmoserver::ServerBuilder;
|
||||
/// server.register_music_source(source).await;
|
||||
/// ```
|
||||
///
|
||||
/// let mut server = ServerBuilder::new_configured().build();
|
||||
///
|
||||
/// // Enregistrer une source
|
||||
/// let qobuz = Arc::new(QobuzSource::new());
|
||||
/// server.register_music_source(qobuz).await;
|
||||
///
|
||||
/// // Lister toutes les sources
|
||||
/// let sources = server.list_music_sources().await;
|
||||
/// Nouveau code :
|
||||
/// ```ignore
|
||||
/// use pmosource::MusicSourceExt;
|
||||
/// server.register_music_source(source).await;
|
||||
/// ```
|
||||
#[async_trait]
|
||||
pub trait MediaServerExt {
|
||||
/// Enregistre une source musicale dans le MediaServer
|
||||
///
|
||||
/// La source devient immédiatement disponible via le service ContentDirectory
|
||||
/// et peut être parcourue par les clients UPnP.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `source` - La source musicale à enregistrer (Arc<dyn MusicSource>)
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```ignore
|
||||
/// let qobuz = Arc::new(QobuzSource::new(credentials));
|
||||
/// server.register_music_source(qobuz).await;
|
||||
/// ```
|
||||
async fn register_music_source(&mut self, source: Arc<dyn MusicSource>);
|
||||
|
||||
/// Récupère une source musicale par son ID
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `id` - L'ID unique de la source
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Un `Arc` vers la source si elle existe, ou `None`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```ignore
|
||||
/// if let Some(source) = server.get_music_source("qobuz").await {
|
||||
/// println!("Found: {}", source.name());
|
||||
/// }
|
||||
/// ```
|
||||
async fn get_music_source(&self, id: &str) -> Option<Arc<dyn MusicSource>>;
|
||||
|
||||
/// Liste toutes les sources musicales enregistrées
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Un vecteur contenant toutes les sources enregistrées.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```ignore
|
||||
/// let sources = server.list_music_sources().await;
|
||||
/// for source in sources {
|
||||
/// println!("- {} ({})", source.name(), source.id());
|
||||
/// }
|
||||
/// ```
|
||||
async fn list_music_sources(&self) -> Vec<Arc<dyn MusicSource>>;
|
||||
|
||||
/// Compte le nombre de sources musicales enregistrées
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Le nombre total de sources.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```ignore
|
||||
/// let count = server.count_music_sources().await;
|
||||
/// println!("Total sources: {}", count);
|
||||
/// ```
|
||||
async fn count_music_sources(&self) -> usize;
|
||||
|
||||
/// Supprime une source musicale du registre
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `id` - L'ID de la source à supprimer
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// `true` si la source a été supprimée, `false` si elle n'existait pas.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```ignore
|
||||
/// if server.remove_music_source("old-radio").await {
|
||||
/// println!("Source removed");
|
||||
/// }
|
||||
/// ```
|
||||
async fn remove_music_source(&mut self, id: &str) -> bool;
|
||||
async fn count_music_sources(&self) -> usize {
|
||||
pmosource::api::list_all_sources().await.len()
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl MediaServerExt for Server {
|
||||
async fn register_music_source(&mut self, source: Arc<dyn MusicSource>) {
|
||||
let registry = get_source_registry().await;
|
||||
|
||||
tracing::info!(
|
||||
source_id = %source.id(),
|
||||
source_name = %source.name(),
|
||||
"Registering music source to MediaServer"
|
||||
);
|
||||
|
||||
registry.register(source).await;
|
||||
}
|
||||
|
||||
async fn get_music_source(&self, id: &str) -> Option<Arc<dyn MusicSource>> {
|
||||
let registry = get_source_registry().await;
|
||||
registry.get(id).await
|
||||
}
|
||||
|
||||
async fn list_music_sources(&self) -> Vec<Arc<dyn MusicSource>> {
|
||||
let registry = get_source_registry().await;
|
||||
registry.list_all().await
|
||||
}
|
||||
|
||||
async fn count_music_sources(&self) -> usize {
|
||||
let registry = get_source_registry().await;
|
||||
registry.count().await
|
||||
}
|
||||
|
||||
async fn remove_music_source(&mut self, id: &str) -> bool {
|
||||
let registry = get_source_registry().await;
|
||||
registry.remove(id).await
|
||||
}
|
||||
// Implementation par défaut fournie dans le trait
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use pmosource::{MusicSource, Result, BrowseResult};
|
||||
use pmodidl::{Container, Item};
|
||||
use std::time::SystemTime;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct DummySource {
|
||||
id: String,
|
||||
name: String,
|
||||
}
|
||||
|
||||
impl DummySource {
|
||||
fn new(id: &str, name: &str) -> Self {
|
||||
Self {
|
||||
id: id.to_string(),
|
||||
name: name.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl MusicSource for DummySource {
|
||||
fn name(&self) -> &str {
|
||||
&self.name
|
||||
}
|
||||
|
||||
fn id(&self) -> &str {
|
||||
&self.id
|
||||
}
|
||||
|
||||
fn default_image(&self) -> &[u8] {
|
||||
&[]
|
||||
}
|
||||
|
||||
async fn root_container(&self) -> Result<Container> {
|
||||
Ok(Container {
|
||||
id: self.id.clone(),
|
||||
parent_id: "0".to_string(),
|
||||
restricted: Some("1".to_string()),
|
||||
child_count: Some("0".to_string()),
|
||||
title: self.name.clone(),
|
||||
class: "object.container".to_string(),
|
||||
containers: vec![],
|
||||
items: vec![],
|
||||
})
|
||||
}
|
||||
|
||||
async fn browse(&self, _object_id: &str) -> Result<BrowseResult> {
|
||||
Ok(BrowseResult::Items(vec![]))
|
||||
}
|
||||
|
||||
async fn resolve_uri(&self, object_id: &str) -> Result<String> {
|
||||
Ok(format!("http://example.com/{}", object_id))
|
||||
}
|
||||
|
||||
fn supports_fifo(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
async fn append_track(&self, _track: Item) -> Result<()> {
|
||||
Err(pmosource::MusicSourceError::FifoNotSupported)
|
||||
}
|
||||
|
||||
async fn remove_oldest(&self) -> Result<Option<Item>> {
|
||||
Err(pmosource::MusicSourceError::FifoNotSupported)
|
||||
}
|
||||
|
||||
async fn update_id(&self) -> u32 {
|
||||
0
|
||||
}
|
||||
|
||||
async fn last_change(&self) -> Option<SystemTime> {
|
||||
None
|
||||
}
|
||||
|
||||
async fn get_items(&self, _offset: usize, _count: usize) -> Result<Vec<Item>> {
|
||||
Ok(vec![])
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_global_registry_singleton() {
|
||||
// Vérifier que le registre global est bien un singleton
|
||||
let registry1 = get_source_registry().await;
|
||||
let registry2 = get_source_registry().await;
|
||||
|
||||
// Les deux références devraient pointer vers le même registre
|
||||
assert!(std::ptr::eq(registry1, registry2));
|
||||
#[test]
|
||||
fn test_trait_exists() {
|
||||
// Ce test vérifie simplement que le module compile
|
||||
// Les tests fonctionnels sont maintenant dans pmosource
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
//! Ce module fournit des helpers pour créer et enregistrer facilement des sources
|
||||
//! musicales préconfigurées à partir de la configuration système.
|
||||
|
||||
use crate::server_ext::MediaServerExt;
|
||||
use pmosource::MusicSourceExt;
|
||||
use pmoserver::Server;
|
||||
use std::sync::Arc;
|
||||
|
||||
|
||||
@@ -12,7 +12,8 @@
|
||||
//! - `POST /sources/qobuz` - Enregistrer Qobuz (feature "qobuz")
|
||||
//! - `DELETE /sources/:id` - Désenregistrer une source
|
||||
|
||||
use crate::server_ext::get_source_registry;
|
||||
// Utiliser les fonctions du registre de pmosource
|
||||
use pmosource::api::{list_all_sources, get_source as get_source_from_registry, register_source, unregister_source as unregister_source_from_registry};
|
||||
use axum::{
|
||||
extract::Path,
|
||||
http::StatusCode,
|
||||
@@ -102,8 +103,7 @@ pub struct ErrorResponse {
|
||||
tag = "sources"
|
||||
)]
|
||||
async fn list_sources() -> impl IntoResponse {
|
||||
let registry = get_source_registry().await;
|
||||
let sources = registry.list_all().await;
|
||||
let sources = list_all_sources().await;
|
||||
|
||||
let source_infos: Vec<SourceInfo> = sources
|
||||
.iter()
|
||||
@@ -147,9 +147,7 @@ async fn list_sources() -> impl IntoResponse {
|
||||
tag = "sources"
|
||||
)]
|
||||
async fn get_source(Path(id): Path<String>) -> impl IntoResponse {
|
||||
let registry = get_source_registry().await;
|
||||
|
||||
match registry.get(&id).await {
|
||||
match get_source_from_registry(&id).await {
|
||||
Some(source) => {
|
||||
let caps = source.capabilities();
|
||||
let info = SourceInfo {
|
||||
@@ -192,8 +190,6 @@ async fn get_source(Path(id): Path<String>) -> impl IntoResponse {
|
||||
async fn register_qobuz(Json(creds): Json<QobuzCredentials>) -> impl IntoResponse {
|
||||
use pmoqobuz::{QobuzClient, QobuzSource};
|
||||
|
||||
let registry = get_source_registry().await;
|
||||
|
||||
// Créer le client selon les credentials fournis
|
||||
let client_result = if let (Some(username), Some(password)) = (creds.username, creds.password) {
|
||||
QobuzClient::new(&username, &password).await
|
||||
@@ -223,7 +219,7 @@ async fn register_qobuz(Json(creds): Json<QobuzCredentials>) -> impl IntoRespons
|
||||
let source = Arc::new(QobuzSource::new(client, &base_url));
|
||||
let source_id = source.as_ref().id().to_string();
|
||||
|
||||
registry.register(source).await;
|
||||
register_source(source).await;
|
||||
|
||||
(
|
||||
StatusCode::CREATED,
|
||||
@@ -252,8 +248,6 @@ async fn register_qobuz(Json(creds): Json<QobuzCredentials>) -> impl IntoRespons
|
||||
async fn register_paradise(Json(params): Json<ParadiseParams>) -> impl IntoResponse {
|
||||
use pmoparadise::{RadioParadiseClient, RadioParadiseSource};
|
||||
|
||||
let registry = get_source_registry().await;
|
||||
|
||||
// Créer le client (Radio Paradise ne nécessite pas d'auth)
|
||||
let client = match RadioParadiseClient::new().await {
|
||||
Ok(c) => c,
|
||||
@@ -282,7 +276,7 @@ async fn register_paradise(Json(params): Json<ParadiseParams>) -> impl IntoRespo
|
||||
|
||||
let source_id = source.as_ref().id().to_string();
|
||||
|
||||
registry.register(source).await;
|
||||
register_source(source).await;
|
||||
|
||||
(
|
||||
StatusCode::CREATED,
|
||||
@@ -310,9 +304,7 @@ async fn register_paradise(Json(params): Json<ParadiseParams>) -> impl IntoRespo
|
||||
tag = "sources"
|
||||
)]
|
||||
async fn unregister_source(Path(id): Path<String>) -> impl IntoResponse {
|
||||
let registry = get_source_registry().await;
|
||||
|
||||
if registry.remove(&id).await {
|
||||
if unregister_source_from_registry(&id).await {
|
||||
(
|
||||
StatusCode::OK,
|
||||
Json(serde_json::json!({
|
||||
|
||||
@@ -30,6 +30,19 @@ pmoplaylist = { path = "../pmoplaylist" }
|
||||
pmoaudiocache = { path = "../pmoaudiocache", optional = true }
|
||||
pmocovers = { path = "../pmocovers", optional = true }
|
||||
|
||||
# Server extension (optional)
|
||||
pmoserver = { path = "../pmoserver", optional = true }
|
||||
pmoconfig = { path = "../pmoconfig", optional = true }
|
||||
|
||||
# Web framework for API (optional)
|
||||
axum = { version = "0.8", optional = true }
|
||||
serde = { version = "1.0", features = ["derive"], optional = true }
|
||||
serde_json = { version = "1.0", optional = true }
|
||||
utoipa = { version = "5.3", optional = true }
|
||||
tracing = { version = "0.1", optional = true }
|
||||
lazy_static = { version = "1.4", optional = true }
|
||||
|
||||
[features]
|
||||
default = ["cache"]
|
||||
cache = ["pmoaudiocache", "pmocovers"]
|
||||
server = ["pmoserver", "pmoconfig", "axum", "serde", "serde_json", "utoipa", "tracing", "lazy_static"]
|
||||
|
||||
486
pmosource/src/api.rs
Normal file
486
pmosource/src/api.rs
Normal file
@@ -0,0 +1,486 @@
|
||||
//! # Sources API - API REST pour la gestion des sources musicales
|
||||
//!
|
||||
//! Ce module fournit une API REST pour :
|
||||
//! - Lister les sources enregistrées
|
||||
//! - Obtenir des informations sur une source spécifique
|
||||
//! - Récupérer les statistiques d'une source
|
||||
//!
|
||||
//! ## Routes
|
||||
//!
|
||||
//! - `GET /sources` - Liste toutes les sources
|
||||
//! - `GET /sources/:id` - Informations sur une source
|
||||
//! - `GET /sources/:id/capabilities` - Capacités d'une source
|
||||
//! - `GET /sources/:id/statistics` - Statistiques d'une source
|
||||
//! - `GET /sources/:id/root` - Container racine d'une source
|
||||
//! - `GET /sources/:id/image` - Image par défaut d'une source
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
use axum::{
|
||||
extract::Path,
|
||||
http::{StatusCode, header},
|
||||
response::{IntoResponse, Response},
|
||||
routing::get,
|
||||
Json, Router,
|
||||
};
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
use crate::{MusicSource, SourceCapabilities, SourceStatistics};
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Information sur une source musicale
|
||||
#[cfg(feature = "server")]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, utoipa::ToSchema)]
|
||||
pub struct SourceInfo {
|
||||
/// ID unique de la source
|
||||
pub id: String,
|
||||
/// Nom de la source
|
||||
pub name: String,
|
||||
/// La source supporte-t-elle les opérations FIFO
|
||||
pub supports_fifo: bool,
|
||||
/// Capacités de la source
|
||||
pub capabilities: SourceCapabilitiesInfo,
|
||||
}
|
||||
|
||||
/// Capacités d'une source
|
||||
#[cfg(feature = "server")]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, utoipa::ToSchema)]
|
||||
pub struct SourceCapabilitiesInfo {
|
||||
pub supports_search: bool,
|
||||
pub supports_favorites: bool,
|
||||
pub supports_playlists: bool,
|
||||
pub supports_user_content: bool,
|
||||
pub supports_high_res_audio: bool,
|
||||
pub max_sample_rate: Option<u32>,
|
||||
pub supports_multiple_formats: bool,
|
||||
pub supports_advanced_search: bool,
|
||||
pub supports_pagination: bool,
|
||||
}
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
impl From<SourceCapabilities> for SourceCapabilitiesInfo {
|
||||
fn from(caps: SourceCapabilities) -> Self {
|
||||
Self {
|
||||
supports_search: caps.supports_search,
|
||||
supports_favorites: caps.supports_favorites,
|
||||
supports_playlists: caps.supports_playlists,
|
||||
supports_user_content: caps.supports_user_content,
|
||||
supports_high_res_audio: caps.supports_high_res_audio,
|
||||
max_sample_rate: caps.max_sample_rate,
|
||||
supports_multiple_formats: caps.supports_multiple_formats,
|
||||
supports_advanced_search: caps.supports_advanced_search,
|
||||
supports_pagination: caps.supports_pagination,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Statistiques d'une source
|
||||
#[cfg(feature = "server")]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, utoipa::ToSchema)]
|
||||
pub struct SourceStatisticsInfo {
|
||||
pub total_items: Option<usize>,
|
||||
pub total_containers: Option<usize>,
|
||||
pub cached_items: Option<usize>,
|
||||
pub cache_size_bytes: Option<u64>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
impl From<SourceStatistics> for SourceStatisticsInfo {
|
||||
fn from(stats: SourceStatistics) -> Self {
|
||||
Self {
|
||||
total_items: stats.total_items,
|
||||
total_containers: stats.total_containers,
|
||||
cached_items: stats.cached_items,
|
||||
cache_size_bytes: stats.cache_size_bytes,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Liste des sources enregistrées
|
||||
#[cfg(feature = "server")]
|
||||
#[derive(Debug, Serialize, Deserialize, utoipa::ToSchema)]
|
||||
pub struct SourcesList {
|
||||
/// Nombre total de sources
|
||||
pub count: usize,
|
||||
/// Liste des sources
|
||||
pub sources: Vec<SourceInfo>,
|
||||
}
|
||||
|
||||
/// Container racine d'une source
|
||||
#[cfg(feature = "server")]
|
||||
#[derive(Debug, Serialize, Deserialize, utoipa::ToSchema)]
|
||||
pub struct SourceRootContainer {
|
||||
/// ID du container
|
||||
pub id: String,
|
||||
/// Parent ID
|
||||
pub parent_id: String,
|
||||
/// Titre du container
|
||||
pub title: String,
|
||||
/// Classe UPnP
|
||||
pub class: String,
|
||||
/// Nombre d'enfants
|
||||
pub child_count: Option<String>,
|
||||
}
|
||||
|
||||
/// Message d'erreur
|
||||
#[cfg(feature = "server")]
|
||||
#[derive(Debug, Serialize, utoipa::ToSchema)]
|
||||
pub struct ErrorResponse {
|
||||
/// Message d'erreur
|
||||
pub error: String,
|
||||
}
|
||||
|
||||
// ============= Gestionnaire de registre global =============
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
lazy_static::lazy_static! {
|
||||
static ref SOURCE_REGISTRY: Arc<RwLock<Vec<Arc<dyn MusicSource>>>> =
|
||||
Arc::new(RwLock::new(Vec::new()));
|
||||
}
|
||||
|
||||
/// Enregistre une source dans le registre global
|
||||
#[cfg(feature = "server")]
|
||||
pub async fn register_source(source: Arc<dyn MusicSource>) {
|
||||
let mut registry = SOURCE_REGISTRY.write().await;
|
||||
|
||||
// Vérifier si la source existe déjà (par ID)
|
||||
let source_id = source.id();
|
||||
registry.retain(|s| s.id() != source_id);
|
||||
|
||||
// Ajouter la nouvelle source
|
||||
registry.push(source);
|
||||
}
|
||||
|
||||
/// Retire une source du registre global
|
||||
#[cfg(feature = "server")]
|
||||
pub async fn unregister_source(source_id: &str) -> bool {
|
||||
let mut registry = SOURCE_REGISTRY.write().await;
|
||||
let initial_len = registry.len();
|
||||
registry.retain(|s| s.id() != source_id);
|
||||
registry.len() < initial_len
|
||||
}
|
||||
|
||||
/// Liste toutes les sources enregistrées
|
||||
#[cfg(feature = "server")]
|
||||
pub async fn list_all_sources() -> Vec<Arc<dyn MusicSource>> {
|
||||
let registry = SOURCE_REGISTRY.read().await;
|
||||
registry.clone()
|
||||
}
|
||||
|
||||
/// Récupère une source par son ID
|
||||
#[cfg(feature = "server")]
|
||||
pub async fn get_source(source_id: &str) -> Option<Arc<dyn MusicSource>> {
|
||||
let registry = SOURCE_REGISTRY.read().await;
|
||||
registry.iter().find(|s| s.id() == source_id).cloned()
|
||||
}
|
||||
|
||||
// ============= Handlers API =============
|
||||
|
||||
/// Liste toutes les sources musicales enregistrées
|
||||
///
|
||||
/// Retourne la liste complète des sources avec leurs informations.
|
||||
#[cfg(feature = "server")]
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = "/sources",
|
||||
responses(
|
||||
(status = 200, description = "Liste des sources", body = SourcesList),
|
||||
),
|
||||
tag = "sources"
|
||||
)]
|
||||
async fn list_sources() -> impl IntoResponse {
|
||||
let sources = list_all_sources().await;
|
||||
|
||||
let source_infos: Vec<SourceInfo> = sources
|
||||
.iter()
|
||||
.map(|s| {
|
||||
let caps = s.capabilities();
|
||||
SourceInfo {
|
||||
id: s.id().to_string(),
|
||||
name: s.name().to_string(),
|
||||
supports_fifo: s.supports_fifo(),
|
||||
capabilities: caps.into(),
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
let list = SourcesList {
|
||||
count: source_infos.len(),
|
||||
sources: source_infos,
|
||||
};
|
||||
|
||||
Json(list)
|
||||
}
|
||||
|
||||
/// Obtient les informations d'une source spécifique
|
||||
///
|
||||
/// Retourne les détails d'une source musicale par son ID.
|
||||
#[cfg(feature = "server")]
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = "/sources/{id}",
|
||||
params(
|
||||
("id" = String, Path, description = "ID de la source")
|
||||
),
|
||||
responses(
|
||||
(status = 200, description = "Informations de la source", body = SourceInfo),
|
||||
(status = 404, description = "Source non trouvée", body = ErrorResponse),
|
||||
),
|
||||
tag = "sources"
|
||||
)]
|
||||
async fn get_source_info(Path(id): Path<String>) -> impl IntoResponse {
|
||||
match get_source(&id).await {
|
||||
Some(source) => {
|
||||
let caps = source.capabilities();
|
||||
let info = SourceInfo {
|
||||
id: source.id().to_string(),
|
||||
name: source.name().to_string(),
|
||||
supports_fifo: source.supports_fifo(),
|
||||
capabilities: caps.into(),
|
||||
};
|
||||
(StatusCode::OK, Json(info)).into_response()
|
||||
}
|
||||
None => (
|
||||
StatusCode::NOT_FOUND,
|
||||
Json(ErrorResponse {
|
||||
error: format!("Source '{}' not found", id),
|
||||
}),
|
||||
)
|
||||
.into_response(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Obtient les capacités d'une source
|
||||
///
|
||||
/// Retourne les capacités détaillées d'une source musicale.
|
||||
#[cfg(feature = "server")]
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = "/sources/{id}/capabilities",
|
||||
params(
|
||||
("id" = String, Path, description = "ID de la source")
|
||||
),
|
||||
responses(
|
||||
(status = 200, description = "Capacités de la source", body = SourceCapabilitiesInfo),
|
||||
(status = 404, description = "Source non trouvée", body = ErrorResponse),
|
||||
),
|
||||
tag = "sources"
|
||||
)]
|
||||
async fn get_source_capabilities(Path(id): Path<String>) -> impl IntoResponse {
|
||||
match get_source(&id).await {
|
||||
Some(source) => {
|
||||
let caps: SourceCapabilitiesInfo = source.capabilities().into();
|
||||
(StatusCode::OK, Json(caps)).into_response()
|
||||
}
|
||||
None => (
|
||||
StatusCode::NOT_FOUND,
|
||||
Json(ErrorResponse {
|
||||
error: format!("Source '{}' not found", id),
|
||||
}),
|
||||
)
|
||||
.into_response(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Obtient les statistiques d'une source
|
||||
///
|
||||
/// Retourne les statistiques d'une source musicale.
|
||||
#[cfg(feature = "server")]
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = "/sources/{id}/statistics",
|
||||
params(
|
||||
("id" = String, Path, description = "ID de la source")
|
||||
),
|
||||
responses(
|
||||
(status = 200, description = "Statistiques de la source", body = SourceStatisticsInfo),
|
||||
(status = 404, description = "Source non trouvée", body = ErrorResponse),
|
||||
(status = 500, description = "Erreur lors de la récupération des statistiques", body = ErrorResponse),
|
||||
),
|
||||
tag = "sources"
|
||||
)]
|
||||
async fn get_source_statistics(Path(id): Path<String>) -> impl IntoResponse {
|
||||
match get_source(&id).await {
|
||||
Some(source) => {
|
||||
match source.statistics().await {
|
||||
Ok(stats) => {
|
||||
let stats_info: SourceStatisticsInfo = stats.into();
|
||||
(StatusCode::OK, Json(stats_info)).into_response()
|
||||
}
|
||||
Err(e) => (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Json(ErrorResponse {
|
||||
error: format!("Failed to get statistics: {}", e),
|
||||
}),
|
||||
)
|
||||
.into_response(),
|
||||
}
|
||||
}
|
||||
None => (
|
||||
StatusCode::NOT_FOUND,
|
||||
Json(ErrorResponse {
|
||||
error: format!("Source '{}' not found", id),
|
||||
}),
|
||||
)
|
||||
.into_response(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Obtient le container racine d'une source
|
||||
///
|
||||
/// Retourne le container racine d'une source musicale.
|
||||
#[cfg(feature = "server")]
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = "/sources/{id}/root",
|
||||
params(
|
||||
("id" = String, Path, description = "ID de la source")
|
||||
),
|
||||
responses(
|
||||
(status = 200, description = "Container racine de la source", body = SourceRootContainer),
|
||||
(status = 404, description = "Source non trouvée", body = ErrorResponse),
|
||||
(status = 500, description = "Erreur lors de la récupération du container", body = ErrorResponse),
|
||||
),
|
||||
tag = "sources"
|
||||
)]
|
||||
async fn get_source_root(Path(id): Path<String>) -> impl IntoResponse {
|
||||
match get_source(&id).await {
|
||||
Some(source) => {
|
||||
match source.root_container().await {
|
||||
Ok(container) => {
|
||||
let root = SourceRootContainer {
|
||||
id: container.id,
|
||||
parent_id: container.parent_id,
|
||||
title: container.title,
|
||||
class: container.class,
|
||||
child_count: container.child_count,
|
||||
};
|
||||
(StatusCode::OK, Json(root)).into_response()
|
||||
}
|
||||
Err(e) => (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Json(ErrorResponse {
|
||||
error: format!("Failed to get root container: {}", e),
|
||||
}),
|
||||
)
|
||||
.into_response(),
|
||||
}
|
||||
}
|
||||
None => (
|
||||
StatusCode::NOT_FOUND,
|
||||
Json(ErrorResponse {
|
||||
error: format!("Source '{}' not found", id),
|
||||
}),
|
||||
)
|
||||
.into_response(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Obtient l'image par défaut d'une source
|
||||
///
|
||||
/// Retourne l'image/logo par défaut d'une source en format WebP.
|
||||
#[cfg(feature = "server")]
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = "/sources/{id}/image",
|
||||
params(
|
||||
("id" = String, Path, description = "ID de la source")
|
||||
),
|
||||
responses(
|
||||
(status = 200, description = "Image de la source", content_type = "image/webp"),
|
||||
(status = 404, description = "Source non trouvée", body = ErrorResponse),
|
||||
),
|
||||
tag = "sources"
|
||||
)]
|
||||
async fn get_source_image(Path(id): Path<String>) -> Response {
|
||||
match get_source(&id).await {
|
||||
Some(source) => {
|
||||
// Copier les données de l'image pour respecter les lifetime requirements
|
||||
let image_data = source.default_image().to_vec();
|
||||
let mime_type = source.default_image_mime_type().to_string();
|
||||
|
||||
(
|
||||
StatusCode::OK,
|
||||
[(header::CONTENT_TYPE, mime_type.as_str())],
|
||||
image_data,
|
||||
)
|
||||
.into_response()
|
||||
}
|
||||
None => (
|
||||
StatusCode::NOT_FOUND,
|
||||
Json(ErrorResponse {
|
||||
error: format!("Source '{}' not found", id),
|
||||
}),
|
||||
)
|
||||
.into_response(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Crée le router pour l'API des sources
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Un `Router` Axum avec toutes les routes de l'API configurées.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```ignore
|
||||
/// use pmosource::api::create_sources_router;
|
||||
/// use axum::Router;
|
||||
///
|
||||
/// let app = Router::new()
|
||||
/// .nest("/api", create_sources_router());
|
||||
/// ```
|
||||
#[cfg(feature = "server")]
|
||||
pub fn create_sources_router() -> Router {
|
||||
Router::new()
|
||||
.route("/sources", get(list_sources))
|
||||
.route("/sources/{id}", get(get_source_info))
|
||||
.route("/sources/{id}/capabilities", get(get_source_capabilities))
|
||||
.route("/sources/{id}/statistics", get(get_source_statistics))
|
||||
.route("/sources/{id}/root", get(get_source_root))
|
||||
.route("/sources/{id}/image", get(get_source_image))
|
||||
}
|
||||
|
||||
/// Structure pour la documentation OpenAPI
|
||||
#[cfg(feature = "server")]
|
||||
#[derive(utoipa::OpenApi)]
|
||||
#[openapi(
|
||||
paths(
|
||||
list_sources,
|
||||
get_source_info,
|
||||
get_source_capabilities,
|
||||
get_source_statistics,
|
||||
get_source_root,
|
||||
get_source_image,
|
||||
),
|
||||
components(
|
||||
schemas(
|
||||
SourceInfo,
|
||||
SourceCapabilitiesInfo,
|
||||
SourceStatisticsInfo,
|
||||
SourcesList,
|
||||
SourceRootContainer,
|
||||
ErrorResponse,
|
||||
)
|
||||
),
|
||||
tags(
|
||||
(name = "sources", description = "API de gestion des sources musicales")
|
||||
)
|
||||
)]
|
||||
pub struct SourcesApiDoc;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn test_api_module_compiles() {
|
||||
// Ce test vérifie simplement que le module compile
|
||||
}
|
||||
}
|
||||
@@ -12,10 +12,28 @@
|
||||
//! - **Cache Integration**: Automatic URI resolution with `pmoaudiocache` and `pmocovers`.
|
||||
//! - **Change Tracking**: `update_id` and `last_change` for UPnP notifications.
|
||||
//! - **Send + Sync**: Ready for async servers.
|
||||
//! - **Server Extension**: Optional `pmoserver` integration with REST API (feature `server`).
|
||||
//!
|
||||
//! ## Usage
|
||||
//!
|
||||
//! ### Basic Usage (implementing a source)
|
||||
//!
|
||||
//! See the [examples/radio_paradise.rs](../examples/radio_paradise.rs) for a complete implementation.
|
||||
//!
|
||||
//! ### Server Integration (feature `server`)
|
||||
//!
|
||||
//! ```rust,ignore
|
||||
//! use pmosource::MusicSourceExt;
|
||||
//! use pmoserver::ServerBuilder;
|
||||
//!
|
||||
//! let mut server = ServerBuilder::new_configured().build();
|
||||
//!
|
||||
//! // Initialiser le système de sources
|
||||
//! server.init_music_sources().await?;
|
||||
//!
|
||||
//! // Enregistrer des sources
|
||||
//! server.register_music_source(Arc::new(my_source)).await;
|
||||
//! ```
|
||||
|
||||
use pmodidl::{Container, Item};
|
||||
use std::fmt::Debug;
|
||||
@@ -886,6 +904,20 @@ pub use async_trait::async_trait;
|
||||
pub use pmodidl;
|
||||
pub use pmoplaylist;
|
||||
|
||||
// Server extension modules (feature-gated)
|
||||
#[cfg(feature = "server")]
|
||||
pub mod pmoserver_ext;
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
pub mod api;
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
mod pmoserver_impl;
|
||||
|
||||
// Re-export server extension trait
|
||||
#[cfg(feature = "server")]
|
||||
pub use pmoserver_ext::MusicSourceExt;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
188
pmosource/src/pmoserver_ext.rs
Normal file
188
pmosource/src/pmoserver_ext.rs
Normal file
@@ -0,0 +1,188 @@
|
||||
//! # Music Source Extension Trait
|
||||
//!
|
||||
//! Ce module définit le trait d'extension [`MusicSourceExt`] qui permet d'ajouter
|
||||
//! facilement la gestion des sources musicales à un serveur `pmoserver::Server`.
|
||||
//!
|
||||
//! ## Architecture
|
||||
//!
|
||||
//! Ce trait suit le pattern d'extension utilisé par les autres crates de l'écosystème
|
||||
//! PMOMusic (`pmocovers`, `pmoaudiocache`, `pmoqobuz`, etc.). Il permet à `pmosource`
|
||||
//! d'étendre `pmoserver::Server` sans que `pmoserver` ne connaisse `pmosource`.
|
||||
//!
|
||||
//! ## Exemple d'utilisation
|
||||
//!
|
||||
//! ```rust,ignore
|
||||
//! use pmosource::{MusicSourceExt, MusicSource};
|
||||
//! use pmoserver::ServerBuilder;
|
||||
//! use std::sync::Arc;
|
||||
//!
|
||||
//! # async fn example() -> anyhow::Result<()> {
|
||||
//! let mut server = ServerBuilder::new_configured().build();
|
||||
//!
|
||||
//! // Initialiser le gestionnaire de sources avec API
|
||||
//! server.init_music_sources().await?;
|
||||
//!
|
||||
//! // Enregistrer une source
|
||||
//! let source: Arc<dyn MusicSource> = Arc::new(MySource::new());
|
||||
//! server.register_music_source(source).await;
|
||||
//!
|
||||
//! // Lister les sources
|
||||
//! let sources = server.list_music_sources().await;
|
||||
//! println!("{} sources registered", sources.len());
|
||||
//!
|
||||
//! server.start().await;
|
||||
//! # Ok(())
|
||||
//! # }
|
||||
//! ```
|
||||
|
||||
use crate::MusicSource;
|
||||
use anyhow::Result;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Extension trait pour ajouter la gestion des sources musicales à un serveur
|
||||
///
|
||||
/// Ce trait étend `pmoserver::Server` avec des fonctionnalités de gestion de sources
|
||||
/// musicales, incluant :
|
||||
/// - Enregistrement de sources implémentant [`MusicSource`]
|
||||
/// - API REST pour lister et gérer les sources
|
||||
/// - Documentation OpenAPI automatique
|
||||
/// - Intégration avec le registre global de sources
|
||||
///
|
||||
/// # Thread Safety
|
||||
///
|
||||
/// Toutes les opérations sont thread-safe et utilisent un registre partagé
|
||||
/// accessible via `Arc<SourceRegistry>`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust,ignore
|
||||
/// use pmosource::MusicSourceExt;
|
||||
/// use pmoserver::ServerBuilder;
|
||||
///
|
||||
/// let mut server = ServerBuilder::new_configured().build();
|
||||
///
|
||||
/// // Initialiser le système de sources (enregistre les routes API)
|
||||
/// server.init_music_sources().await?;
|
||||
///
|
||||
/// // Le serveur est maintenant prêt à accepter des sources
|
||||
/// ```
|
||||
#[cfg_attr(feature = "server", async_trait::async_trait)]
|
||||
pub trait MusicSourceExt {
|
||||
/// Initialise le système de gestion des sources musicales
|
||||
///
|
||||
/// Cette méthode :
|
||||
/// 1. Initialise le registre global de sources
|
||||
/// 2. Enregistre les routes API REST (`/api/sources/*`)
|
||||
/// 3. Configure la documentation OpenAPI
|
||||
///
|
||||
/// Cette méthode doit être appelée avant d'enregistrer des sources.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// `Ok(())` si l'initialisation réussit.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Retourne une erreur si le système de sources est déjà initialisé
|
||||
/// ou si l'enregistrement des routes échoue.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust,ignore
|
||||
/// server.init_music_sources().await?;
|
||||
/// ```
|
||||
#[cfg(feature = "server")]
|
||||
async fn init_music_sources(&mut self) -> Result<()>;
|
||||
|
||||
/// Enregistre une source musicale
|
||||
///
|
||||
/// Ajoute une source au registre global, la rendant disponible pour
|
||||
/// les clients UPnP et l'API REST.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `source` - La source musicale à enregistrer (implémente [`MusicSource`])
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust,ignore
|
||||
/// let source = Arc::new(QobuzSource::new(client, base_url));
|
||||
/// server.register_music_source(source).await;
|
||||
/// ```
|
||||
#[cfg(feature = "server")]
|
||||
async fn register_music_source(&mut self, source: Arc<dyn MusicSource>);
|
||||
|
||||
/// Désenregistre une source musicale par son ID
|
||||
///
|
||||
/// Retire la source du registre global.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `source_id` - L'ID unique de la source à retirer
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// `true` si la source a été trouvée et retirée, `false` sinon.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust,ignore
|
||||
/// if server.unregister_music_source("qobuz").await {
|
||||
/// println!("Qobuz source removed");
|
||||
/// }
|
||||
/// ```
|
||||
#[cfg(feature = "server")]
|
||||
async fn unregister_music_source(&mut self, source_id: &str) -> bool;
|
||||
|
||||
/// Liste toutes les sources enregistrées
|
||||
///
|
||||
/// Retourne une copie de toutes les sources actuellement enregistrées
|
||||
/// dans le registre global.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Un vecteur de `Arc<dyn MusicSource>` contenant toutes les sources.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust,ignore
|
||||
/// let sources = server.list_music_sources().await;
|
||||
/// for source in sources {
|
||||
/// println!("- {} ({})", source.name(), source.id());
|
||||
/// }
|
||||
/// ```
|
||||
#[cfg(feature = "server")]
|
||||
async fn list_music_sources(&self) -> Vec<Arc<dyn MusicSource>>;
|
||||
|
||||
/// Récupère une source spécifique par son ID
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `source_id` - L'ID unique de la source recherchée
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// `Some(Arc<dyn MusicSource>)` si la source existe, `None` sinon.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust,ignore
|
||||
/// if let Some(source) = server.get_music_source("qobuz").await {
|
||||
/// println!("Found: {}", source.name());
|
||||
/// }
|
||||
/// ```
|
||||
#[cfg(feature = "server")]
|
||||
async fn get_music_source(&self, source_id: &str) -> Option<Arc<dyn MusicSource>>;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
// Les tests fonctionnels nécessitent l'implémentation du trait,
|
||||
// voir pmoserver_impl.rs
|
||||
#[test]
|
||||
fn test_trait_exists() {
|
||||
// Ce test vérifie simplement que le trait compile
|
||||
}
|
||||
}
|
||||
116
pmosource/src/pmoserver_impl.rs
Normal file
116
pmosource/src/pmoserver_impl.rs
Normal file
@@ -0,0 +1,116 @@
|
||||
//! # Implémentation du trait MusicSourceExt pour pmoserver::Server
|
||||
//!
|
||||
//! Ce module enrichit `pmoserver::Server` avec les fonctionnalités de gestion
|
||||
//! de sources musicales en implémentant le trait [`MusicSourceExt`](crate::MusicSourceExt).
|
||||
//!
|
||||
//! ## Architecture
|
||||
//!
|
||||
//! `pmosource` étend `pmoserver::Server` sans que `pmoserver` connaisse `pmosource`.
|
||||
//! C'est le pattern d'extension utilisé par tous les crates de l'écosystème PMOMusic.
|
||||
//!
|
||||
//! ## Exemple d'utilisation
|
||||
//!
|
||||
//! ```rust,no_run
|
||||
//! use pmosource::MusicSourceExt;
|
||||
//! use pmoserver::ServerBuilder;
|
||||
//!
|
||||
//! # async fn example() -> anyhow::Result<()> {
|
||||
//! let mut server = ServerBuilder::new("MyApp", "http://localhost:3000", 3000).build();
|
||||
//!
|
||||
//! // Initialiser le système de sources (enregistre l'API)
|
||||
//! server.init_music_sources().await?;
|
||||
//!
|
||||
//! // Le trait MusicSourceExt est automatiquement disponible
|
||||
//! let source = Arc::new(MySource::new());
|
||||
//! server.register_music_source(source).await;
|
||||
//!
|
||||
//! server.start().await;
|
||||
//! # Ok(())
|
||||
//! # }
|
||||
//! ```
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
use crate::api::{register_source, unregister_source, list_all_sources, get_source, create_sources_router, SourcesApiDoc};
|
||||
#[cfg(feature = "server")]
|
||||
use crate::pmoserver_ext::MusicSourceExt;
|
||||
#[cfg(feature = "server")]
|
||||
use crate::MusicSource;
|
||||
#[cfg(feature = "server")]
|
||||
use anyhow::Result;
|
||||
#[cfg(feature = "server")]
|
||||
use pmoserver::Server;
|
||||
#[cfg(feature = "server")]
|
||||
use std::sync::Arc;
|
||||
#[cfg(feature = "server")]
|
||||
use tracing::info;
|
||||
#[cfg(feature = "server")]
|
||||
use utoipa::OpenApi;
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
#[async_trait::async_trait]
|
||||
impl MusicSourceExt for Server {
|
||||
async fn init_music_sources(&mut self) -> Result<()> {
|
||||
info!("Initializing music sources management system...");
|
||||
|
||||
// Créer le router pour l'API des sources
|
||||
let router = create_sources_router();
|
||||
|
||||
// Créer la documentation OpenAPI
|
||||
let openapi = SourcesApiDoc::openapi();
|
||||
|
||||
// Enregistrer l'API avec Swagger UI
|
||||
// Le router sera nesté automatiquement sous /api/sources par add_openapi
|
||||
// Routes finales: /api/sources, /api/sources/{id}, etc.
|
||||
// Swagger UI sera disponible à /swagger-ui/sources
|
||||
self.add_openapi(router, openapi, "sources").await;
|
||||
|
||||
info!("✅ Music sources API registered at /api/sources");
|
||||
info!(" Swagger UI available at /swagger-ui/sources");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn register_music_source(&mut self, source: Arc<dyn MusicSource>) {
|
||||
let source_id = source.id().to_string();
|
||||
let source_name = source.name().to_string();
|
||||
|
||||
info!("Registering music source: {} ({})", source_name, source_id);
|
||||
|
||||
register_source(source).await;
|
||||
|
||||
info!("✅ Source '{}' registered successfully", source_name);
|
||||
}
|
||||
|
||||
async fn unregister_music_source(&mut self, source_id: &str) -> bool {
|
||||
info!("Unregistering music source: {}", source_id);
|
||||
|
||||
let result = unregister_source(source_id).await;
|
||||
|
||||
if result {
|
||||
info!("✅ Source '{}' unregistered successfully", source_id);
|
||||
} else {
|
||||
tracing::warn!("⚠️ Source '{}' not found", source_id);
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
async fn list_music_sources(&self) -> Vec<Arc<dyn MusicSource>> {
|
||||
list_all_sources().await
|
||||
}
|
||||
|
||||
async fn get_music_source(&self, source_id: &str) -> Option<Arc<dyn MusicSource>> {
|
||||
get_source(source_id).await
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_trait_implemented() {
|
||||
// Ce test vérifie simplement que le trait est bien implémenté
|
||||
// Les tests fonctionnels nécessiteraient un serveur et des sources réelles
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user