aboutsummaryrefslogtreecommitdiff
path: root/src/routes/start.rs
diff options
context:
space:
mode:
authorFxQnLr <[email protected]>2024-02-25 15:15:19 +0100
committerFxQnLr <[email protected]>2024-02-25 15:15:19 +0100
commit9058f191b69ecafc8fdeace227ac113412d03888 (patch)
tree88ae071fa31c9a5831722ec82878ccf8fd2b224a /src/routes/start.rs
parent2f9f18b80a9e2134f674f345e48a5f21de5efadd (diff)
downloadwebol-9058f191b69ecafc8fdeace227ac113412d03888.tar
webol-9058f191b69ecafc8fdeace227ac113412d03888.tar.gz
webol-9058f191b69ecafc8fdeace227ac113412d03888.zip
Closes #16. Impl auth as extractor
Diffstat (limited to 'src/routes/start.rs')
-rw-r--r--src/routes/start.rs65
1 files changed, 28 insertions, 37 deletions
diff --git a/src/routes/start.rs b/src/routes/start.rs
index 4888325..d4c0802 100644
--- a/src/routes/start.rs
+++ b/src/routes/start.rs
@@ -1,10 +1,8 @@
1use crate::auth::auth;
2use crate::db::Device; 1use crate::db::Device;
3use crate::error::Error; 2use crate::error::Error;
4use crate::services::ping::Value as PingValue; 3use crate::services::ping::Value as PingValue;
5use crate::wol::{create_buffer, send_packet}; 4use crate::wol::{create_buffer, send_packet};
6use axum::extract::State; 5use axum::extract::State;
7use axum::http::HeaderMap;
8use axum::Json; 6use axum::Json;
9use serde::{Deserialize, Serialize}; 7use serde::{Deserialize, Serialize};
10use serde_json::{json, Value}; 8use serde_json::{json, Value};
@@ -14,48 +12,41 @@ use uuid::Uuid;
14 12
15pub async fn start( 13pub async fn start(
16 State(state): State<Arc<crate::AppState>>, 14 State(state): State<Arc<crate::AppState>>,
17 headers: HeaderMap,
18 Json(payload): Json<Payload>, 15 Json(payload): Json<Payload>,
19) -> Result<Json<Value>, Error> { 16) -> Result<Json<Value>, Error> {
20 info!("POST request"); 17 info!("POST request");
21 let secret = headers.get("authorization"); 18 let device = sqlx::query_as!(
22 let authorized = matches!(auth(&state.config, secret)?, crate::auth::Response::Success); 19 Device,
23 if authorized { 20 r#"
24 let device = sqlx::query_as!( 21 SELECT id, mac, broadcast_addr, ip, times
25 Device, 22 FROM devices
26 r#" 23 WHERE id = $1;
27 SELECT id, mac, broadcast_addr, ip, times 24 "#,
28 FROM devices 25 payload.id
29 WHERE id = $1; 26 )
30 "#, 27 .fetch_one(&state.db)
31 payload.id 28 .await?;
32 )
33 .fetch_one(&state.db)
34 .await?;
35 29
36 info!("starting {}", device.id); 30 info!("starting {}", device.id);
37 31
38 let bind_addr = "0.0.0.0:0"; 32 let bind_addr = "0.0.0.0:0";
39 33
40 let _ = send_packet( 34 let _ = send_packet(
41 bind_addr, 35 bind_addr,
42 &device.broadcast_addr, 36 &device.broadcast_addr,
43 &create_buffer(&device.mac.to_string())?, 37 &create_buffer(&device.mac.to_string())?,
44 )?; 38 )?;
45 let dev_id = device.id.clone(); 39 let dev_id = device.id.clone();
46 let uuid = if payload.ping.is_some_and(|ping| ping) { 40 let uuid = if payload.ping.is_some_and(|ping| ping) {
47 Some(setup_ping(state, device)) 41 Some(setup_ping(state, device))
48 } else {
49 None
50 };
51 Ok(Json(json!(Response {
52 id: dev_id,
53 boot: true,
54 uuid
55 })))
56 } else { 42 } else {
57 Err(Error::Generic) 43 None
58 } 44 };
45 Ok(Json(json!(Response {
46 id: dev_id,
47 boot: true,
48 uuid
49 })))
59} 50}
60 51
61fn setup_ping(state: Arc<crate::AppState>, device: Device) -> String { 52fn setup_ping(state: Arc<crate::AppState>, device: Device) -> String {