summaryrefslogtreecommitdiff
path: root/src/routes/start.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/routes/start.rs')
-rw-r--r--src/routes/start.rs30
1 files changed, 21 insertions, 9 deletions
diff --git a/src/routes/start.rs b/src/routes/start.rs
index e7d7e0e..2d505fc 100644
--- a/src/routes/start.rs
+++ b/src/routes/start.rs
@@ -4,28 +4,40 @@ use axum::Json;
4use axum::response::{IntoResponse, Response}; 4use axum::response::{IntoResponse, Response};
5use serde::{Deserialize, Serialize}; 5use serde::{Deserialize, Serialize};
6use std::error::Error; 6use std::error::Error;
7use std::sync::Arc;
8use axum::extract::State;
7use serde_json::{json, Value}; 9use serde_json::{json, Value};
8use tracing::error; 10use tracing::{error, info};
9use crate::auth::{auth, AuthError}; 11use crate::auth::{auth, AuthError};
10use crate::config::SETTINGS; 12use crate::config::SETTINGS;
11use crate::wol::{create_buffer, send_packet}; 13use crate::wol::{create_buffer, send_packet};
14use crate::db::Device;
12 15
13pub async fn start(headers: HeaderMap, Json(payload): Json<StartPayload>) -> Result<Json<Value>, StartError> { 16pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<StartPayload>) -> Result<Json<Value>, StartError> {
14 let secret = headers.get("authorization"); 17 let secret = headers.get("authorization");
15 if auth(secret).map_err(StartError::Auth)? { 18 if auth(secret).map_err(StartError::Auth)? {
19 let device = sqlx::query_as!(
20 Device,
21 r#"
22 SELECT id, mac, broadcast_addr
23 FROM devices
24 WHERE id = ?1;
25 "#,
26 payload.id
27 ).fetch_one(&state.db).await.map_err(|err| StartError::Server(Box::new(err)))?;
28
29 info!("starting {}", device.id);
30
16 let bind_addr = SETTINGS 31 let bind_addr = SETTINGS
17 .get_string("bindaddr") 32 .get_string("bindaddr")
18 .map_err(|err| StartError::Server(Box::new(err)))?; 33 .map_err(|err| StartError::Server(Box::new(err)))?;
19 let broadcast_addr = SETTINGS 34
20 .get_string("broadcastaddr")
21 .map_err(|err| StartError::Server(Box::new(err)))?;
22 let _ = send_packet( 35 let _ = send_packet(
23 &bind_addr.parse().map_err(|err| StartError::Server(Box::new(err)))?, 36 &bind_addr.parse().map_err(|err| StartError::Server(Box::new(err)))?,
24 &broadcast_addr.parse().map_err(|err| StartError::Server(Box::new(err)))?, 37 &device.broadcast_addr.parse().map_err(|err| StartError::Server(Box::new(err)))?,
25 // TODO: MAC saved in DB 38 create_buffer(&device.mac).map_err(|err| StartError::Server(Box::new(err)))?
26 create_buffer(std::env::var("MAC").unwrap().as_str()).map_err(|err| StartError::Server(Box::new(err)))?
27 ).map_err(|err| StartError::Server(Box::new(err))); 39 ).map_err(|err| StartError::Server(Box::new(err)));
28 Ok(Json(json!(StartResponse { id: payload.id, boot: true }))) 40 Ok(Json(json!(StartResponse { id: device.id, boot: true })))
29 } else { 41 } else {
30 Err(StartError::Generic) 42 Err(StartError::Generic)
31 } 43 }