From 3e6a72428824c5a50a873a4284b86d0a9e47a778 Mon Sep 17 00:00:00 2001 From: fx Date: Mon, 9 Oct 2023 17:26:59 +0200 Subject: db int for api --- src/routes/start.rs | 52 +++++++++++++--------------------------------------- 1 file changed, 13 insertions(+), 39 deletions(-) (limited to 'src/routes/start.rs') diff --git a/src/routes/start.rs b/src/routes/start.rs index 2d505fc..d16ea4e 100644 --- a/src/routes/start.rs +++ b/src/routes/start.rs @@ -1,45 +1,43 @@ use axum::headers::HeaderMap; -use axum::http::StatusCode; use axum::Json; -use axum::response::{IntoResponse, Response}; use serde::{Deserialize, Serialize}; -use std::error::Error; use std::sync::Arc; use axum::extract::State; use serde_json::{json, Value}; -use tracing::{error, info}; -use crate::auth::{auth, AuthError}; +use tracing::info; +use crate::auth::auth; use crate::config::SETTINGS; use crate::wol::{create_buffer, send_packet}; use crate::db::Device; +use crate::error::WebolError; -pub async fn start(State(state): State>, headers: HeaderMap, Json(payload): Json) -> Result, StartError> { +pub async fn start(State(state): State>, headers: HeaderMap, Json(payload): Json) -> Result, WebolError> { let secret = headers.get("authorization"); - if auth(secret).map_err(StartError::Auth)? { + if auth(secret).map_err(WebolError::Auth)? { let device = sqlx::query_as!( Device, r#" SELECT id, mac, broadcast_addr FROM devices - WHERE id = ?1; + WHERE id = $1; "#, payload.id - ).fetch_one(&state.db).await.map_err(|err| StartError::Server(Box::new(err)))?; + ).fetch_one(&state.db).await.map_err(|err| WebolError::Server(Box::new(err)))?; info!("starting {}", device.id); let bind_addr = SETTINGS .get_string("bindaddr") - .map_err(|err| StartError::Server(Box::new(err)))?; + .map_err(|err| WebolError::Server(Box::new(err)))?; let _ = send_packet( - &bind_addr.parse().map_err(|err| StartError::Server(Box::new(err)))?, - &device.broadcast_addr.parse().map_err(|err| StartError::Server(Box::new(err)))?, - create_buffer(&device.mac).map_err(|err| StartError::Server(Box::new(err)))? - ).map_err(|err| StartError::Server(Box::new(err))); + &bind_addr.parse().map_err(|err| WebolError::Server(Box::new(err)))?, + &device.broadcast_addr.parse().map_err(|err| WebolError::Server(Box::new(err)))?, + create_buffer(&device.mac).map_err(|err| WebolError::Server(Box::new(err)))? + ).map_err(|err| WebolError::Server(Box::new(err))); Ok(Json(json!(StartResponse { id: device.id, boot: true }))) } else { - Err(StartError::Generic) + Err(WebolError::Generic) } } @@ -53,28 +51,4 @@ pub struct StartPayload { struct StartResponse { id: String, boot: bool, -} - -pub enum StartError { - Auth(AuthError), - Generic, - Server(Box), -} - -impl IntoResponse for StartError { - fn into_response(self) -> Response { - let (status, error_message) = match self { - StartError::Auth(err) => err.get(), - StartError::Generic => (StatusCode::INTERNAL_SERVER_ERROR, ""), - StartError::Server(err) => { - error!("server error: {}", err.to_string()); - (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") - }, - - }; - let body = Json(json!({ - "error": error_message, - })); - (status, body).into_response() - } } \ No newline at end of file -- cgit v1.2.3