diff --git a/src/client.rs b/src/client.rs index bfa1b2ac4196528d2fb9fca0b0a7a583357f25f9..9d1aa46321d52114d916b1f4181c81545413b741 100644 --- a/src/client.rs +++ b/src/client.rs @@ -6,7 +6,7 @@ use uuid::Uuid; use crate::song::ApiSong; use crate::song::ApiSongState; use crate::song::Song; -use crate::websocket_command::Command; +use crate::websockets_command::Command; pub fn handle_client(websocket: &mut WebSocket<TcpStream>, playlist: &Mutable<Vec<Song>>) -> () { println!("New client connected!"); @@ -137,62 +137,140 @@ fn generate_playlist_api(playlist: &Vec<Song>) -> Vec<ApiSong> { thumbnail_url: None, state: ApiSongState::InfoFetching, }, - Song::InfoFetched { uuid, url, title, artist, artist_url, length, thumbnail_file, .. } => ApiSong { + Song::InfoFetched { + uuid, + url, + title, + artist, + artist_url, + length, + thumbnail_file, + .. + } => ApiSong { uuid: uuid.to_string(), url: url.to_string(), title: title.to_owned(), artist: artist.to_owned(), - artist_url: if let Some(artist_url) = artist_url { Some(artist_url.to_string()) } else { None }, + artist_url: if let Some(artist_url) = artist_url { + Some(artist_url.to_string()) + } else { + None + }, length: length.to_owned(), thumbnail_url: thumbnail_file.to_owned(), state: ApiSongState::InfoFetched, }, - Song::Downloading { uuid, url, title, artist, artist_url, length, thumbnail_file, .. } => ApiSong { + Song::Downloading { + uuid, + url, + title, + artist, + artist_url, + length, + thumbnail_file, + .. + } => ApiSong { uuid: uuid.to_string(), url: url.to_string(), title: title.to_owned(), artist: artist.to_owned(), - artist_url: if let Some(artist_url) = artist_url { Some(artist_url.to_string()) } else { None }, + artist_url: if let Some(artist_url) = artist_url { + Some(artist_url.to_string()) + } else { + None + }, length: length.to_owned(), thumbnail_url: thumbnail_file.to_owned(), state: ApiSongState::Downloading, }, - Song::Downloaded { uuid, url, title, artist, artist_url, length, thumbnail_file, .. } => ApiSong { + Song::Downloaded { + uuid, + url, + title, + artist, + artist_url, + length, + thumbnail_file, + .. + } => ApiSong { uuid: uuid.to_string(), url: url.to_string(), title: title.to_owned(), artist: artist.to_owned(), - artist_url: if let Some(artist_url) = artist_url { Some(artist_url.to_string()) } else { None }, + artist_url: if let Some(artist_url) = artist_url { + Some(artist_url.to_string()) + } else { + None + }, length: length.to_owned(), thumbnail_url: thumbnail_file.to_owned(), state: ApiSongState::Downloaded, }, - Song::Converting { uuid, url, title, artist, artist_url, length, thumbnail_file, .. } => ApiSong { + Song::Converting { + uuid, + url, + title, + artist, + artist_url, + length, + thumbnail_file, + .. + } => ApiSong { uuid: uuid.to_string(), url: url.to_string(), title: title.to_owned(), artist: artist.to_owned(), - artist_url: if let Some(artist_url) = artist_url { Some(artist_url.to_string()) } else { None }, + artist_url: if let Some(artist_url) = artist_url { + Some(artist_url.to_string()) + } else { + None + }, length: length.to_owned(), thumbnail_url: thumbnail_file.to_owned(), state: ApiSongState::Converting, }, - Song::Ready { uuid, url, title, artist, artist_url, length, thumbnail_file, .. } => ApiSong { + Song::Ready { + uuid, + url, + title, + artist, + artist_url, + length, + thumbnail_file, + .. + } => ApiSong { uuid: uuid.to_string(), url: url.to_string(), title: title.to_owned(), artist: artist.to_owned(), - artist_url: if let Some(artist_url) = artist_url { Some(artist_url.to_string()) } else { None }, + artist_url: if let Some(artist_url) = artist_url { + Some(artist_url.to_string()) + } else { + None + }, length: length.to_owned(), thumbnail_url: thumbnail_file.to_owned(), state: ApiSongState::Ready, }, - Song::Playing { uuid, url, title, artist, artist_url, length, thumbnail_file, .. } => ApiSong { + Song::Playing { + uuid, + url, + title, + artist, + artist_url, + length, + thumbnail_file, + .. + } => ApiSong { uuid: uuid.to_string(), url: url.to_string(), title: title.to_owned(), artist: artist.to_owned(), - artist_url: if let Some(artist_url) = artist_url { Some(artist_url.to_string()) } else { None }, + artist_url: if let Some(artist_url) = artist_url { + Some(artist_url.to_string()) + } else { + None + }, length: length.to_owned(), thumbnail_url: thumbnail_file.to_owned(), state: ApiSongState::Playing, diff --git a/src/http.rs b/src/http.rs index 244cb0ce5d50a6e78b695183a948962a8b9ecf72..3338240c4deb00c641f596d623e377fd89a225af 100644 --- a/src/http.rs +++ b/src/http.rs @@ -1,16 +1,22 @@ use axum::handler::HandlerWithoutStateExt; use axum::http::StatusCode; use axum::response::IntoResponse; -use axum::routing::get_service; +use axum::routing::{get, get_service}; use axum::Router; // use futures_signals::signal::Mutable; use std::io; +use std::sync::Arc; use tower::util::ServiceExt; use tower_http::services::ServeDir; +use crate::websockets_connection_params::WebsocketsConnectionParams; // use crate::song::Song; -pub async fn handle_http(http_bind_address: &str, /* playlist: &Mutable<Vec<Song>> */) { +pub async fn handle_http( + http_bind_address: &str, + client_token: Arc<String>, + websockets_url: Arc<String>, /* playlist: &Mutable<Vec<Song>> */ +) { println!("Listening for http connections on {}!", http_bind_address); async fn handle_404() -> (StatusCode, &'static str) { @@ -28,6 +34,16 @@ pub async fn handle_http(http_bind_address: &str, /* playlist: &Mutable<Vec<Song let serve_thumbnails = get_service(serve_thumbnails).handle_error(handle_error); let app = Router::new() + .route( + "/websockets", + get(|| async move { + serde_json::to_string(&WebsocketsConnectionParams { + url: websockets_url.to_string(), + token: client_token.to_string(), + }) + .unwrap() + }), + ) .nest_service("/", serve_frontend) .nest_service("/thumbnail", serve_thumbnails); diff --git a/src/main.rs b/src/main.rs index 5ae06ecee25c94b3ae0fb32eaa52d518cb6457f0..61e93d1744643b2a07f3b7cdbefd2d9fa9bac128 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,8 @@ mod player; mod song; mod util; mod websockets; -mod websocket_command; +mod websockets_command; +mod websockets_connection_params; use futures_signals::signal::Mutable; use std::env; @@ -22,21 +23,30 @@ async fn main() { let playlist_websockets = Mutable::clone(&playlist); let playlist_downloader = Mutable::clone(&playlist); + let player_token = + Arc::new(env::var("SPOCCIFY_PLAYER_TOKEN").expect("No player token provided!")); + let client_token = + Arc::new(env::var("SPOCCIFY_CLIENT_TOKEN").expect("No client token provided!")); + let client_token_clone = Arc::clone(&client_token); + + let websockets_url = + Arc::new(env::var("SPOCCIFY_WEBSOCKETS_URL").unwrap_or("ws://localhost:9001".to_owned())); + let http_bind_host = env::var("SPOCCIFY_HTTP_BIND_HOST").unwrap_or("127.0.0.1".to_owned()); let http_bind_port = env::var("SPOCCIFY_HTTP_BIND_PORT").unwrap_or("9000".to_owned()); let http_bind_address = format!("{}:{}", http_bind_host, http_bind_port); - let http_task = http::handle_http(&http_bind_address /* , &playlist_http */); - - let player_token = - Arc::new(env::var("SPOCCIFY_PLAYER_TOKEN").expect("No player token provided!")); - let client_token = - Arc::new(env::var("SPOCCIFY_CLIENT_TOKEN").expect("No client token provided!")); + let http_task = http::handle_http( + &http_bind_address, + client_token_clone, + websockets_url, /* , &playlist_http */ + ); let websockets_bind_host = env::var("SPOCCIFY_WEBSOCKETS_BIND_HOST").unwrap_or("127.0.0.1".to_owned()); - let websockets_bind_port = env::var("SPOCCIFY_WEBSOCKETS_BIND_PORT").unwrap_or("9001".to_owned()); + let websockets_bind_port = + env::var("SPOCCIFY_WEBSOCKETS_BIND_PORT").unwrap_or("9001".to_owned()); let websockets_bind_address = format!("{}:{}", websockets_bind_host, websockets_bind_port); diff --git a/src/websocket_command.rs b/src/websockets_command.rs similarity index 100% rename from src/websocket_command.rs rename to src/websockets_command.rs diff --git a/src/websockets_connection_params.rs b/src/websockets_connection_params.rs new file mode 100644 index 0000000000000000000000000000000000000000..80b016b163e7999e1c3fb0ed0a265ffc064ae97f --- /dev/null +++ b/src/websockets_connection_params.rs @@ -0,0 +1,7 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize)] +pub struct WebsocketsConnectionParams { + pub url: String, + pub token: String, +}