aboutsummaryrefslogtreecommitdiff
path: root/src/routes/start.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/routes/start.rs')
-rw-r--r--src/routes/start.rs72
1 files changed, 49 insertions, 23 deletions
diff --git a/src/routes/start.rs b/src/routes/start.rs
index a206cbd..4264588 100644
--- a/src/routes/start.rs
+++ b/src/routes/start.rs
@@ -1,23 +1,27 @@
1use axum::Json; 1use crate::auth::auth;
2use crate::config::SETTINGS;
3use crate::db::Device;
4use crate::error::Error;
5use crate::services::ping::Value as PingValue;
6use crate::wol::{create_buffer, send_packet};
7use axum::extract::State;
2use axum::http::HeaderMap; 8use axum::http::HeaderMap;
9use axum::Json;
3use serde::{Deserialize, Serialize}; 10use serde::{Deserialize, Serialize};
4use std::sync::Arc;
5use axum::extract::State;
6use serde_json::{json, Value}; 11use serde_json::{json, Value};
12use std::sync::Arc;
7use tracing::{debug, info}; 13use tracing::{debug, info};
8use uuid::Uuid; 14use uuid::Uuid;
9use crate::auth::auth;
10use crate::config::SETTINGS;
11use crate::wol::{create_buffer, send_packet};
12use crate::db::Device;
13use crate::error::WebolError;
14use crate::services::ping::PingValue;
15 15
16#[axum_macros::debug_handler] 16#[axum_macros::debug_handler]
17pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<StartPayload>) -> Result<Json<Value>, WebolError> { 17pub async fn start(
18 State(state): State<Arc<crate::AppState>>,
19 headers: HeaderMap,
20 Json(payload): Json<Payload>,
21) -> Result<Json<Value>, Error> {
18 info!("POST request"); 22 info!("POST request");
19 let secret = headers.get("authorization"); 23 let secret = headers.get("authorization");
20 let authorized = auth(secret).map_err(WebolError::Auth)?; 24 let authorized = auth(secret).map_err(Error::Auth)?;
21 if authorized { 25 if authorized {
22 let device = sqlx::query_as!( 26 let device = sqlx::query_as!(
23 Device, 27 Device,
@@ -27,7 +31,10 @@ pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap
27 WHERE id = $1; 31 WHERE id = $1;
28 "#, 32 "#,
29 payload.id 33 payload.id
30 ).fetch_one(&state.db).await.map_err(WebolError::DB)?; 34 )
35 .fetch_one(&state.db)
36 .await
37 .map_err(Error::DB)?;
31 38
32 info!("starting {}", device.id); 39 info!("starting {}", device.id);
33 40
@@ -36,9 +43,9 @@ pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap
36 .unwrap_or("0.0.0.0:1111".to_string()); 43 .unwrap_or("0.0.0.0:1111".to_string());
37 44
38 let _ = send_packet( 45 let _ = send_packet(
39 &bind_addr.parse().map_err(WebolError::IpParse)?, 46 &bind_addr.parse().map_err(Error::IpParse)?,
40 &device.broadcast_addr.parse().map_err(WebolError::IpParse)?, 47 &device.broadcast_addr.parse().map_err(Error::IpParse)?,
41 create_buffer(&device.mac)? 48 &create_buffer(&device.mac)?,
42 )?; 49 )?;
43 let dev_id = device.id.clone(); 50 let dev_id = device.id.clone();
44 let uuid = if payload.ping.is_some_and(|ping| ping) { 51 let uuid = if payload.ping.is_some_and(|ping| ping) {
@@ -49,7 +56,7 @@ pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap
49 uuid = Some(key); 56 uuid = Some(key);
50 break; 57 break;
51 } 58 }
52 }; 59 }
53 let uuid_gen = match uuid { 60 let uuid_gen = match uuid {
54 Some(u) => u, 61 Some(u) => u,
55 None => Uuid::new_v4().to_string(), 62 None => Uuid::new_v4().to_string(),
@@ -58,26 +65,45 @@ pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap
58 65
59 tokio::spawn(async move { 66 tokio::spawn(async move {
60 debug!("init ping service"); 67 debug!("init ping service");
61 state.ping_map.insert(uuid_gen.clone(), PingValue { ip: device.ip.clone(), online: false }); 68 state.ping_map.insert(
69 uuid_gen.clone(),
70 PingValue {
71 ip: device.ip.clone(),
72 online: false,
73 },
74 );
62 75
63 crate::services::ping::spawn(state.ping_send.clone(), device, uuid_gen.clone(), &state.ping_map, &state.db).await; 76 crate::services::ping::spawn(
77 state.ping_send.clone(),
78 device,
79 uuid_gen.clone(),
80 &state.ping_map,
81 &state.db,
82 )
83 .await;
64 }); 84 });
65 Some(uuid_genc) 85 Some(uuid_genc)
66 } else { None }; 86 } else {
67 Ok(Json(json!(StartResponse { id: dev_id, boot: true, uuid }))) 87 None
88 };
89 Ok(Json(json!(Response {
90 id: dev_id,
91 boot: true,
92 uuid
93 })))
68 } else { 94 } else {
69 Err(WebolError::Generic) 95 Err(Error::Generic)
70 } 96 }
71} 97}
72 98
73#[derive(Deserialize)] 99#[derive(Deserialize)]
74pub struct StartPayload { 100pub struct Payload {
75 id: String, 101 id: String,
76 ping: Option<bool>, 102 ping: Option<bool>,
77} 103}
78 104
79#[derive(Serialize)] 105#[derive(Serialize)]
80struct StartResponse { 106struct Response {
81 id: String, 107 id: String,
82 boot: bool, 108 boot: bool,
83 uuid: Option<String>, 109 uuid: Option<String>,