From 6f94a825485b1d622d8ebe88ddf3e2f426d1c69c Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Wed, 29 Nov 2023 15:48:26 +0100 Subject: update to axum 0.7 and cargo update --- src/auth.rs | 3 +-- src/main.rs | 17 +++++++++----- src/routes/device.rs | 2 +- src/routes/start.rs | 4 ++-- src/services/ping.rs | 66 ++++++++++++++++++++++++---------------------------- src/wol.rs | 2 +- 6 files changed, 46 insertions(+), 48 deletions(-) (limited to 'src') diff --git a/src/auth.rs b/src/auth.rs index e4b1c2f..90d920f 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -1,5 +1,4 @@ -use axum::headers::HeaderValue; -use axum::http::StatusCode; +use axum::http::{StatusCode, HeaderValue}; use axum::http::header::ToStrError; use tracing::{debug, error, trace}; use crate::auth::AuthError::{MissingSecret, WrongSecret}; diff --git a/src/main.rs b/src/main.rs index e96b736..aab9df3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ use std::env; +use std::net::SocketAddr; use std::sync::Arc; use axum::{Router, routing::post}; use axum::routing::{get, put}; @@ -24,7 +25,10 @@ mod error; mod services; #[tokio::main] -async fn main() { +async fn main() -> color_eyre::eyre::Result<()> { + + color_eyre::install()?; + unsafe { local_offset::set_soundness(local_offset::Soundness::Unsound); } let time_format = time::macros::format_description!("[year]-[month]-[day] [hour]:[minute]:[second]"); @@ -64,14 +68,15 @@ async fn main() { let addr = SETTINGS.get_string("serveraddr").unwrap_or("0.0.0.0:7229".to_string()); info!("start server on {}", addr); - axum::Server::bind(&addr.parse().unwrap()) - .serve(app.into_make_service()) - .await - .unwrap(); + let listener = tokio::net::TcpListener::bind(addr.parse::()?) + .await?; + axum::serve(listener, app).await?; + + Ok(()) } pub struct AppState { db: PgPool, ping_send: Sender, ping_map: PingMap, -} \ No newline at end of file +} diff --git a/src/routes/device.rs b/src/routes/device.rs index 678d117..a3308d4 100644 --- a/src/routes/device.rs +++ b/src/routes/device.rs @@ -1,7 +1,7 @@ use std::sync::Arc; use axum::extract::State; -use axum::headers::HeaderMap; use axum::Json; +use axum::http::HeaderMap; use serde::{Deserialize, Serialize}; use serde_json::{json, Value}; use tracing::{debug, info}; diff --git a/src/routes/start.rs b/src/routes/start.rs index 1555db3..a206cbd 100644 --- a/src/routes/start.rs +++ b/src/routes/start.rs @@ -1,5 +1,5 @@ -use axum::headers::HeaderMap; use axum::Json; +use axum::http::HeaderMap; use serde::{Deserialize, Serialize}; use std::sync::Arc; use axum::extract::State; @@ -60,7 +60,7 @@ pub async fn start(State(state): State>, headers: HeaderMap debug!("init ping service"); state.ping_map.insert(uuid_gen.clone(), PingValue { ip: device.ip.clone(), online: false }); - crate::services::ping::spawn(state.ping_send.clone(), device, uuid_gen.clone(), &state.ping_map, &state.db).await + crate::services::ping::spawn(state.ping_send.clone(), device, uuid_gen.clone(), &state.ping_map, &state.db).await; }); Some(uuid_genc) } else { None }; diff --git a/src/services/ping.rs b/src/services/ping.rs index c3bdced..0f773f4 100644 --- a/src/services/ping.rs +++ b/src/services/ping.rs @@ -85,17 +85,14 @@ pub async fn status_websocket(mut socket: WebSocket, state: Arc) { trace!("Search for uuid: {}", uuid); let eta = get_eta(&state.db).await; - let _ = socket.send(Message::Text(format!("eta_{}_{}", eta, uuid))).await; + let _ = socket.send(Message::Text(format!("eta_{eta}_{uuid}"))).await; let device_exists = state.ping_map.contains_key(&uuid); - match device_exists { - true => { - let _ = socket.send(process_device(state.clone(), uuid).await).await; - }, - false => { - debug!("didn't find any device"); - let _ = socket.send(Message::Text(format!("notfound_{}", uuid))).await; - }, + if device_exists { + let _ = socket.send(process_device(state.clone(), uuid).await).await; + } else { + debug!("didn't find any device"); + let _ = socket.send(Message::Text(format!("notfound_{uuid}"))).await; }; let _ = socket.close().await; @@ -118,34 +115,31 @@ async fn process_device(state: Arc, uuid: String) -> Message { let pm = state.ping_map.clone().into_read_only(); let device = pm.get(&uuid).expect("fatal error"); debug!("got device: {} (online: {})", device.ip, device.online); - match device.online { - true => { - debug!("already started"); - Message::Text(format!("start_{}", uuid)) - }, - false => { - loop{ - trace!("wait for tx message"); - let message = state.ping_send.subscribe().recv().await.expect("fatal error"); - trace!("got message {:?}", message); - return match message { - BroadcastCommands::Success(msg_uuid) => { - if msg_uuid != uuid { continue; } - trace!("message == uuid success"); - Message::Text(format!("start_{}", uuid)) - }, - BroadcastCommands::Timeout(msg_uuid) => { - if msg_uuid != uuid { continue; } - trace!("message == uuid timeout"); - Message::Text(format!("timeout_{}", uuid)) - }, - BroadcastCommands::Error(msg_uuid) => { - if msg_uuid != uuid { continue; } - trace!("message == uuid error"); - Message::Text(format!("error_{}", uuid)) - } + if device.online { + debug!("already started"); + Message::Text(format!("start_{uuid}")) + } else { + loop { + trace!("wait for tx message"); + let message = state.ping_send.subscribe().recv().await.expect("fatal error"); + trace!("got message {:?}", message); + return match message { + BroadcastCommands::Success(msg_uuid) => { + if msg_uuid != uuid { continue; } + trace!("message == uuid success"); + Message::Text(format!("start_{uuid}")) + }, + BroadcastCommands::Timeout(msg_uuid) => { + if msg_uuid != uuid { continue; } + trace!("message == uuid timeout"); + Message::Text(format!("timeout_{uuid}")) + }, + BroadcastCommands::Error(msg_uuid) => { + if msg_uuid != uuid { continue; } + trace!("message == uuid error"); + Message::Text(format!("error_{uuid}")) } } } } -} \ No newline at end of file +} diff --git a/src/wol.rs b/src/wol.rs index 0cdcae3..8755b21 100644 --- a/src/wol.rs +++ b/src/wol.rs @@ -11,7 +11,7 @@ pub fn create_buffer(mac_addr: &str) -> Result, WebolError> { let mut mac = Vec::new(); let sp = mac_addr.split(':'); for f in sp { - mac.push(u8::from_str_radix(f, 16).map_err(WebolError::BufferParse)?) + mac.push(u8::from_str_radix(f, 16).map_err(WebolError::BufferParse)?); }; let mut buf = vec![255; 6]; for _ in 0..16 { -- cgit v1.2.3