diff options
author | fx <[email protected]> | 2023-10-09 17:26:59 +0200 |
---|---|---|
committer | fx <[email protected]> | 2023-10-09 17:26:59 +0200 |
commit | 3e6a72428824c5a50a873a4284b86d0a9e47a778 (patch) | |
tree | 7f3594f4068a8009210039bc33e0205a672828f7 /src/routes/start.rs | |
parent | 732c487d3dab4af9fc561527591d3d56299e39f2 (diff) | |
download | webol-3e6a72428824c5a50a873a4284b86d0a9e47a778.tar webol-3e6a72428824c5a50a873a4284b86d0a9e47a778.tar.gz webol-3e6a72428824c5a50a873a4284b86d0a9e47a778.zip |
db int for api
Diffstat (limited to 'src/routes/start.rs')
-rw-r--r-- | src/routes/start.rs | 52 |
1 files changed, 13 insertions, 39 deletions
diff --git a/src/routes/start.rs b/src/routes/start.rs index 2d505fc..d16ea4e 100644 --- a/src/routes/start.rs +++ b/src/routes/start.rs | |||
@@ -1,45 +1,43 @@ | |||
1 | use axum::headers::HeaderMap; | 1 | use axum::headers::HeaderMap; |
2 | use axum::http::StatusCode; | ||
3 | use axum::Json; | 2 | use axum::Json; |
4 | use axum::response::{IntoResponse, Response}; | ||
5 | use serde::{Deserialize, Serialize}; | 3 | use serde::{Deserialize, Serialize}; |
6 | use std::error::Error; | ||
7 | use std::sync::Arc; | 4 | use std::sync::Arc; |
8 | use axum::extract::State; | 5 | use axum::extract::State; |
9 | use serde_json::{json, Value}; | 6 | use serde_json::{json, Value}; |
10 | use tracing::{error, info}; | 7 | use tracing::info; |
11 | use crate::auth::{auth, AuthError}; | 8 | use crate::auth::auth; |
12 | use crate::config::SETTINGS; | 9 | use crate::config::SETTINGS; |
13 | use crate::wol::{create_buffer, send_packet}; | 10 | use crate::wol::{create_buffer, send_packet}; |
14 | use crate::db::Device; | 11 | use crate::db::Device; |
12 | use crate::error::WebolError; | ||
15 | 13 | ||
16 | pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<StartPayload>) -> Result<Json<Value>, StartError> { | 14 | pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<StartPayload>) -> Result<Json<Value>, WebolError> { |
17 | let secret = headers.get("authorization"); | 15 | let secret = headers.get("authorization"); |
18 | if auth(secret).map_err(StartError::Auth)? { | 16 | if auth(secret).map_err(WebolError::Auth)? { |
19 | let device = sqlx::query_as!( | 17 | let device = sqlx::query_as!( |
20 | Device, | 18 | Device, |
21 | r#" | 19 | r#" |
22 | SELECT id, mac, broadcast_addr | 20 | SELECT id, mac, broadcast_addr |
23 | FROM devices | 21 | FROM devices |
24 | WHERE id = ?1; | 22 | WHERE id = $1; |
25 | "#, | 23 | "#, |
26 | payload.id | 24 | payload.id |
27 | ).fetch_one(&state.db).await.map_err(|err| StartError::Server(Box::new(err)))?; | 25 | ).fetch_one(&state.db).await.map_err(|err| WebolError::Server(Box::new(err)))?; |
28 | 26 | ||
29 | info!("starting {}", device.id); | 27 | info!("starting {}", device.id); |
30 | 28 | ||
31 | let bind_addr = SETTINGS | 29 | let bind_addr = SETTINGS |
32 | .get_string("bindaddr") | 30 | .get_string("bindaddr") |
33 | .map_err(|err| StartError::Server(Box::new(err)))?; | 31 | .map_err(|err| WebolError::Server(Box::new(err)))?; |
34 | 32 | ||
35 | let _ = send_packet( | 33 | let _ = send_packet( |
36 | &bind_addr.parse().map_err(|err| StartError::Server(Box::new(err)))?, | 34 | &bind_addr.parse().map_err(|err| WebolError::Server(Box::new(err)))?, |
37 | &device.broadcast_addr.parse().map_err(|err| StartError::Server(Box::new(err)))?, | 35 | &device.broadcast_addr.parse().map_err(|err| WebolError::Server(Box::new(err)))?, |
38 | create_buffer(&device.mac).map_err(|err| StartError::Server(Box::new(err)))? | 36 | create_buffer(&device.mac).map_err(|err| WebolError::Server(Box::new(err)))? |
39 | ).map_err(|err| StartError::Server(Box::new(err))); | 37 | ).map_err(|err| WebolError::Server(Box::new(err))); |
40 | Ok(Json(json!(StartResponse { id: device.id, boot: true }))) | 38 | Ok(Json(json!(StartResponse { id: device.id, boot: true }))) |
41 | } else { | 39 | } else { |
42 | Err(StartError::Generic) | 40 | Err(WebolError::Generic) |
43 | } | 41 | } |
44 | } | 42 | } |
45 | 43 | ||
@@ -53,28 +51,4 @@ pub struct StartPayload { | |||
53 | struct StartResponse { | 51 | struct StartResponse { |
54 | id: String, | 52 | id: String, |
55 | boot: bool, | 53 | boot: bool, |
56 | } | ||
57 | |||
58 | pub enum StartError { | ||
59 | Auth(AuthError), | ||
60 | Generic, | ||
61 | Server(Box<dyn Error>), | ||
62 | } | ||
63 | |||
64 | impl IntoResponse for StartError { | ||
65 | fn into_response(self) -> Response { | ||
66 | let (status, error_message) = match self { | ||
67 | StartError::Auth(err) => err.get(), | ||
68 | StartError::Generic => (StatusCode::INTERNAL_SERVER_ERROR, ""), | ||
69 | StartError::Server(err) => { | ||
70 | error!("server error: {}", err.to_string()); | ||
71 | (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") | ||
72 | }, | ||
73 | |||
74 | }; | ||
75 | let body = Json(json!({ | ||
76 | "error": error_message, | ||
77 | })); | ||
78 | (status, body).into_response() | ||
79 | } | ||
80 | } \ No newline at end of file | 54 | } \ No newline at end of file |