Compare commits
4 Commits
v0.1.0-dev
...
v0.1.0-dev
Author | SHA1 | Date | |
---|---|---|---|
2f11ff9e89 | |||
5ced62ec7a | |||
817ea9fed2 | |||
ce1d522814 |
2
.env.docker.sample
Normal file
2
.env.docker.sample
Normal file
@@ -0,0 +1,2 @@
|
||||
ROOT_DIRECTORY=/home/songparser/mydata
|
||||
ICARUS_BASE_API_URL=http://localhost:3000
|
2
.env.sample
Normal file
2
.env.sample
Normal file
@@ -0,0 +1,2 @@
|
||||
ROOT_DIRECTORY=/home/songparser/mydata
|
||||
ICARUS_BASE_API_URL=http://localhost:3000
|
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,2 +1,2 @@
|
||||
/target
|
||||
Cargo.lock
|
||||
.env
|
||||
|
1799
Cargo.lock
generated
Normal file
1799
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -6,4 +6,9 @@ rust-version = "1.86"
|
||||
|
||||
[dependencies]
|
||||
tokio = { version = "1.44.1", features = ["full"] }
|
||||
reqwest = { version = "0.12.19", features = ["json"] }
|
||||
serde = { version = "1.0.218", features = ["derive"] }
|
||||
serde_json = { version = "1.0.139" }
|
||||
uuid = { version = "1.16.0", features = ["v4", "serde"] }
|
||||
dotenvy = { version = "0.15.7" }
|
||||
icarus_models = { git = "ssh://git@git.kundeng.us/phoenix/icarus_models.git", tag = "v0.4.3" }
|
||||
|
101
src/main.rs
101
src/main.rs
@@ -1,38 +1,81 @@
|
||||
use std::error::Error;
|
||||
use tokio::io::AsyncReadExt;
|
||||
use tokio::net::{TcpListener, TcpStream};
|
||||
use tokio::spawn;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
let listener = TcpListener::bind("127.0.0.1:8080").await?;
|
||||
println!("API calling service listening on 127.0.0.1:8080");
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let app_base_url = get_icarus_url().await;
|
||||
|
||||
loop {
|
||||
let (stream, addr) = listener.accept().await?;
|
||||
println!("Accepted connection from: {}", addr);
|
||||
println!("Base URL: {}", app_base_url);
|
||||
|
||||
spawn(async move {
|
||||
if let Err(e) = handle_connection(stream).await {
|
||||
eprintln!("Error handling connection from {}: {}", addr, e);
|
||||
match api::fetch_next_queue_item(&app_base_url).await {
|
||||
Ok(response) => {
|
||||
match response
|
||||
.json::<responses::fetch_next_queue_item::SongQueueItem>()
|
||||
.await
|
||||
{
|
||||
Ok(song_queue_item) => {
|
||||
if !song_queue_item.data.is_empty() {
|
||||
println!("Song queue item: {:?}", song_queue_item);
|
||||
|
||||
// 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
|
||||
} else {
|
||||
println!("No data to fetch");
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!("Error: {:?}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
Err(e) => eprintln!("API call failed: {}", e),
|
||||
}
|
||||
|
||||
let request_data = String::from_utf8_lossy(&buffer[..n]).trim().to_string();
|
||||
println!("Received request: {}", request_data);
|
||||
println!("Sleeping");
|
||||
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
mod responses {
|
||||
pub mod fetch_next_queue_item {
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct QueueItem {
|
||||
pub id: uuid::Uuid,
|
||||
pub filename: String,
|
||||
pub status: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct SongQueueItem {
|
||||
pub message: String,
|
||||
pub data: Vec<QueueItem>,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mod api {
|
||||
pub async fn fetch_next_queue_item(
|
||||
base_url: &String,
|
||||
) -> Result<reqwest::Response, reqwest::Error> {
|
||||
let client = reqwest::Client::new();
|
||||
let fetch_endpoint = String::from("api/v2/song/queue/next");
|
||||
let api_url = format!("{}/{}", base_url, fetch_endpoint);
|
||||
client.get(api_url).send().await
|
||||
}
|
||||
}
|
||||
|
||||
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