summaryrefslogtreecommitdiff
path: root/src/routes/start.rs
diff options
context:
space:
mode:
authorFxQnLr <[email protected]>2023-11-06 11:09:34 +0100
committerGitHub <[email protected]>2023-11-06 11:09:34 +0100
commit1cd2a8e4aecfaad2a8385a6bea61580209b86398 (patch)
treec357bcaca0681caf9a6742c857bb494dc4315900 /src/routes/start.rs
parentd9d7b125e4fcaa3aedd7b57a69e6880e012ccf33 (diff)
parent32561060a8dc6fc6118498da06bdd8f5b4c3f0fd (diff)
downloadwebol-1cd2a8e4aecfaad2a8385a6bea61580209b86398.tar
webol-1cd2a8e4aecfaad2a8385a6bea61580209b86398.tar.gz
webol-1cd2a8e4aecfaad2a8385a6bea61580209b86398.zip
Merge pull request #6 from FxQnLr/ping
Ping
Diffstat (limited to 'src/routes/start.rs')
-rw-r--r--src/routes/start.rs39
1 files changed, 28 insertions, 11 deletions
diff --git a/src/routes/start.rs b/src/routes/start.rs
index 163d58c..271f924 100644
--- a/src/routes/start.rs
+++ b/src/routes/start.rs
@@ -4,26 +4,30 @@ use serde::{Deserialize, Serialize};
4use std::sync::Arc; 4use std::sync::Arc;
5use axum::extract::State; 5use axum::extract::State;
6use serde_json::{json, Value}; 6use serde_json::{json, Value};
7use tracing::info; 7use tracing::{debug, info};
8use uuid::Uuid;
8use crate::auth::auth; 9use crate::auth::auth;
9use crate::config::SETTINGS; 10use crate::config::SETTINGS;
10use crate::wol::{create_buffer, send_packet}; 11use crate::wol::{create_buffer, send_packet};
11use crate::db::Device; 12use crate::db::Device;
12use crate::error::WebolError; 13use crate::error::WebolError;
14use crate::services::ping::PingValue;
13 15
16#[axum_macros::debug_handler]
14pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<StartPayload>) -> Result<Json<Value>, WebolError> { 17pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<StartPayload>) -> Result<Json<Value>, WebolError> {
15 info!("POST request"); 18 info!("POST request");
16 let secret = headers.get("authorization"); 19 let secret = headers.get("authorization");
17 if auth(secret).map_err(WebolError::Auth)? { 20 let authorized = auth(secret).map_err(WebolError::Auth)?;
21 if authorized {
18 let device = sqlx::query_as!( 22 let device = sqlx::query_as!(
19 Device, 23 Device,
20 r#" 24 r#"
21 SELECT id, mac, broadcast_addr 25 SELECT id, mac, broadcast_addr, ip
22 FROM devices 26 FROM devices
23 WHERE id = $1; 27 WHERE id = $1;
24 "#, 28 "#,
25 payload.id 29 payload.id
26 ).fetch_one(&state.db).await.map_err(|err| WebolError::Server(Box::new(err)))?; 30 ).fetch_one(&state.db).await.map_err(WebolError::DB)?;
27 31
28 info!("starting {}", device.id); 32 info!("starting {}", device.id);
29 33
@@ -32,11 +36,23 @@ pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap
32 .unwrap_or("0.0.0.0:1111".to_string()); 36 .unwrap_or("0.0.0.0:1111".to_string());
33 37
34 let _ = send_packet( 38 let _ = send_packet(
35 &bind_addr.parse().map_err(|err| WebolError::Server(Box::new(err)))?, 39 &bind_addr.parse().map_err(WebolError::IpParse)?,
36 &device.broadcast_addr.parse().map_err(|err| WebolError::Server(Box::new(err)))?, 40 &device.broadcast_addr.parse().map_err(WebolError::IpParse)?,
37 create_buffer(&device.mac).map_err(|err| WebolError::Server(Box::new(err)))? 41 create_buffer(&device.mac)?
38 ).map_err(|err| WebolError::Server(Box::new(err))); 42 )?;
39 Ok(Json(json!(StartResponse { id: device.id, boot: true }))) 43
44 let uuid = if payload.ping.is_some_and(|ping| ping) {
45 let uuid_gen = Uuid::new_v4().to_string();
46 let uuid_genc = uuid_gen.clone();
47 tokio::spawn(async move {
48 debug!("init ping service");
49 state.ping_map.insert(uuid_gen.clone(), PingValue { ip: device.ip.clone(), online: false });
50
51 crate::services::ping::spawn(state.ping_send.clone(), device.ip, uuid_gen.clone(), &state.ping_map).await
52 });
53 Some(uuid_genc)
54 } else { None };
55 Ok(Json(json!(StartResponse { id: device.id, boot: true, uuid })))
40 } else { 56 } else {
41 Err(WebolError::Generic) 57 Err(WebolError::Generic)
42 } 58 }
@@ -45,11 +61,12 @@ pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap
45#[derive(Deserialize)] 61#[derive(Deserialize)]
46pub struct StartPayload { 62pub struct StartPayload {
47 id: String, 63 id: String,
48 _test: Option<bool>, 64 ping: Option<bool>,
49} 65}
50 66
51#[derive(Serialize)] 67#[derive(Serialize)]
52struct StartResponse { 68struct StartResponse {
53 id: String, 69 id: String,
54 boot: bool, 70 boot: bool,
55} \ No newline at end of file 71 uuid: Option<String>,
72}