aboutsummaryrefslogtreecommitdiff
path: root/src/routes/start.rs
diff options
context:
space:
mode:
authorfx <[email protected]>2023-10-24 01:15:22 +0200
committerfx <[email protected]>2023-10-24 01:15:22 +0200
commitdcfb83fb2069bfcf4642b03453253e35479bf3da (patch)
treeccdacf40274227e86a8294558347aeea43b1724f /src/routes/start.rs
parentd9d7b125e4fcaa3aedd7b57a69e6880e012ccf33 (diff)
downloadwebol-dcfb83fb2069bfcf4642b03453253e35479bf3da.tar
webol-dcfb83fb2069bfcf4642b03453253e35479bf3da.tar.gz
webol-dcfb83fb2069bfcf4642b03453253e35479bf3da.zip
first ping impl baseline, doesnt work
Diffstat (limited to 'src/routes/start.rs')
-rw-r--r--src/routes/start.rs21
1 files changed, 13 insertions, 8 deletions
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}