Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: websocket stats endpoint #12

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
53 changes: 52 additions & 1 deletion src/handlers/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ 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, Responder};
use askama::Template;
use core::fmt;
use minify::html::minify;
use std::fmt::Display;


use actix::{Actor, StreamHandler};
use actix_web_actors::ws;


extern crate minify;

extern crate systemstat;
Expand Down Expand Up @@ -190,6 +195,12 @@ pub async fn get_stats_from_linux(sys: PlatformImpl) -> Stats {
}
}

/**
==============================
HTML Endpoints
==============================
*/

#[get("")]
pub async fn index_page() -> Result<HttpResponse, Error> {
let sys = System::new();
Expand Down Expand Up @@ -241,3 +252,43 @@ 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<Self>;
}

/// Handler for ws::Message message
impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for MyWs {
fn handle(&mut self, msg: Result<ws::Message, ws::ProtocolError>, 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<HttpResponse, Error> {
let resp = ws::start(MyWs {}, &req, stream);
println!("{:?}", resp);
resp
}

/**
==============================
SSE Endpoints
==============================
*/


3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Check warning

Code scanning / clippy

unused import: `actix_web::web::ServiceConfig`

unused import: `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;
Expand All @@ -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) {
Expand Down