From 10359f3f78100436c11b3c1fede6db1eb6396b65 Mon Sep 17 00:00:00 2001 From: xamfy Date: Wed, 5 Oct 2022 16:13:58 +0530 Subject: [PATCH 1/3] feature: websocket stats endpoint --- Cargo.lock | 54 ++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ src/handlers/handlers.rs | 40 +++++++++++++++++++++++++++++ src/main.rs | 3 +++ 4 files changed, 99 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 1acb233..2522d25 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,30 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "actix" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f728064aca1c318585bf4bb04ffcfac9e75e508ab4e8b1bd9ba5dfe04e2cbed5" +dependencies = [ + "actix-rt", + "actix_derive", + "bitflags", + "bytes", + "crossbeam-channel", + "futures-core", + "futures-sink", + "futures-task", + "futures-util", + "log", + "once_cell", + "parking_lot", + "pin-project-lite", + "smallvec", + "tokio", + "tokio-util", +] + [[package]] name = "actix-codec" version = "0.5.0" @@ -204,6 +228,23 @@ dependencies = [ "url", ] +[[package]] +name = "actix-web-actors" +version = "4.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31efe7896f3933ce03dd4710be560254272334bb321a18fd8ff62b1a557d9d19" +dependencies = [ + "actix", + "actix-codec", + "actix-http", + "actix-web", + "bytes", + "bytestring", + "futures-core", + "pin-project-lite", + "tokio", +] + [[package]] name = "actix-web-codegen" version = "4.1.0" @@ -266,6 +307,17 @@ dependencies = [ "syn", ] +[[package]] +name = "actix_derive" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d44b8fee1ced9671ba043476deddef739dd0959bf77030b26b738cc591737a7" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "adler" version = "1.0.2" @@ -2855,8 +2907,10 @@ checksum = "c811ca4a8c853ef420abd8592ba53ddbbac90410fab6903b3e79972a631f7680" name = "x-server-stats" version = "0.1.2" dependencies = [ + "actix", "actix-governor", "actix-web", + "actix-web-actors", "actix-web-lab", "askama", "askama_actix", diff --git a/Cargo.toml b/Cargo.toml index ac9b9ef..ee8d819 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,9 @@ path = "src/main.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +actix = "0.13.0" actix-web = "4" +actix-web-actors = "4.1.0" #config = "0.13.1" config = "0.12" deadpool-postgres = { version = "0.10.2", features = ["serde"] } diff --git a/src/handlers/handlers.rs b/src/handlers/handlers.rs index 6a63ff9..84364b9 100644 --- a/src/handlers/handlers.rs +++ b/src/handlers/handlers.rs @@ -7,11 +7,18 @@ use actix_web::http::header::ContentType; use actix_web::http::StatusCode; use actix_web::{get, Error, HttpResponse, Responder}; use actix_web_lab::__reexports::serde_json; +use actix_web::{get, web, App, Error, HttpRequest, HttpResponse, HttpServer}; use askama::Template; use core::fmt; use minify::html::minify; use std::fmt::Display; + +use actix::{Actor, StreamHandler}; +use actix_web_actors::ws; + +use crate::Stats; + extern crate minify; extern crate systemstat; @@ -241,3 +248,36 @@ pub fn check_whether_key(mem: &Memory, key: &str) -> String { }; return d; } + +/** +============================== + WebSocket Endpoints +============================== + */ + +/// Define HTTP actor +struct MyWs; + +impl Actor for MyWs { + type Context = ws::WebsocketContext; +} + +/// Handler for ws::Message message +impl StreamHandler> for MyWs { + fn handle(&mut self, msg: Result, ctx: &mut Self::Context) { + match msg { + Ok(ws::Message::Ping(msg)) => ctx.pong(&msg), + Ok(ws::Message::Text(text)) => ctx.text(text), + Ok(ws::Message::Binary(bin)) => ctx.binary(bin), + _ => (), + } + } +} + +#[get("/ws")] +pub async fn ws_stats_index(req: HttpRequest, stream: web::Payload) -> Result { + let resp = ws::start(MyWs {}, &req, stream); + println!("{:?}", resp); + resp +} + diff --git a/src/main.rs b/src/main.rs index 6131231..ea9f844 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,9 +10,11 @@ use std::env; use ::config::Config; use actix_governor::{Governor, GovernorConfigBuilder}; use actix_web::{middleware::Logger, web, App, HttpServer}; +use actix_web::web::ServiceConfig; use actix_web_lab::web as web_lab; use dotenv::dotenv; use handlers::index_page; +use handlers::ws_stats_index; use tokio_postgres::NoTls; use crate::config::ServerConfig; @@ -22,6 +24,7 @@ use stats::Stats; fn scoped_config(cfg: &mut web::ServiceConfig) { cfg.service(index_page); + cfg.service(ws_stats_index); } fn api_scoped_config(cfg: &mut web::ServiceConfig) { From 77770f116b51b47809d63e72ee9cc61fe2a0b442 Mon Sep 17 00:00:00 2001 From: xamfy Date: Thu, 6 Oct 2022 07:17:32 +0530 Subject: [PATCH 2/3] feature: websocket stats endpoint --- src/handlers/handlers.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/handlers/handlers.rs b/src/handlers/handlers.rs index 84364b9..cab8bca 100644 --- a/src/handlers/handlers.rs +++ b/src/handlers/handlers.rs @@ -197,6 +197,12 @@ pub async fn get_stats_from_linux(sys: PlatformImpl) -> Stats { } } +/** +============================== + HTML Endpoints +============================== + */ + #[get("")] pub async fn index_page() -> Result { let sys = System::new(); @@ -281,3 +287,10 @@ pub async fn ws_stats_index(req: HttpRequest, stream: web::Payload) -> Result Date: Sat, 15 Oct 2022 21:40:34 +0530 Subject: [PATCH 3/3] ops: fix merge conflict --- src/handlers/handlers.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/handlers/handlers.rs b/src/handlers/handlers.rs index cab8bca..858137a 100644 --- a/src/handlers/handlers.rs +++ b/src/handlers/handlers.rs @@ -5,9 +5,8 @@ use crate::Stats; use actix_web::body::BoxBody; use actix_web::http::header::ContentType; use actix_web::http::StatusCode; -use actix_web::{get, Error, HttpResponse, Responder}; use actix_web_lab::__reexports::serde_json; -use actix_web::{get, web, App, Error, HttpRequest, HttpResponse, HttpServer}; +use actix_web::{get, web, App, Error, HttpRequest, HttpResponse, HttpServer, Responder}; use askama::Template; use core::fmt; use minify::html::minify; @@ -17,7 +16,6 @@ use std::fmt::Display; use actix::{Actor, StreamHandler}; use actix_web_actors::ws; -use crate::Stats; extern crate minify;