From 3bc7cf8ed36016ca3da9438a98f4fe8b8e6f9e61 Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Thu, 15 Feb 2024 17:17:30 +0100 Subject: Closes #10 & #12. Added `thiserror` crate and changed to `IntoSocketAddr` for easier usage and error handling --- src/routes/device.rs | 12 ++++++------ src/routes/start.rs | 9 ++++----- 2 files changed, 10 insertions(+), 11 deletions(-) (limited to 'src/routes') diff --git a/src/routes/device.rs b/src/routes/device.rs index c85df1b..aa52cf7 100644 --- a/src/routes/device.rs +++ b/src/routes/device.rs @@ -12,7 +12,7 @@ use crate::error::Error; pub async fn get(State(state): State>, headers: HeaderMap, Json(payload): Json) -> Result, Error> { info!("add device {}", payload.id); let secret = headers.get("authorization"); - if auth(&state.config, secret).map_err(Error::Auth)? { + if auth(&state.config, secret)? { let device = sqlx::query_as!( Device, r#" @@ -21,7 +21,7 @@ pub async fn get(State(state): State>, headers: HeaderMap, WHERE id = $1; "#, payload.id - ).fetch_one(&state.db).await.map_err(Error::DB)?; + ).fetch_one(&state.db).await?; debug!("got device {:?}", device); @@ -39,7 +39,7 @@ pub struct GetDevicePayload { pub async fn put(State(state): State>, headers: HeaderMap, Json(payload): Json) -> Result, Error> { info!("add device {} ({}, {}, {})", payload.id, payload.mac, payload.broadcast_addr, payload.ip); let secret = headers.get("authorization"); - if auth(&state.config, secret).map_err(Error::Auth)? { + if auth(&state.config, secret)? { sqlx::query!( r#" INSERT INTO devices (id, mac, broadcast_addr, ip) @@ -49,7 +49,7 @@ pub async fn put(State(state): State>, headers: HeaderMap, payload.mac, payload.broadcast_addr, payload.ip - ).execute(&state.db).await.map_err(Error::DB)?; + ).execute(&state.db).await?; Ok(Json(json!(PutDeviceResponse { success: true }))) } else { @@ -73,7 +73,7 @@ pub struct PutDeviceResponse { pub async fn post(State(state): State>, headers: HeaderMap, Json(payload): Json) -> Result, Error> { info!("edit device {} ({}, {}, {})", payload.id, payload.mac, payload.broadcast_addr, payload.ip); let secret = headers.get("authorization"); - if auth(&state.config, secret).map_err(Error::Auth)? { + if auth(&state.config, secret)? { let device = sqlx::query_as!( Device, r#" @@ -85,7 +85,7 @@ pub async fn post(State(state): State>, headers: HeaderMap, payload.broadcast_addr, payload.ip, payload.id - ).fetch_one(&state.db).await.map_err(Error::DB)?; + ).fetch_one(&state.db).await?; Ok(Json(json!(device))) } else { diff --git a/src/routes/start.rs b/src/routes/start.rs index ce95bf3..66b7cb4 100644 --- a/src/routes/start.rs +++ b/src/routes/start.rs @@ -20,7 +20,7 @@ pub async fn start( ) -> Result, Error> { info!("POST request"); let secret = headers.get("authorization"); - let authorized = auth(&state.config, secret).map_err(Error::Auth)?; + let authorized = auth(&state.config, secret)?; if authorized { let device = sqlx::query_as!( Device, @@ -32,16 +32,15 @@ pub async fn start( payload.id ) .fetch_one(&state.db) - .await - .map_err(Error::DB)?; + .await?; info!("starting {}", device.id); let bind_addr = "0.0.0.0:0"; let _ = send_packet( - &bind_addr.parse().map_err(Error::IpParse)?, - &device.broadcast_addr.parse().map_err(Error::IpParse)?, + bind_addr, + &device.broadcast_addr, &create_buffer(&device.mac)?, )?; let dev_id = device.id.clone(); -- cgit v1.2.3