summaryrefslogtreecommitdiff
path: root/src/routes/device.rs
diff options
context:
space:
mode:
authorFxQnLr <[email protected]>2024-02-18 21:16:46 +0100
committerFxQnLr <[email protected]>2024-02-18 21:16:46 +0100
commit2f9f18b80a9e2134f674f345e48a5f21de5efadd (patch)
treec4202bb5c1a490233e89d928cf8c5b91258d4c90 /src/routes/device.rs
parent016fa3a31f8847d3f52800941b7f8fe5ef872240 (diff)
downloadwebol-2f9f18b80a9e2134f674f345e48a5f21de5efadd.tar
webol-2f9f18b80a9e2134f674f345e48a5f21de5efadd.tar.gz
webol-2f9f18b80a9e2134f674f345e48a5f21de5efadd.zip
Refactor stuff. Use Postgres Types
Diffstat (limited to 'src/routes/device.rs')
-rw-r--r--src/routes/device.rs19
1 files changed, 13 insertions, 6 deletions
diff --git a/src/routes/device.rs b/src/routes/device.rs
index 5ca574a..2f0093d 100644
--- a/src/routes/device.rs
+++ b/src/routes/device.rs
@@ -4,9 +4,11 @@ use crate::error::Error;
4use axum::extract::State; 4use axum::extract::State;
5use axum::http::HeaderMap; 5use axum::http::HeaderMap;
6use axum::Json; 6use axum::Json;
7use mac_address::MacAddress;
7use serde::{Deserialize, Serialize}; 8use serde::{Deserialize, Serialize};
8use serde_json::{json, Value}; 9use serde_json::{json, Value};
9use std::sync::Arc; 10use sqlx::types::ipnetwork::IpNetwork;
11use std::{sync::Arc, str::FromStr};
10use tracing::{debug, info}; 12use tracing::{debug, info};
11 13
12pub async fn get( 14pub async fn get(
@@ -14,7 +16,7 @@ pub async fn get(
14 headers: HeaderMap, 16 headers: HeaderMap,
15 Json(payload): Json<GetDevicePayload>, 17 Json(payload): Json<GetDevicePayload>,
16) -> Result<Json<Value>, Error> { 18) -> Result<Json<Value>, Error> {
17 info!("add device {}", payload.id); 19 info!("get device {}", payload.id);
18 let secret = headers.get("authorization"); 20 let secret = headers.get("authorization");
19 let authorized = matches!(auth(&state.config, secret)?, crate::auth::Response::Success); 21 let authorized = matches!(auth(&state.config, secret)?, crate::auth::Response::Success);
20 if authorized { 22 if authorized {
@@ -52,18 +54,21 @@ pub async fn put(
52 "add device {} ({}, {}, {})", 54 "add device {} ({}, {}, {})",
53 payload.id, payload.mac, payload.broadcast_addr, payload.ip 55 payload.id, payload.mac, payload.broadcast_addr, payload.ip
54 ); 56 );
57
55 let secret = headers.get("authorization"); 58 let secret = headers.get("authorization");
56 let authorized = matches!(auth(&state.config, secret)?, crate::auth::Response::Success); 59 let authorized = matches!(auth(&state.config, secret)?, crate::auth::Response::Success);
57 if authorized { 60 if authorized {
61 let ip = IpNetwork::from_str(&payload.ip)?;
62 let mac = MacAddress::from_str(&payload.mac)?;
58 sqlx::query!( 63 sqlx::query!(
59 r#" 64 r#"
60 INSERT INTO devices (id, mac, broadcast_addr, ip) 65 INSERT INTO devices (id, mac, broadcast_addr, ip)
61 VALUES ($1, $2, $3, $4); 66 VALUES ($1, $2, $3, $4);
62 "#, 67 "#,
63 payload.id, 68 payload.id,
64 payload.mac, 69 mac,
65 payload.broadcast_addr, 70 payload.broadcast_addr,
66 payload.ip 71 ip
67 ) 72 )
68 .execute(&state.db) 73 .execute(&state.db)
69 .await?; 74 .await?;
@@ -99,6 +104,8 @@ pub async fn post(
99 let secret = headers.get("authorization"); 104 let secret = headers.get("authorization");
100 let authorized = matches!(auth(&state.config, secret)?, crate::auth::Response::Success); 105 let authorized = matches!(auth(&state.config, secret)?, crate::auth::Response::Success);
101 if authorized { 106 if authorized {
107 let ip = IpNetwork::from_str(&payload.ip)?;
108 let mac = MacAddress::from_str(&payload.mac)?;
102 let device = sqlx::query_as!( 109 let device = sqlx::query_as!(
103 Device, 110 Device,
104 r#" 111 r#"
@@ -106,9 +113,9 @@ pub async fn post(
106 SET mac = $1, broadcast_addr = $2, ip = $3 WHERE id = $4 113 SET mac = $1, broadcast_addr = $2, ip = $3 WHERE id = $4
107 RETURNING id, mac, broadcast_addr, ip, times; 114 RETURNING id, mac, broadcast_addr, ip, times;
108 "#, 115 "#,
109 payload.mac, 116 mac,
110 payload.broadcast_addr, 117 payload.broadcast_addr,
111 payload.ip, 118 ip,
112 payload.id 119 payload.id
113 ) 120 )
114 .fetch_one(&state.db) 121 .fetch_one(&state.db)