Skip to content

Commit

Permalink
Add stats route
Browse files Browse the repository at this point in the history
  • Loading branch information
de-luca committed Jul 1, 2023
1 parent c44d303 commit 8a6efbb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::request;
use crate::response;
use crate::room::Room;

use crate::response::{Created, Info, Joined};
use crate::response::{Created, Info, Joined, Stats};
use log::info;
use response::{Error, Payload, Signal};
use std::collections::HashMap;
Expand All @@ -29,6 +29,13 @@ impl Handler {
}
}

pub(crate) fn get_stats(&self) -> Stats {
Stats {
peers: self.peers.read().unwrap().len(),
rooms: self.rooms.read().unwrap().len(),
}
}

pub(crate) fn add_peer(&self, tx: Tx) -> Uuid {
let id = Uuid::new_v4();
self.peers.write().unwrap().insert(id, tx);
Expand Down
12 changes: 10 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,20 @@ async fn main() {
tokio::spawn(register_cleaner(handler.clone()));
info!("Registered room cleaner");

let handler = warp::any().map(move || handler.clone());

let addr: SocketAddr = options.addr.parse().expect("Cannot parse addr!");

let routes = warp::ws()
.and(warp::any().map(move || handler.clone()))
let beacon = warp::ws()
.and(handler.clone())
.map(|ws: warp::ws::Ws, handler| ws.on_upgrade(move |ws| handle_connection(ws, handler)));

let stats = warp::get()
.and(handler.clone())
.map(|handler: Handler| warp::reply::json(&handler.get_stats()));

let routes = beacon.or(stats);

if options.cert.is_some() && options.key.is_some() {
info!("Starting with TLS");
warp::serve(routes)
Expand Down
6 changes: 6 additions & 0 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,9 @@ pub struct Signal {
pub enum Error {
RoomDoesNotExists,
}

#[derive(Serialize, Debug)]
pub struct Stats {
pub peers: usize,
pub rooms: usize,
}

0 comments on commit 8a6efbb

Please sign in to comment.