add missing src/main.rs
This commit is contained in:
71
src/main.rs
Normal file
71
src/main.rs
Normal file
@@ -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<Mutex<Streamer>>,
|
||||
}
|
||||
|
||||
#[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<AppState>,
|
||||
// this argument tells axum to parse the request body
|
||||
// as JSON into a `CreateUser` type
|
||||
Json(payload): Json<SetCutoff>,
|
||||
) -> (StatusCode, Json<bool>) {
|
||||
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,
|
||||
}
|
||||
Reference in New Issue
Block a user