aboutsummaryrefslogtreecommitdiff
path: root/src/routes
diff options
context:
space:
mode:
authorFxQnLr <[email protected]>2024-02-15 17:17:30 +0100
committerFxQnLr <[email protected]>2024-02-15 17:17:30 +0100
commit3bc7cf8ed36016ca3da9438a98f4fe8b8e6f9e61 (patch)
tree48a38a52ebfc41fadab439b9a87fae3d43f78790 /src/routes
parentc663810817183c8f92a4279236ca84d271365088 (diff)
downloadwebol-3bc7cf8ed36016ca3da9438a98f4fe8b8e6f9e61.tar
webol-3bc7cf8ed36016ca3da9438a98f4fe8b8e6f9e61.tar.gz
webol-3bc7cf8ed36016ca3da9438a98f4fe8b8e6f9e61.zip
Closes #10 & #12. Added `thiserror` crate and changed to `IntoSocketAddr` for easier usage and error handling
Diffstat (limited to 'src/routes')
-rw-r--r--src/routes/device.rs12
-rw-r--r--src/routes/start.rs9
2 files changed, 10 insertions, 11 deletions
diff --git a/src/routes/device.rs b/src/routes/device.rs
index c85df1b..aa52cf7 100644
--- a/src/routes/device.rs
+++ b/src/routes/device.rs
@@ -12,7 +12,7 @@ use crate::error::Error;
12pub async fn get(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<GetDevicePayload>) -> Result<Json<Value>, Error> { 12pub async fn get(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<GetDevicePayload>) -> Result<Json<Value>, Error> {
13 info!("add device {}", payload.id); 13 info!("add device {}", payload.id);
14 let secret = headers.get("authorization"); 14 let secret = headers.get("authorization");
15 if auth(&state.config, secret).map_err(Error::Auth)? { 15 if auth(&state.config, secret)? {
16 let device = sqlx::query_as!( 16 let device = sqlx::query_as!(
17 Device, 17 Device,
18 r#" 18 r#"
@@ -21,7 +21,7 @@ pub async fn get(State(state): State<Arc<crate::AppState>>, headers: HeaderMap,
21 WHERE id = $1; 21 WHERE id = $1;
22 "#, 22 "#,
23 payload.id 23 payload.id
24 ).fetch_one(&state.db).await.map_err(Error::DB)?; 24 ).fetch_one(&state.db).await?;
25 25
26 debug!("got device {:?}", device); 26 debug!("got device {:?}", device);
27 27
@@ -39,7 +39,7 @@ pub struct GetDevicePayload {
39pub async fn put(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<PutDevicePayload>) -> Result<Json<Value>, Error> { 39pub async fn put(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<PutDevicePayload>) -> Result<Json<Value>, Error> {
40 info!("add device {} ({}, {}, {})", payload.id, payload.mac, payload.broadcast_addr, payload.ip); 40 info!("add device {} ({}, {}, {})", payload.id, payload.mac, payload.broadcast_addr, payload.ip);
41 let secret = headers.get("authorization"); 41 let secret = headers.get("authorization");
42 if auth(&state.config, secret).map_err(Error::Auth)? { 42 if auth(&state.config, secret)? {
43 sqlx::query!( 43 sqlx::query!(
44 r#" 44 r#"
45 INSERT INTO devices (id, mac, broadcast_addr, ip) 45 INSERT INTO devices (id, mac, broadcast_addr, ip)
@@ -49,7 +49,7 @@ pub async fn put(State(state): State<Arc<crate::AppState>>, headers: HeaderMap,
49 payload.mac, 49 payload.mac,
50 payload.broadcast_addr, 50 payload.broadcast_addr,
51 payload.ip 51 payload.ip
52 ).execute(&state.db).await.map_err(Error::DB)?; 52 ).execute(&state.db).await?;
53 53
54 Ok(Json(json!(PutDeviceResponse { success: true }))) 54 Ok(Json(json!(PutDeviceResponse { success: true })))
55 } else { 55 } else {
@@ -73,7 +73,7 @@ pub struct PutDeviceResponse {
73pub async fn post(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<PostDevicePayload>) -> Result<Json<Value>, Error> { 73pub async fn post(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<PostDevicePayload>) -> Result<Json<Value>, Error> {
74 info!("edit device {} ({}, {}, {})", payload.id, payload.mac, payload.broadcast_addr, payload.ip); 74 info!("edit device {} ({}, {}, {})", payload.id, payload.mac, payload.broadcast_addr, payload.ip);
75 let secret = headers.get("authorization"); 75 let secret = headers.get("authorization");
76 if auth(&state.config, secret).map_err(Error::Auth)? { 76 if auth(&state.config, secret)? {
77 let device = sqlx::query_as!( 77 let device = sqlx::query_as!(
78 Device, 78 Device,
79 r#" 79 r#"
@@ -85,7 +85,7 @@ pub async fn post(State(state): State<Arc<crate::AppState>>, headers: HeaderMap,
85 payload.broadcast_addr, 85 payload.broadcast_addr,
86 payload.ip, 86 payload.ip,
87 payload.id 87 payload.id
88 ).fetch_one(&state.db).await.map_err(Error::DB)?; 88 ).fetch_one(&state.db).await?;
89 89
90 Ok(Json(json!(device))) 90 Ok(Json(json!(device)))
91 } else { 91 } else {
diff --git a/src/routes/start.rs b/src/routes/start.rs
index ce95bf3..66b7cb4 100644
--- a/src/routes/start.rs
+++ b/src/routes/start.rs
@@ -20,7 +20,7 @@ pub async fn start(
20) -> Result<Json<Value>, Error> { 20) -> Result<Json<Value>, Error> {
21 info!("POST request"); 21 info!("POST request");
22 let secret = headers.get("authorization"); 22 let secret = headers.get("authorization");
23 let authorized = auth(&state.config, secret).map_err(Error::Auth)?; 23 let authorized = auth(&state.config, secret)?;
24 if authorized { 24 if authorized {
25 let device = sqlx::query_as!( 25 let device = sqlx::query_as!(
26 Device, 26 Device,
@@ -32,16 +32,15 @@ pub async fn start(
32 payload.id 32 payload.id
33 ) 33 )
34 .fetch_one(&state.db) 34 .fetch_one(&state.db)
35 .await 35 .await?;
36 .map_err(Error::DB)?;
37 36
38 info!("starting {}", device.id); 37 info!("starting {}", device.id);
39 38
40 let bind_addr = "0.0.0.0:0"; 39 let bind_addr = "0.0.0.0:0";
41 40
42 let _ = send_packet( 41 let _ = send_packet(
43 &bind_addr.parse().map_err(Error::IpParse)?, 42 bind_addr,
44 &device.broadcast_addr.parse().map_err(Error::IpParse)?, 43 &device.broadcast_addr,
45 &create_buffer(&device.mac)?, 44 &create_buffer(&device.mac)?,
46 )?; 45 )?;
47 let dev_id = device.id.clone(); 46 let dev_id = device.id.clone();