From a2a767c42f71db36fba4953f980bd48bbea802ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabor=20K=C3=B6rber?= Date: Tue, 18 Jul 2023 23:49:32 +0200 Subject: [PATCH] trying to create an appstate --- src/main.rs | 68 +++++++++++++++++++++++++++++++-------------------- src/vbplay.rs | 4 ++- 2 files changed, 45 insertions(+), 27 deletions(-) diff --git a/src/main.rs b/src/main.rs index 13a1a25..16f4fad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,10 @@ +use axum::extract::{FromRef, State}; use axum::{routing, Router}; use axum_template::{engine::Engine, Key, RenderHtml}; use minijinja::{path_loader, Environment}; use std::net::SocketAddr; +use std::sync::Arc; use std::thread; use std::time::Duration; @@ -11,6 +13,18 @@ mod vbplay; type AppEngine = Engine>; +#[derive(Clone)] +pub struct AppState { + pub engine: AppEngine, + pub clips: Vec, +} + +impl FromRef for AppEngine { + fn from_ref(state: &AppState) -> Self { + state.engine.clone() + } +} + fn play_liv() { let file_name = "sounds/example/liv_aaaaaaaa.mp3"; let tx = vbplay::audio_thread(); @@ -22,10 +36,22 @@ fn play_liv() { tx.send(vbplay::Command::Stop).unwrap(); } +fn play_clip(file_name: &str) { + let tx = vbplay::audio_thread(); + tx.send(vbplay::Command::Play(file_name.into())).unwrap(); +} + #[tokio::main] async fn main() { let mut jinja = Environment::new(); jinja.set_loader(path_loader("templates")); + let template_engine = Engine::from(jinja); + + let state = AppState { + engine: template_engine, + clips: soundclips::scan_directory_for_clips("E:/sounds/soundboard", &["mp3"]) + .expect("No Soundclips found."), + }; // Set the address to run our application on let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); @@ -33,8 +59,8 @@ async fn main() { // Build our application with a route let app = Router::new() .route("/", routing::get(home)) - .route("/play/:x", routing::get(play_handler)) - .with_state(Engine::from(jinja)); + .route("/play/:hash", routing::get(play_handler)) + .with_state(state); println!("listening on {}", addr); @@ -45,31 +71,21 @@ async fn main() { .unwrap(); } -async fn home(engine: AppEngine) -> impl axum::response::IntoResponse { +async fn home(engine: AppEngine, state: State) -> impl axum::response::IntoResponse { RenderHtml(Key("index.html".to_owned()), engine, ()) } -async fn root_handler() -> axum::response::Html<&'static str> { - let html = r#" - - - - Play - - - - - - - "#; - html.into() -} - -async fn play_handler(axum::extract::Path(x): axum::extract::Path) -> String { - play_liv(); - format!("You sent: {}", x) +async fn play_handler( + axum::extract::Path(hash): axum::extract::Path, + state: State, +) -> String { + if let Some(clip) = state + .clips + .clone() + .into_iter() + .find(|s| hash == s.hash().to_string()) + { + play_clip(clip.hash()); + } + "".to_owned() } diff --git a/src/vbplay.rs b/src/vbplay.rs index 30f1c90..f789f5b 100644 --- a/src/vbplay.rs +++ b/src/vbplay.rs @@ -40,7 +40,9 @@ fn find_virtual_audio_cable() -> Option { return None; } -pub fn audio_thread() -> Sender { +pub type AudioThread = Sender; + +pub fn audio_thread() -> AudioThread { let (tx, rx) = mpsc::channel(); thread::spawn(move || {