Skip to content

Commit

Permalink
feature: websocket stats endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
xamfy committed Oct 5, 2022
1 parent 084a0c4 commit 750426b
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
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.

3 changes: 3 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 All @@ -49,3 +51,4 @@ systemstat = "0.2.0"

nats = "0.23.0" #https://github.com/nats-io/nats.rs
kafka = "0.9" #https://github.com/kafka-rust/kafka-rust

39 changes: 38 additions & 1 deletion src/handlers/handlers.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use actix_web::http::StatusCode;
use actix_web::{get, web, Error, HttpResponse};
use actix_web::{get, web, App, Error, HttpRequest, HttpResponse, HttpServer};
use askama::Template;
use deadpool_postgres::{Pool};
use minify::html::minify;


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

use crate::Stats;

extern crate minify;
Expand Down Expand Up @@ -86,3 +90,36 @@ pub async fn index_page() -> Result<HttpResponse, Error> {
.content_type("text/html; charset=utf-8")
.body(html_str))
}

/**
==============================
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
}

3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,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;
Expand All @@ -20,6 +22,7 @@ use stats::Stats;

fn scoped_config(cfg: &mut web::ServiceConfig) {
cfg.service(index_page);
cfg.service(ws_stats_index);
}

#[actix_web::main]
Expand Down

0 comments on commit 750426b

Please sign in to comment.