From 8ed77d7ab484121e9d70158e14c9fd6c243f1c70 Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Mon, 12 Feb 2024 14:58:08 +0100 Subject: Close #9. Config impl with struct and files --- src/routes/device.rs | 6 +++--- src/routes/start.rs | 8 +++----- 2 files changed, 6 insertions(+), 8 deletions(-) (limited to 'src/routes') diff --git a/src/routes/device.rs b/src/routes/device.rs index b80cb85..c85df1b 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(secret).map_err(Error::Auth)? { + if auth(&state.config, secret).map_err(Error::Auth)? { let device = sqlx::query_as!( Device, r#" @@ -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(secret).map_err(Error::Auth)? { + if auth(&state.config, secret).map_err(Error::Auth)? { sqlx::query!( r#" INSERT INTO devices (id, mac, broadcast_addr, ip) @@ -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(secret).map_err(Error::Auth)? { + if auth(&state.config, secret).map_err(Error::Auth)? { let device = sqlx::query_as!( Device, r#" diff --git a/src/routes/start.rs b/src/routes/start.rs index 4264588..ce95bf3 100644 --- a/src/routes/start.rs +++ b/src/routes/start.rs @@ -1,5 +1,4 @@ use crate::auth::auth; -use crate::config::SETTINGS; use crate::db::Device; use crate::error::Error; use crate::services::ping::Value as PingValue; @@ -21,7 +20,7 @@ pub async fn start( ) -> Result, Error> { info!("POST request"); let secret = headers.get("authorization"); - let authorized = auth(secret).map_err(Error::Auth)?; + let authorized = auth(&state.config, secret).map_err(Error::Auth)?; if authorized { let device = sqlx::query_as!( Device, @@ -38,9 +37,7 @@ pub async fn start( info!("starting {}", device.id); - let bind_addr = SETTINGS - .get_string("bindaddr") - .unwrap_or("0.0.0.0:1111".to_string()); + let bind_addr = "0.0.0.0:0"; let _ = send_packet( &bind_addr.parse().map_err(Error::IpParse)?, @@ -75,6 +72,7 @@ pub async fn start( crate::services::ping::spawn( state.ping_send.clone(), + &state.config, device, uuid_gen.clone(), &state.ping_map, -- cgit v1.2.3