rsiot/components/cmp_http_server/routes/
get.rs

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
use axum::extract;

use crate::message::*;

use super::super::{error::Error, shared_state::TSharedState};

/// Маршрут для получения сообщений
pub async fn get<TMsg, TService>(
    extract::Path(key): extract::Path<String>,
    extract::State(shared_state): extract::State<TSharedState<TMsg, TService>>,
) -> Result<String, Error>
where
    TMsg: MsgDataBound,
    TService: ServiceBound,
{
    let shared_state = shared_state.lock().await;

    let msg = shared_state
        .msg_bus
        .recv_cache_msg(&key)
        .await
        .ok_or(Error::UnknownMessageKey(key))?;
    let json = (shared_state.config.fn_input)(&msg).map_err(Error::FnOutput)?;
    let json = match json {
        Some(json) => json,
        None => return Ok("".into()),
    };
    Ok(json)
}