diff options
author | fx <[email protected]> | 2023-10-09 16:06:08 +0200 |
---|---|---|
committer | fx <[email protected]> | 2023-10-09 16:06:08 +0200 |
commit | 732c487d3dab4af9fc561527591d3d56299e39f2 (patch) | |
tree | 8c26464761f0c879a0aa7517e236af72cf87c6b2 /src/routes | |
parent | bc5f721de8996b48550b5069f5592caf2968e822 (diff) | |
download | webol-732c487d3dab4af9fc561527591d3d56299e39f2.tar webol-732c487d3dab4af9fc561527591d3d56299e39f2.tar.gz webol-732c487d3dab4af9fc561527591d3d56299e39f2.zip |
added db for devices
Diffstat (limited to 'src/routes')
-rw-r--r-- | src/routes/start.rs | 30 |
1 files changed, 21 insertions, 9 deletions
diff --git a/src/routes/start.rs b/src/routes/start.rs index e7d7e0e..2d505fc 100644 --- a/src/routes/start.rs +++ b/src/routes/start.rs | |||
@@ -4,28 +4,40 @@ use axum::Json; | |||
4 | use axum::response::{IntoResponse, Response}; | 4 | use axum::response::{IntoResponse, Response}; |
5 | use serde::{Deserialize, Serialize}; | 5 | use serde::{Deserialize, Serialize}; |
6 | use std::error::Error; | 6 | use std::error::Error; |
7 | use std::sync::Arc; | ||
8 | use axum::extract::State; | ||
7 | use serde_json::{json, Value}; | 9 | use serde_json::{json, Value}; |
8 | use tracing::error; | 10 | use tracing::{error, info}; |
9 | use crate::auth::{auth, AuthError}; | 11 | use crate::auth::{auth, AuthError}; |
10 | use crate::config::SETTINGS; | 12 | use crate::config::SETTINGS; |
11 | use crate::wol::{create_buffer, send_packet}; | 13 | use crate::wol::{create_buffer, send_packet}; |
14 | use crate::db::Device; | ||
12 | 15 | ||
13 | pub async fn start(headers: HeaderMap, Json(payload): Json<StartPayload>) -> Result<Json<Value>, StartError> { | 16 | pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<StartPayload>) -> Result<Json<Value>, StartError> { |
14 | let secret = headers.get("authorization"); | 17 | let secret = headers.get("authorization"); |
15 | if auth(secret).map_err(StartError::Auth)? { | 18 | if auth(secret).map_err(StartError::Auth)? { |
19 | let device = sqlx::query_as!( | ||
20 | Device, | ||
21 | r#" | ||
22 | SELECT id, mac, broadcast_addr | ||
23 | FROM devices | ||
24 | WHERE id = ?1; | ||
25 | "#, | ||
26 | payload.id | ||
27 | ).fetch_one(&state.db).await.map_err(|err| StartError::Server(Box::new(err)))?; | ||
28 | |||
29 | info!("starting {}", device.id); | ||
30 | |||
16 | let bind_addr = SETTINGS | 31 | let bind_addr = SETTINGS |
17 | .get_string("bindaddr") | 32 | .get_string("bindaddr") |
18 | .map_err(|err| StartError::Server(Box::new(err)))?; | 33 | .map_err(|err| StartError::Server(Box::new(err)))?; |
19 | let broadcast_addr = SETTINGS | 34 | |
20 | .get_string("broadcastaddr") | ||
21 | .map_err(|err| StartError::Server(Box::new(err)))?; | ||
22 | let _ = send_packet( | 35 | let _ = send_packet( |
23 | &bind_addr.parse().map_err(|err| StartError::Server(Box::new(err)))?, | 36 | &bind_addr.parse().map_err(|err| StartError::Server(Box::new(err)))?, |
24 | &broadcast_addr.parse().map_err(|err| StartError::Server(Box::new(err)))?, | 37 | &device.broadcast_addr.parse().map_err(|err| StartError::Server(Box::new(err)))?, |
25 | // TODO: MAC saved in DB | 38 | create_buffer(&device.mac).map_err(|err| StartError::Server(Box::new(err)))? |
26 | create_buffer(std::env::var("MAC").unwrap().as_str()).map_err(|err| StartError::Server(Box::new(err)))? | ||
27 | ).map_err(|err| StartError::Server(Box::new(err))); | 39 | ).map_err(|err| StartError::Server(Box::new(err))); |
28 | Ok(Json(json!(StartResponse { id: payload.id, boot: true }))) | 40 | Ok(Json(json!(StartResponse { id: device.id, boot: true }))) |
29 | } else { | 41 | } else { |
30 | Err(StartError::Generic) | 42 | Err(StartError::Generic) |
31 | } | 43 | } |