diff options
author | FxQnLr <[email protected]> | 2024-02-15 19:29:48 +0100 |
---|---|---|
committer | FxQnLr <[email protected]> | 2024-02-15 19:29:48 +0100 |
commit | 016fa3a31f8847d3f52800941b7f8fe5ef872240 (patch) | |
tree | 59bca278b082b0801c1a4174aac856be9ccd298f /src/routes/device.rs | |
parent | 3bc7cf8ed36016ca3da9438a98f4fe8b8e6f9e61 (diff) | |
download | webol-016fa3a31f8847d3f52800941b7f8fe5ef872240.tar webol-016fa3a31f8847d3f52800941b7f8fe5ef872240.tar.gz webol-016fa3a31f8847d3f52800941b7f8fe5ef872240.zip |
Closes #15. No usable Error message right now
Diffstat (limited to 'src/routes/device.rs')
-rw-r--r-- | src/routes/device.rs | 63 |
1 files changed, 45 insertions, 18 deletions
diff --git a/src/routes/device.rs b/src/routes/device.rs index aa52cf7..5ca574a 100644 --- a/src/routes/device.rs +++ b/src/routes/device.rs | |||
@@ -1,18 +1,23 @@ | |||
1 | use std::sync::Arc; | 1 | use crate::auth::auth; |
2 | use crate::db::Device; | ||
3 | use crate::error::Error; | ||
2 | use axum::extract::State; | 4 | use axum::extract::State; |
3 | use axum::Json; | ||
4 | use axum::http::HeaderMap; | 5 | use axum::http::HeaderMap; |
6 | use axum::Json; | ||
5 | use serde::{Deserialize, Serialize}; | 7 | use serde::{Deserialize, Serialize}; |
6 | use serde_json::{json, Value}; | 8 | use serde_json::{json, Value}; |
9 | use std::sync::Arc; | ||
7 | use tracing::{debug, info}; | 10 | use tracing::{debug, info}; |
8 | use crate::auth::auth; | ||
9 | use crate::db::Device; | ||
10 | use crate::error::Error; | ||
11 | 11 | ||
12 | pub async fn get(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<GetDevicePayload>) -> Result<Json<Value>, Error> { | 12 | pub async fn get( |
13 | State(state): State<Arc<crate::AppState>>, | ||
14 | headers: HeaderMap, | ||
15 | Json(payload): Json<GetDevicePayload>, | ||
16 | ) -> Result<Json<Value>, Error> { | ||
13 | info!("add device {}", payload.id); | 17 | info!("add device {}", payload.id); |
14 | let secret = headers.get("authorization"); | 18 | let secret = headers.get("authorization"); |
15 | if auth(&state.config, secret)? { | 19 | let authorized = matches!(auth(&state.config, secret)?, crate::auth::Response::Success); |
20 | if authorized { | ||
16 | let device = sqlx::query_as!( | 21 | let device = sqlx::query_as!( |
17 | Device, | 22 | Device, |
18 | r#" | 23 | r#" |
@@ -21,7 +26,9 @@ pub async fn get(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, | |||
21 | WHERE id = $1; | 26 | WHERE id = $1; |
22 | "#, | 27 | "#, |
23 | payload.id | 28 | payload.id |
24 | ).fetch_one(&state.db).await?; | 29 | ) |
30 | .fetch_one(&state.db) | ||
31 | .await?; | ||
25 | 32 | ||
26 | debug!("got device {:?}", device); | 33 | debug!("got device {:?}", device); |
27 | 34 | ||
@@ -36,10 +43,18 @@ pub struct GetDevicePayload { | |||
36 | id: String, | 43 | id: String, |
37 | } | 44 | } |
38 | 45 | ||
39 | pub async fn put(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<PutDevicePayload>) -> Result<Json<Value>, Error> { | 46 | pub async fn put( |
40 | info!("add device {} ({}, {}, {})", payload.id, payload.mac, payload.broadcast_addr, payload.ip); | 47 | State(state): State<Arc<crate::AppState>>, |
48 | headers: HeaderMap, | ||
49 | Json(payload): Json<PutDevicePayload>, | ||
50 | ) -> Result<Json<Value>, Error> { | ||
51 | info!( | ||
52 | "add device {} ({}, {}, {})", | ||
53 | payload.id, payload.mac, payload.broadcast_addr, payload.ip | ||
54 | ); | ||
41 | let secret = headers.get("authorization"); | 55 | let secret = headers.get("authorization"); |
42 | if auth(&state.config, secret)? { | 56 | let authorized = matches!(auth(&state.config, secret)?, crate::auth::Response::Success); |
57 | if authorized { | ||
43 | sqlx::query!( | 58 | sqlx::query!( |
44 | r#" | 59 | r#" |
45 | INSERT INTO devices (id, mac, broadcast_addr, ip) | 60 | INSERT INTO devices (id, mac, broadcast_addr, ip) |
@@ -49,7 +64,9 @@ pub async fn put(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, | |||
49 | payload.mac, | 64 | payload.mac, |
50 | payload.broadcast_addr, | 65 | payload.broadcast_addr, |
51 | payload.ip | 66 | payload.ip |
52 | ).execute(&state.db).await?; | 67 | ) |
68 | .execute(&state.db) | ||
69 | .await?; | ||
53 | 70 | ||
54 | Ok(Json(json!(PutDeviceResponse { success: true }))) | 71 | Ok(Json(json!(PutDeviceResponse { success: true }))) |
55 | } else { | 72 | } else { |
@@ -62,18 +79,26 @@ pub struct PutDevicePayload { | |||
62 | id: String, | 79 | id: String, |
63 | mac: String, | 80 | mac: String, |
64 | broadcast_addr: String, | 81 | broadcast_addr: String, |
65 | ip: String | 82 | ip: String, |
66 | } | 83 | } |
67 | 84 | ||
68 | #[derive(Serialize)] | 85 | #[derive(Serialize)] |
69 | pub struct PutDeviceResponse { | 86 | pub struct PutDeviceResponse { |
70 | success: bool | 87 | success: bool, |
71 | } | 88 | } |
72 | 89 | ||
73 | pub async fn post(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<PostDevicePayload>) -> Result<Json<Value>, Error> { | 90 | pub async fn post( |
74 | info!("edit device {} ({}, {}, {})", payload.id, payload.mac, payload.broadcast_addr, payload.ip); | 91 | State(state): State<Arc<crate::AppState>>, |
92 | headers: HeaderMap, | ||
93 | Json(payload): Json<PostDevicePayload>, | ||
94 | ) -> Result<Json<Value>, Error> { | ||
95 | info!( | ||
96 | "edit device {} ({}, {}, {})", | ||
97 | payload.id, payload.mac, payload.broadcast_addr, payload.ip | ||
98 | ); | ||
75 | let secret = headers.get("authorization"); | 99 | let secret = headers.get("authorization"); |
76 | if auth(&state.config, secret)? { | 100 | let authorized = matches!(auth(&state.config, secret)?, crate::auth::Response::Success); |
101 | if authorized { | ||
77 | let device = sqlx::query_as!( | 102 | let device = sqlx::query_as!( |
78 | Device, | 103 | Device, |
79 | r#" | 104 | r#" |
@@ -85,7 +110,9 @@ pub async fn post(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, | |||
85 | payload.broadcast_addr, | 110 | payload.broadcast_addr, |
86 | payload.ip, | 111 | payload.ip, |
87 | payload.id | 112 | payload.id |
88 | ).fetch_one(&state.db).await?; | 113 | ) |
114 | .fetch_one(&state.db) | ||
115 | .await?; | ||
89 | 116 | ||
90 | Ok(Json(json!(device))) | 117 | Ok(Json(json!(device))) |
91 | } else { | 118 | } else { |