spacegate_shell/ext_features/
axum.rs

1use spacegate_config::service::ListenEvent;
2use spacegate_ext_axum::axum::{self, http, routing::post, Extension, Json, Router};
3use tokio::sync::mpsc::Sender;
4#[derive(Debug, Clone)]
5pub struct App {
6    pub listen_event_tx: Sender<ListenEvent>,
7}
8/// Axum Api Router
9pub fn shell_routers(router: Router) -> Router {
10    router.nest("/control", control_routes()).route("/health", axum::routing::get(axum::Json(true))).fallback(axum::routing::any(axum::response::Html(
11        axum::body::Bytes::from_static(include_bytes!("./axum/static_resource/web-server-index.html")),
12    )))
13}
14
15pub fn control_routes() -> Router {
16    Router::new().route("/push_event", post(event))
17}
18
19pub struct HttpEventListener {}
20
21pub async fn event(state: Extension<App>, event: Json<ListenEvent>) -> Result<(), http::StatusCode> {
22    let send_result = state.listen_event_tx.send(event.0).await;
23    if let Err(_e) = send_result {
24        Err(http::StatusCode::INTERNAL_SERVER_ERROR)
25    } else {
26        Ok(())
27    }
28}