trying to create an appstate
This commit is contained in:
parent
12ea20f270
commit
a2a767c42f
68
src/main.rs
68
src/main.rs
@ -1,8 +1,10 @@
|
|||||||
|
use axum::extract::{FromRef, State};
|
||||||
use axum::{routing, Router};
|
use axum::{routing, Router};
|
||||||
use axum_template::{engine::Engine, Key, RenderHtml};
|
use axum_template::{engine::Engine, Key, RenderHtml};
|
||||||
use minijinja::{path_loader, Environment};
|
use minijinja::{path_loader, Environment};
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
@ -11,6 +13,18 @@ mod vbplay;
|
|||||||
|
|
||||||
type AppEngine = Engine<Environment<'static>>;
|
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() {
|
fn play_liv() {
|
||||||
let file_name = "sounds/example/liv_aaaaaaaa.mp3";
|
let file_name = "sounds/example/liv_aaaaaaaa.mp3";
|
||||||
let tx = vbplay::audio_thread();
|
let tx = vbplay::audio_thread();
|
||||||
@ -22,10 +36,22 @@ fn play_liv() {
|
|||||||
tx.send(vbplay::Command::Stop).unwrap();
|
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]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let mut jinja = Environment::new();
|
let mut jinja = Environment::new();
|
||||||
jinja.set_loader(path_loader("templates"));
|
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
|
// Set the address to run our application on
|
||||||
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
|
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
|
||||||
@ -33,8 +59,8 @@ async fn main() {
|
|||||||
// Build our application with a route
|
// Build our application with a route
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
.route("/", routing::get(home))
|
.route("/", routing::get(home))
|
||||||
.route("/play/:x", routing::get(play_handler))
|
.route("/play/:hash", routing::get(play_handler))
|
||||||
.with_state(Engine::from(jinja));
|
.with_state(state);
|
||||||
|
|
||||||
println!("listening on {}", addr);
|
println!("listening on {}", addr);
|
||||||
|
|
||||||
@ -45,31 +71,21 @@ async fn main() {
|
|||||||
.unwrap();
|
.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, ())
|
RenderHtml(Key("index.html".to_owned()), engine, ())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn root_handler() -> axum::response::Html<&'static str> {
|
async fn play_handler(
|
||||||
let html = r#"
|
axum::extract::Path(hash): axum::extract::Path<String>,
|
||||||
<!DOCTYPE html>
|
state: State<AppState>,
|
||||||
<html>
|
) -> String {
|
||||||
<head>
|
if let Some(clip) = state
|
||||||
<title>Play</title>
|
.clips
|
||||||
<script>
|
.clone()
|
||||||
function play() {
|
.into_iter()
|
||||||
fetch("/play/1");
|
.find(|s| hash == s.hash().to_string())
|
||||||
}
|
{
|
||||||
</script>
|
play_clip(clip.hash());
|
||||||
</head>
|
}
|
||||||
<body>
|
"".to_owned()
|
||||||
<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)
|
|
||||||
}
|
}
|
||||||
|
@ -40,7 +40,9 @@ fn find_virtual_audio_cable() -> Option<Device> {
|
|||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn audio_thread() -> Sender<Command> {
|
pub type AudioThread = Sender<Command>;
|
||||||
|
|
||||||
|
pub fn audio_thread() -> AudioThread {
|
||||||
let (tx, rx) = mpsc::channel();
|
let (tx, rx) = mpsc::channel();
|
||||||
|
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
|
Loading…
Reference in New Issue
Block a user