trying to create an appstate
This commit is contained in:
parent
12ea20f270
commit
a2a767c42f
66
src/main.rs
66
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<Environment<'static>>;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AppState {
|
||||
pub engine: AppEngine,
|
||||
pub clips: Vec<soundclips::SoundClip>,
|
||||
}
|
||||
|
||||
impl FromRef<AppState> 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<AppState>) -> impl axum::response::IntoResponse {
|
||||
RenderHtml(Key("index.html".to_owned()), engine, ())
|
||||
}
|
||||
|
||||
async fn root_handler() -> axum::response::Html<&'static str> {
|
||||
let html = r#"
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Play</title>
|
||||
<script>
|
||||
function play() {
|
||||
fetch("/play/1");
|
||||
async fn play_handler(
|
||||
axum::extract::Path(hash): axum::extract::Path<String>,
|
||||
state: State<AppState>,
|
||||
) -> String {
|
||||
if let Some(clip) = state
|
||||
.clips
|
||||
.clone()
|
||||
.into_iter()
|
||||
.find(|s| hash == s.hash().to_string())
|
||||
{
|
||||
play_clip(clip.hash());
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<button onclick="play()">Play</button>
|
||||
</body>
|
||||
</html>
|
||||
"#;
|
||||
html.into()
|
||||
}
|
||||
|
||||
async fn play_handler(axum::extract::Path(x): axum::extract::Path<u32>) -> String {
|
||||
play_liv();
|
||||
format!("You sent: {}", x)
|
||||
"".to_owned()
|
||||
}
|
||||
|
@ -40,7 +40,9 @@ fn find_virtual_audio_cable() -> Option<Device> {
|
||||
return None;
|
||||
}
|
||||
|
||||
pub fn audio_thread() -> Sender<Command> {
|
||||
pub type AudioThread = Sender<Command>;
|
||||
|
||||
pub fn audio_thread() -> AudioThread {
|
||||
let (tx, rx) = mpsc::channel();
|
||||
|
||||
thread::spawn(move || {
|
||||
|
Loading…
Reference in New Issue
Block a user