diff options
Diffstat (limited to 'src/routes/device.rs')
-rw-r--r-- | src/routes/device.rs | 19 |
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; | |||
4 | use axum::extract::State; | 4 | use axum::extract::State; |
5 | use axum::http::HeaderMap; | 5 | use axum::http::HeaderMap; |
6 | use axum::Json; | 6 | use axum::Json; |
7 | use mac_address::MacAddress; | ||
7 | use serde::{Deserialize, Serialize}; | 8 | use serde::{Deserialize, Serialize}; |
8 | use serde_json::{json, Value}; | 9 | use serde_json::{json, Value}; |
9 | use std::sync::Arc; | 10 | use sqlx::types::ipnetwork::IpNetwork; |
11 | use std::{sync::Arc, str::FromStr}; | ||
10 | use tracing::{debug, info}; | 12 | use tracing::{debug, info}; |
11 | 13 | ||
12 | pub async fn get( | 14 | pub 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) |