mirror of
https://github.com/csehviktor/status-monitor.git
synced 2025-08-08 18:06:14 +02:00
delete directory
This commit is contained in:
@@ -1,58 +0,0 @@
|
||||
use chrono::{Duration, Utc};
|
||||
use std::sync::Arc;
|
||||
use warp::{Filter, Rejection, Reply, reply::json};
|
||||
|
||||
use crate::storage::StorageRepository;
|
||||
|
||||
pub struct HttpRoutes {
|
||||
storage: Arc<dyn StorageRepository>,
|
||||
}
|
||||
|
||||
impl HttpRoutes {
|
||||
pub fn new(storage: Arc<dyn StorageRepository>) -> Self {
|
||||
Self { storage }
|
||||
}
|
||||
|
||||
pub fn routes(self: Arc<Self>) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
|
||||
let agents_storage = self.storage.clone();
|
||||
let history_storage = self.storage.clone();
|
||||
|
||||
let agents_route = warp::path!("agents")
|
||||
.and(warp::get())
|
||||
.and(warp::any().map(move || agents_storage.clone()))
|
||||
.and_then(Self::get_agents);
|
||||
|
||||
let history_route = warp::path!("history" / String / String)
|
||||
.and(warp::get())
|
||||
.and(warp::any().map(move || history_storage.clone()))
|
||||
.and_then(Self::get_history);
|
||||
|
||||
agents_route.or(history_route)
|
||||
}
|
||||
|
||||
async fn get_agents(storage: Arc<dyn StorageRepository>) -> Result<impl Reply, Rejection> {
|
||||
let agents = storage.get_agents().await.unwrap();
|
||||
|
||||
Ok(json(&agents))
|
||||
}
|
||||
|
||||
async fn get_history(
|
||||
agent: String,
|
||||
dur: String,
|
||||
storage: Arc<dyn StorageRepository>,
|
||||
) -> Result<impl Reply, Rejection> {
|
||||
let now = Utc::now();
|
||||
|
||||
let duration = match dur.as_str() {
|
||||
"hour" => Some(now - Duration::hours(1)),
|
||||
"day" => Some(now - Duration::days(1)),
|
||||
"week" => Some(now - Duration::weeks(1)),
|
||||
"month" => Some(now - Duration::weeks(4)),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
let history = storage.get_history(&agent, duration).await.unwrap();
|
||||
|
||||
Ok(json(&history))
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
use http::HttpRoutes;
|
||||
use std::sync::Arc;
|
||||
use warp::Filter;
|
||||
use websocket::WebsocketRoutes;
|
||||
|
||||
use crate::{bridge::ClientManager, storage::StorageRepositoryImpl};
|
||||
|
||||
pub mod http;
|
||||
pub mod websocket;
|
||||
|
||||
pub struct Server {
|
||||
clients: Arc<ClientManager>,
|
||||
storage: Arc<StorageRepositoryImpl>,
|
||||
}
|
||||
|
||||
impl Server {
|
||||
pub fn new(clients: Arc<ClientManager>, storage: Arc<StorageRepositoryImpl>) -> Self {
|
||||
Self { clients, storage }
|
||||
}
|
||||
|
||||
pub async fn serve(&self) {
|
||||
let http_routes = Arc::new(HttpRoutes::new(self.storage.inner()));
|
||||
let ws_routes = Arc::new(WebsocketRoutes::new(self.clients.clone()));
|
||||
|
||||
let cors = warp::cors().allow_any_origin().allow_methods(vec!["GET"]);
|
||||
|
||||
let routes = http_routes.routes().with(cors).or(ws_routes.routes());
|
||||
|
||||
println!("starting server on :{}", 3000);
|
||||
warp::serve(routes).run(([0, 0, 0, 0], 3000)).await;
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
use common::StatusMessage;
|
||||
use futures_util::{SinkExt, StreamExt};
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::mpsc::channel;
|
||||
use warp::{
|
||||
Filter, Rejection, Reply,
|
||||
filters::ws::{Message, WebSocket},
|
||||
};
|
||||
|
||||
use crate::bridge::ClientManager;
|
||||
|
||||
pub struct WebsocketRoutes {
|
||||
clients: Arc<ClientManager>,
|
||||
}
|
||||
|
||||
impl WebsocketRoutes {
|
||||
pub fn new(clients: Arc<ClientManager>) -> Self {
|
||||
Self { clients }
|
||||
}
|
||||
|
||||
pub fn routes(self: Arc<Self>) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
|
||||
warp::path!("ws" / String)
|
||||
.and(warp::get())
|
||||
.and(warp::ws())
|
||||
.and(warp::any().map(move || self.clients.clone()))
|
||||
.map(
|
||||
|agent: String, websocket: warp::ws::Ws, clients: Arc<ClientManager>| {
|
||||
websocket.on_upgrade(move |websocket| {
|
||||
Self::handle_ws_connection(agent, websocket, clients)
|
||||
})
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
async fn handle_ws_connection(
|
||||
agent: String,
|
||||
websocket: WebSocket,
|
||||
clients: Arc<ClientManager>,
|
||||
) {
|
||||
let (mut ws_tx, _) = websocket.split();
|
||||
let (tx, mut rx) = channel::<StatusMessage>(100);
|
||||
|
||||
clients.add_client(tx).await;
|
||||
|
||||
tokio::spawn(async move {
|
||||
while let Some(msg) = rx.recv().await {
|
||||
if msg.agent != agent {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ws_tx.send(Message::text(msg)).await.is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user