Skip to content
Snippets Groups Projects
Commit b8c13f0a authored by Falk Rehse's avatar Falk Rehse
Browse files

Provide websockets connection params [skip CI]

on /websockets
parent 826909f8
Branches
No related tags found
No related merge requests found
Pipeline #118869 skipped
...@@ -6,7 +6,7 @@ use uuid::Uuid; ...@@ -6,7 +6,7 @@ use uuid::Uuid;
use crate::song::ApiSong; use crate::song::ApiSong;
use crate::song::ApiSongState; use crate::song::ApiSongState;
use crate::song::Song; 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>>) -> () { pub fn handle_client(websocket: &mut WebSocket<TcpStream>, playlist: &Mutable<Vec<Song>>) -> () {
println!("New client connected!"); println!("New client connected!");
...@@ -137,62 +137,140 @@ fn generate_playlist_api(playlist: &Vec<Song>) -> Vec<ApiSong> { ...@@ -137,62 +137,140 @@ fn generate_playlist_api(playlist: &Vec<Song>) -> Vec<ApiSong> {
thumbnail_url: None, thumbnail_url: None,
state: ApiSongState::InfoFetching, 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(), uuid: uuid.to_string(),
url: url.to_string(), url: url.to_string(),
title: title.to_owned(), title: title.to_owned(),
artist: artist.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(), length: length.to_owned(),
thumbnail_url: thumbnail_file.to_owned(), thumbnail_url: thumbnail_file.to_owned(),
state: ApiSongState::InfoFetched, 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(), uuid: uuid.to_string(),
url: url.to_string(), url: url.to_string(),
title: title.to_owned(), title: title.to_owned(),
artist: artist.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(), length: length.to_owned(),
thumbnail_url: thumbnail_file.to_owned(), thumbnail_url: thumbnail_file.to_owned(),
state: ApiSongState::Downloading, 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(), uuid: uuid.to_string(),
url: url.to_string(), url: url.to_string(),
title: title.to_owned(), title: title.to_owned(),
artist: artist.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(), length: length.to_owned(),
thumbnail_url: thumbnail_file.to_owned(), thumbnail_url: thumbnail_file.to_owned(),
state: ApiSongState::Downloaded, 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(), uuid: uuid.to_string(),
url: url.to_string(), url: url.to_string(),
title: title.to_owned(), title: title.to_owned(),
artist: artist.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(), length: length.to_owned(),
thumbnail_url: thumbnail_file.to_owned(), thumbnail_url: thumbnail_file.to_owned(),
state: ApiSongState::Converting, 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(), uuid: uuid.to_string(),
url: url.to_string(), url: url.to_string(),
title: title.to_owned(), title: title.to_owned(),
artist: artist.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(), length: length.to_owned(),
thumbnail_url: thumbnail_file.to_owned(), thumbnail_url: thumbnail_file.to_owned(),
state: ApiSongState::Ready, 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(), uuid: uuid.to_string(),
url: url.to_string(), url: url.to_string(),
title: title.to_owned(), title: title.to_owned(),
artist: artist.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(), length: length.to_owned(),
thumbnail_url: thumbnail_file.to_owned(), thumbnail_url: thumbnail_file.to_owned(),
state: ApiSongState::Playing, state: ApiSongState::Playing,
......
use axum::handler::HandlerWithoutStateExt; use axum::handler::HandlerWithoutStateExt;
use axum::http::StatusCode; use axum::http::StatusCode;
use axum::response::IntoResponse; use axum::response::IntoResponse;
use axum::routing::get_service; use axum::routing::{get, get_service};
use axum::Router; use axum::Router;
// use futures_signals::signal::Mutable; // use futures_signals::signal::Mutable;
use std::io; use std::io;
use std::sync::Arc;
use tower::util::ServiceExt; use tower::util::ServiceExt;
use tower_http::services::ServeDir; use tower_http::services::ServeDir;
use crate::websockets_connection_params::WebsocketsConnectionParams;
// use crate::song::Song; // 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); println!("Listening for http connections on {}!", http_bind_address);
async fn handle_404() -> (StatusCode, &'static str) { async fn handle_404() -> (StatusCode, &'static str) {
...@@ -28,6 +34,16 @@ pub async fn handle_http(http_bind_address: &str, /* playlist: &Mutable<Vec<Song ...@@ -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 serve_thumbnails = get_service(serve_thumbnails).handle_error(handle_error);
let app = Router::new() 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("/", serve_frontend)
.nest_service("/thumbnail", serve_thumbnails); .nest_service("/thumbnail", serve_thumbnails);
......
...@@ -5,7 +5,8 @@ mod player; ...@@ -5,7 +5,8 @@ mod player;
mod song; mod song;
mod util; mod util;
mod websockets; mod websockets;
mod websocket_command; mod websockets_command;
mod websockets_connection_params;
use futures_signals::signal::Mutable; use futures_signals::signal::Mutable;
use std::env; use std::env;
...@@ -22,21 +23,30 @@ async fn main() { ...@@ -22,21 +23,30 @@ async fn main() {
let playlist_websockets = Mutable::clone(&playlist); let playlist_websockets = Mutable::clone(&playlist);
let playlist_downloader = 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_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_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_bind_address = format!("{}:{}", http_bind_host, http_bind_port);
let http_task = http::handle_http(&http_bind_address /* , &playlist_http */); let http_task = http::handle_http(
&http_bind_address,
let player_token = client_token_clone,
Arc::new(env::var("SPOCCIFY_PLAYER_TOKEN").expect("No player token provided!")); websockets_url, /* , &playlist_http */
let client_token = );
Arc::new(env::var("SPOCCIFY_CLIENT_TOKEN").expect("No client token provided!"));
let websockets_bind_host = let websockets_bind_host =
env::var("SPOCCIFY_WEBSOCKETS_BIND_HOST").unwrap_or("127.0.0.1".to_owned()); 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); let websockets_bind_address = format!("{}:{}", websockets_bind_host, websockets_bind_port);
......
File moved
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct WebsocketsConnectionParams {
pub url: String,
pub token: String,
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment