diff options
author | FxQnLr <[email protected]> | 2023-11-06 11:09:34 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2023-11-06 11:09:34 +0100 |
commit | 1cd2a8e4aecfaad2a8385a6bea61580209b86398 (patch) | |
tree | c357bcaca0681caf9a6742c857bb494dc4315900 /src/routes/start.rs | |
parent | d9d7b125e4fcaa3aedd7b57a69e6880e012ccf33 (diff) | |
parent | 32561060a8dc6fc6118498da06bdd8f5b4c3f0fd (diff) | |
download | webol-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.rs | 39 |
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}; | |||
4 | use std::sync::Arc; | 4 | use std::sync::Arc; |
5 | use axum::extract::State; | 5 | use axum::extract::State; |
6 | use serde_json::{json, Value}; | 6 | use serde_json::{json, Value}; |
7 | use tracing::info; | 7 | use tracing::{debug, info}; |
8 | use uuid::Uuid; | ||
8 | use crate::auth::auth; | 9 | use crate::auth::auth; |
9 | use crate::config::SETTINGS; | 10 | use crate::config::SETTINGS; |
10 | use crate::wol::{create_buffer, send_packet}; | 11 | use crate::wol::{create_buffer, send_packet}; |
11 | use crate::db::Device; | 12 | use crate::db::Device; |
12 | use crate::error::WebolError; | 13 | use crate::error::WebolError; |
14 | use crate::services::ping::PingValue; | ||
13 | 15 | ||
16 | #[axum_macros::debug_handler] | ||
14 | pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<StartPayload>) -> Result<Json<Value>, WebolError> { | 17 | pub 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)] |
46 | pub struct StartPayload { | 62 | pub 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)] |
52 | struct StartResponse { | 68 | struct 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 | } | ||