diff options
author | fx <[email protected]> | 2023-10-25 12:53:31 +0200 |
---|---|---|
committer | fx <[email protected]> | 2023-10-25 12:53:31 +0200 |
commit | 00dd8a9abee6b9f0cfc37c6f20f30f0d99dfe91a (patch) | |
tree | 7906b5836f3ca24686b1b7418c2128b93c33a398 /src/routes | |
parent | f9224ff02e688dec819ab81893320a0611f2a198 (diff) | |
download | webol-00dd8a9abee6b9f0cfc37c6f20f30f0d99dfe91a.tar webol-00dd8a9abee6b9f0cfc37c6f20f30f0d99dfe91a.tar.gz webol-00dd8a9abee6b9f0cfc37c6f20f30f0d99dfe91a.zip |
runs, no error handling
Diffstat (limited to 'src/routes')
-rw-r--r-- | src/routes/mod.rs | 3 | ||||
-rw-r--r-- | src/routes/start.rs | 24 | ||||
-rw-r--r-- | src/routes/status.rs | 12 |
3 files changed, 31 insertions, 8 deletions
diff --git a/src/routes/mod.rs b/src/routes/mod.rs index 12fbfab..d5ab0d6 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs | |||
@@ -1,2 +1,3 @@ | |||
1 | pub mod start; | 1 | pub mod start; |
2 | pub mod device; \ No newline at end of file | 2 | pub mod device; |
3 | pub mod status; \ No newline at end of file | ||
diff --git a/src/routes/start.rs b/src/routes/start.rs index 863ef16..45e7ec8 100644 --- a/src/routes/start.rs +++ b/src/routes/start.rs | |||
@@ -4,15 +4,18 @@ 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::{debug, info}; | 7 | use tracing::{debug, info, warn}; |
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; |
13 | 14 | ||
15 | #[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> { | 16 | pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<StartPayload>) -> Result<Json<Value>, WebolError> { |
15 | info!("POST request"); | 17 | info!("POST request"); |
18 | warn!("{:?}", state.ping_map); | ||
16 | let secret = headers.get("authorization"); | 19 | let secret = headers.get("authorization"); |
17 | let authorized = auth(secret).map_err(WebolError::Auth)?; | 20 | let authorized = auth(secret).map_err(WebolError::Auth)?; |
18 | if authorized { | 21 | if authorized { |
@@ -38,14 +41,20 @@ pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap | |||
38 | create_buffer(&device.mac)? | 41 | create_buffer(&device.mac)? |
39 | )?; | 42 | )?; |
40 | 43 | ||
41 | if payload.ping.is_some_and(|ping| ping) { | 44 | let uuid = if payload.ping.is_some_and(|ping| ping) { |
42 | debug!("ping true"); | 45 | let uuid_gen = Uuid::new_v4().to_string(); |
43 | tokio::spawn(async move { | 46 | let uuid_genc = uuid_gen.clone(); |
47 | tokio::spawn(async move{ | ||
44 | debug!("Init ping service"); | 48 | debug!("Init ping service"); |
45 | crate::services::ping::spawn(state.ping_send.clone()).await | 49 | state.ping_map.lock().await.insert(uuid_gen, ("192.168.178.94".to_string(), false)); |
50 | |||
51 | warn!("{:?}", state.ping_map); | ||
52 | |||
53 | crate::services::ping::spawn(state.ping_send.clone(), "192.168.178.94".to_string()).await; | ||
46 | }); | 54 | }); |
47 | }; | 55 | Some(uuid_genc) |
48 | Ok(Json(json!(StartResponse { id: device.id, boot: true }))) | 56 | } else { None }; |
57 | Ok(Json(json!(StartResponse { id: device.id, boot: true, uuid }))) | ||
49 | } else { | 58 | } else { |
50 | Err(WebolError::Generic) | 59 | Err(WebolError::Generic) |
51 | } | 60 | } |
@@ -61,4 +70,5 @@ pub struct StartPayload { | |||
61 | struct StartResponse { | 70 | struct StartResponse { |
62 | id: String, | 71 | id: String, |
63 | boot: bool, | 72 | boot: bool, |
73 | uuid: Option<String>, | ||
64 | } | 74 | } |
diff --git a/src/routes/status.rs b/src/routes/status.rs new file mode 100644 index 0000000..cdecf6a --- /dev/null +++ b/src/routes/status.rs | |||
@@ -0,0 +1,12 @@ | |||
1 | use std::sync::Arc; | ||
2 | use axum::extract::{State, WebSocketUpgrade}; | ||
3 | use axum::response::Response; | ||
4 | use serde::Deserialize; | ||
5 | use crate::AppState; | ||
6 | use crate::services::ping::status_websocket; | ||
7 | |||
8 | #[axum_macros::debug_handler] | ||
9 | pub async fn status(State(state): State<Arc<AppState>>, ws: WebSocketUpgrade) -> Response { | ||
10 | // TODO: remove unwrap | ||
11 | ws.on_upgrade(move |socket| status_websocket(socket, state.ping_send.clone(), state.ping_map.clone())) | ||
12 | } \ No newline at end of file | ||