Modify application structure #19
1167
Cargo.lock
generated
1167
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -6,4 +6,6 @@ rust-version = "1.86"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
tokio = { version = "1.44.1", features = ["full"] }
|
tokio = { version = "1.44.1", features = ["full"] }
|
||||||
|
reqwest = { version = "0.12.19" }
|
||||||
|
dotenvy = { version = "0.15.7" }
|
||||||
icarus_models = { git = "ssh://git@git.kundeng.us/phoenix/icarus_models.git", tag = "v0.4.3" }
|
icarus_models = { git = "ssh://git@git.kundeng.us/phoenix/icarus_models.git", tag = "v0.4.3" }
|
||||||
|
67
src/main.rs
67
src/main.rs
@@ -1,38 +1,49 @@
|
|||||||
use std::error::Error;
|
// use std::error::Error;
|
||||||
use tokio::io::AsyncReadExt;
|
// use tokio::io::AsyncReadExt;
|
||||||
use tokio::net::{TcpListener, TcpStream};
|
// use tokio::net::{TcpListener, TcpStream};
|
||||||
use tokio::spawn;
|
// use tokio::spawn;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn Error>> {
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let listener = TcpListener::bind("127.0.0.1:8080").await?;
|
let client = reqwest::Client::new();
|
||||||
println!("API calling service listening on 127.0.0.1:8080");
|
let app_base_url = get_icarus_url().await;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let (stream, addr) = listener.accept().await?;
|
println!("Base URL: {}", app_base_url);
|
||||||
println!("Accepted connection from: {}", addr);
|
|
||||||
|
|
||||||
spawn(async move {
|
// TODO: Update the api/v2/song/queue/next endpoint to only retrieve queued song that is
|
||||||
if let Err(e) = handle_connection(stream).await {
|
// ready to be processed. Make necessary changes to other endpoints
|
||||||
eprintln!("Error handling connection from {}: {}", addr, e);
|
|
||||||
|
let api_url = format!("{}/api/v2/song/queue/next", app_base_url);
|
||||||
|
|
||||||
|
match client.get(api_url).send().await {
|
||||||
|
Ok(response) => {
|
||||||
|
let body = response.text().await?;
|
||||||
|
println!("API response: {}", body);
|
||||||
|
// Process data here...
|
||||||
|
|
||||||
|
// TODO: Parse the response body to a struct
|
||||||
|
// TODO: Get queued song data
|
||||||
|
// TODO: Get queued song's metadata
|
||||||
|
// TODO: Get queued coverart
|
||||||
|
// TODO: Get queued coverart's data
|
||||||
|
// TODO: Apply metadata to the queued song
|
||||||
|
// TODO: Update the queued song with the updated queued song
|
||||||
|
// TODO: Create song
|
||||||
|
// TODO: Create coverart
|
||||||
|
// TODO: Wipe data from queued song
|
||||||
|
// TODO: Wipe data from queued coverart
|
||||||
}
|
}
|
||||||
});
|
Err(e) => eprintln!("API call failed: {}", e),
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn handle_connection(mut stream: TcpStream) -> Result<(), Box<dyn Error + Send + Sync>> {
|
|
||||||
let mut buffer = [0; 1024];
|
|
||||||
|
|
||||||
loop {
|
|
||||||
let n = stream.read(&mut buffer).await?;
|
|
||||||
|
|
||||||
if n == 0 {
|
|
||||||
break; // Connection closed
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let request_data = String::from_utf8_lossy(&buffer[..n]).trim().to_string();
|
println!("Sleeping");
|
||||||
println!("Received request: {}", request_data);
|
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
|
||||||
}
|
}
|
||||||
|
// Ok(())
|
||||||
Ok(())
|
}
|
||||||
|
|
||||||
|
async fn get_icarus_url() -> String {
|
||||||
|
dotenvy::dotenv().ok();
|
||||||
|
std::env::var("ICARUS_BASE_API_URL").expect("Could not find url")
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user