aboutsummaryrefslogtreecommitdiff
path: root/src/routes/start.rs
blob: e7d7e0e4b68b3405ce994370ddf923e6e9e88079 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use axum::headers::HeaderMap;
use axum::http::StatusCode;
use axum::Json;
use axum::response::{IntoResponse, Response};
use serde::{Deserialize, Serialize};
use std::error::Error;
use serde_json::{json, Value};
use tracing::error;
use crate::auth::{auth, AuthError};
use crate::config::SETTINGS;
use crate::wol::{create_buffer, send_packet};

pub async fn start(headers: HeaderMap, Json(payload): Json<StartPayload>) -> Result<Json<Value>, StartError> {
    let secret = headers.get("authorization");
    if auth(secret).map_err(StartError::Auth)? {
        let bind_addr = SETTINGS
            .get_string("bindaddr")
            .map_err(|err| StartError::Server(Box::new(err)))?;
        let broadcast_addr = SETTINGS
            .get_string("broadcastaddr")
            .map_err(|err| StartError::Server(Box::new(err)))?;
        let _ = send_packet(
            &bind_addr.parse().map_err(|err| StartError::Server(Box::new(err)))?,
            &broadcast_addr.parse().map_err(|err| StartError::Server(Box::new(err)))?,
            // TODO: MAC saved in DB
            create_buffer(std::env::var("MAC").unwrap().as_str()).map_err(|err| StartError::Server(Box::new(err)))?
        ).map_err(|err| StartError::Server(Box::new(err)));
        Ok(Json(json!(StartResponse { id: payload.id, boot: true })))
    } else {
        Err(StartError::Generic)
    }
}

#[derive(Deserialize)]
pub struct StartPayload {
    id: String,
    _test: Option<bool>,
}

#[derive(Serialize)]
struct StartResponse {
    id: String,
    boot: bool,
}

pub enum StartError {
    Auth(AuthError),
    Generic,
    Server(Box<dyn Error>),
}

impl IntoResponse for StartError {
    fn into_response(self) -> Response {
        let (status, error_message) = match self {
            StartError::Auth(err) => err.get(),
            StartError::Generic => (StatusCode::INTERNAL_SERVER_ERROR, ""),
            StartError::Server(err) => {
                error!("server error: {}", err.to_string());
                (StatusCode::INTERNAL_SERVER_ERROR, "Server Error")
            },

        };
        let body = Json(json!({
            "error": error_message,
        }));
        (status, body).into_response()
    }
}