summaryrefslogtreecommitdiff
path: root/src/routes
diff options
context:
space:
mode:
Diffstat (limited to 'src/routes')
-rw-r--r--src/routes/device.rs8
-rw-r--r--src/routes/start.rs21
2 files changed, 17 insertions, 12 deletions
diff --git a/src/routes/device.rs b/src/routes/device.rs
index 025c7d0..248d1e0 100644
--- a/src/routes/device.rs
+++ b/src/routes/device.rs
@@ -21,7 +21,7 @@ pub async fn get_device(State(state): State<Arc<crate::AppState>>, headers: Head
21 WHERE id = $1; 21 WHERE id = $1;
22 "#, 22 "#,
23 payload.id 23 payload.id
24 ).fetch_one(&state.db).await.map_err(|err| WebolError::Server(Box::new(err)))?; 24 ).fetch_one(&state.db).await.map_err(WebolError::DB)?;
25 25
26 Ok(Json(json!(device))) 26 Ok(Json(json!(device)))
27 } else { 27 } else {
@@ -46,7 +46,7 @@ pub async fn put_device(State(state): State<Arc<crate::AppState>>, headers: Head
46 payload.id, 46 payload.id,
47 payload.mac, 47 payload.mac,
48 payload.broadcast_addr 48 payload.broadcast_addr
49 ).execute(&state.db).await.map_err(|err| WebolError::Server(Box::new(err)))?; 49 ).execute(&state.db).await.map_err(WebolError::DB)?;
50 50
51 Ok(Json(json!(PutDeviceResponse { success: true }))) 51 Ok(Json(json!(PutDeviceResponse { success: true })))
52 } else { 52 } else {
@@ -80,7 +80,7 @@ pub async fn post_device(State(state): State<Arc<crate::AppState>>, headers: Hea
80 payload.mac, 80 payload.mac,
81 payload.broadcast_addr, 81 payload.broadcast_addr,
82 payload.id 82 payload.id
83 ).fetch_one(&state.db).await.map_err(|err| WebolError::Server(Box::new(err)))?; 83 ).fetch_one(&state.db).await.map_err(WebolError::DB)?;
84 84
85 Ok(Json(json!(device))) 85 Ok(Json(json!(device)))
86 } else { 86 } else {
@@ -93,4 +93,4 @@ pub struct PostDevicePayload {
93 id: String, 93 id: String,
94 mac: String, 94 mac: String,
95 broadcast_addr: String, 95 broadcast_addr: String,
96} \ No newline at end of file 96}
diff --git a/src/routes/start.rs b/src/routes/start.rs
index 163d58c..b45fe5b 100644
--- a/src/routes/start.rs
+++ b/src/routes/start.rs
@@ -14,7 +14,8 @@ use crate::error::WebolError;
14pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<StartPayload>) -> Result<Json<Value>, WebolError> { 14pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<StartPayload>) -> Result<Json<Value>, WebolError> {
15 info!("POST request"); 15 info!("POST request");
16 let secret = headers.get("authorization"); 16 let secret = headers.get("authorization");
17 if auth(secret).map_err(WebolError::Auth)? { 17 let authorized = auth(secret).map_err(WebolError::Auth)?;
18 if authorized {
18 let device = sqlx::query_as!( 19 let device = sqlx::query_as!(
19 Device, 20 Device,
20 r#" 21 r#"
@@ -23,7 +24,7 @@ pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap
23 WHERE id = $1; 24 WHERE id = $1;
24 "#, 25 "#,
25 payload.id 26 payload.id
26 ).fetch_one(&state.db).await.map_err(|err| WebolError::Server(Box::new(err)))?; 27 ).fetch_one(&state.db).await.map_err(WebolError::DB)?;
27 28
28 info!("starting {}", device.id); 29 info!("starting {}", device.id);
29 30
@@ -32,10 +33,14 @@ pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap
32 .unwrap_or("0.0.0.0:1111".to_string()); 33 .unwrap_or("0.0.0.0:1111".to_string());
33 34
34 let _ = send_packet( 35 let _ = send_packet(
35 &bind_addr.parse().map_err(|err| WebolError::Server(Box::new(err)))?, 36 &bind_addr.parse().map_err(WebolError::IpParse)?,
36 &device.broadcast_addr.parse().map_err(|err| WebolError::Server(Box::new(err)))?, 37 &device.broadcast_addr.parse().map_err(WebolError::IpParse)?,
37 create_buffer(&device.mac).map_err(|err| WebolError::Server(Box::new(err)))? 38 create_buffer(&device.mac)?
38 ).map_err(|err| WebolError::Server(Box::new(err))); 39 )?;
40
41 if payload.ping.is_some_and(|ping| ping) {
42 tokio::spawn(async move {crate::services::ping::spawn(state.ping_send.clone()).await});
43 }
39 Ok(Json(json!(StartResponse { id: device.id, boot: true }))) 44 Ok(Json(json!(StartResponse { id: device.id, boot: true })))
40 } else { 45 } else {
41 Err(WebolError::Generic) 46 Err(WebolError::Generic)
@@ -45,11 +50,11 @@ pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap
45#[derive(Deserialize)] 50#[derive(Deserialize)]
46pub struct StartPayload { 51pub struct StartPayload {
47 id: String, 52 id: String,
48 _test: Option<bool>, 53 ping: Option<bool>,
49} 54}
50 55
51#[derive(Serialize)] 56#[derive(Serialize)]
52struct StartResponse { 57struct StartResponse {
53 id: String, 58 id: String,
54 boot: bool, 59 boot: bool,
55} \ No newline at end of file 60}