From 36efc0da0ad67b572b42f7272f677ea5cbad4701 Mon Sep 17 00:00:00 2001 From: devdesk Date: Tue, 16 Dec 2025 21:59:17 +0200 Subject: [PATCH] add missing src/main.rs --- src/main.rs | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/main.rs diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..8c7fa4a --- /dev/null +++ b/src/main.rs @@ -0,0 +1,71 @@ +pub(crate) mod offline; +pub(crate) mod stream; + +use std::sync::{Arc, Mutex}; + +use stream::{initialize, start_stream_thread, Streamer}; + +use axum::{ + extract::State, + http::StatusCode, + routing::{get, post}, + Json, Router, +}; +use serde::{Deserialize, Serialize}; + +#[derive(Clone)] +struct AppState { + streamer: Arc>, +} + +#[tokio::main] +async fn main() { + // initialize tracing + tracing_subscriber::fmt::init(); + + println!("creating streamer"); + let streamer = initialize(); + println!("creating streamer thread"); + start_stream_thread(streamer.clone()); + let state = AppState { streamer }; + + println!("starting web interface"); + // build our application with a route + let app = Router::new() + // `GET /` goes to `root` + .route("/", get(root)) + // `POST /users` goes to `set_cutoff` + .route("/cutoff", post(set_cutoff)) + .with_state(state); + + // run our app with hyper, listening globally on port 3000 + let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); + axum::serve(listener, app).await.unwrap(); +} + +// basic handler that responds with a static string +async fn root() -> &'static str { + "Hello, TAMI!" +} + +async fn set_cutoff( + State(state): State, + // this argument tells axum to parse the request body + // as JSON into a `CreateUser` type + Json(payload): Json, +) -> (StatusCode, Json) { + let mut streamer = state.streamer.lock().unwrap(); + streamer.min_cutoff = payload.min; + streamer.max_cutoff = payload.max; + streamer.freq_hz = payload.hz; + println!("updated to {:?}", payload); + (StatusCode::OK, true.into()) +} + +// the input to our `create_user` handler +#[derive(Deserialize, Debug)] +struct SetCutoff { + min: f64, + max: f64, + hz: f64, +}