Prepare backside websocket for dashboard.

This commit is contained in:
DaLaw2 2024-05-08 12:22:15 +08:00
parent fd65edf08a
commit efec41ece3
21 changed files with 145 additions and 8 deletions

View File

@ -3,4 +3,5 @@ pub mod agent;
pub mod calculate_manager;
pub mod file_manager;
pub mod management;
pub mod monitor;
pub use Common::management::*;

71
Cargo.lock generated
View File

@ -34,8 +34,10 @@ version = "1.0.0"
dependencies = [
"chrono",
"colored",
"lazy_static",
"rust-embed",
"serde",
"sysinfo",
"tokio",
"uuid",
]
@ -55,10 +57,12 @@ version = "1.0.0"
dependencies = [
"Common",
"ab_glyph",
"actix",
"actix-files",
"actix-multipart",
"actix-rt",
"actix-web",
"actix-web-actors",
"chrono",
"futures",
"gstreamer",
@ -94,6 +98,31 @@ version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c71b1793ee61086797f5c80b6efa2b8ffa6d5dd703f118545808a7f2e27f7046"
[[package]]
name = "actix"
version = "0.13.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb72882332b6d6282f428b77ba0358cb2687e61a6f6df6a6d3871e8a177c2d4f"
dependencies = [
"actix-macros",
"actix-rt",
"actix_derive",
"bitflags 2.5.0",
"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.2"
@ -323,6 +352,24 @@ dependencies = [
"url",
]
[[package]]
name = "actix-web-actors"
version = "4.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "420b001bb709d8510c3e2659dae046e54509ff9528018d09c78381e765a1f9fa"
dependencies = [
"actix",
"actix-codec",
"actix-http",
"actix-web",
"bytes",
"bytestring",
"futures-core",
"pin-project-lite",
"tokio",
"tokio-util",
]
[[package]]
name = "actix-web-codegen"
version = "4.2.2"
@ -335,6 +382,17 @@ dependencies = [
"syn 2.0.59",
]
[[package]]
name = "actix_derive"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c7db3d5a9718568e4cf4a537cfd7070e6e6ff7481510d0237fb529ac850f6d3"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.59",
]
[[package]]
name = "addr2line"
version = "0.21.0"
@ -798,6 +856,15 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "crossbeam-channel"
version = "0.5.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab3db02a9c5b5121e1e42fbdb1aeb65f5e02624cc58c43f2884c6ccac0b82f95"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-deque"
version = "0.8.5"
@ -2620,9 +2687,9 @@ dependencies = [
[[package]]
name = "sysinfo"
version = "0.30.10"
version = "0.30.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26d7c217777061d5a2d652aea771fb9ba98b6dade657204b08c4b9604d11555b"
checksum = "732ffa00f53e6b2af46208fba5718d9662a421049204e156328b66791ffa15ae"
dependencies = [
"cfg-if",
"core-foundation-sys",

View File

@ -8,7 +8,9 @@ edition = "2021"
[dependencies]
chrono = "0.4.38"
colored = "2.1.0"
sysinfo = "0.30.12"
rust-embed = "8.3.0"
lazy_static = "1.4.0"
tokio = { version = "1.37.0", features = ["full"] }
serde = { version = "1.0.198", features = ["derive"] }
uuid = { version = "1.8.0", features = ["v4", "fast-rng", "macro-diagnostics", "serde"] }

View File

@ -1 +1,2 @@
pub mod utils;
pub mod monitor;

View File

@ -39,7 +39,7 @@ impl Monitor {
tokio::spawn(async {
Self::update_performance().await;
});
logging_information!("Monitor", "Online now");
logging_console!(information_entry!("Monitor", "Online now"));
}
pub async fn terminate() {

View File

@ -2,7 +2,7 @@ use colored::*;
use std::fmt::Display;
use chrono::{DateTime, Local};
pub use crate::{debug_entry, information_entry, notice_entry, warning_entry, error_entry, critical_entry, alert_entry, emergency_entry};
pub use crate::{debug_entry, information_entry, notice_entry, warning_entry, error_entry, critical_entry, alert_entry, emergency_entry, logging_console};
#[derive(Copy, Clone)]
pub enum LogLevel {
@ -109,6 +109,10 @@ impl Display for LogEntry {
}
}
pub fn logging_console(log_entry: LogEntry) {
println!("{}", log_entry.to_colored_string());
}
#[macro_export]
macro_rules! debug_entry {
($position:expr, $message:expr) => {
@ -188,3 +192,10 @@ macro_rules! emergency_entry {
LogEntry::new(LogLevel::Emergency, $position, $message, format!("{}:{} {}", file!(), line!(), $debug_info))
};
}
#[macro_export]
macro_rules! logging_console {
($log_entry:expr) => {
crate::utils::logging::logging_console($log_entry);
};
}

View File

@ -9,6 +9,7 @@ edition = "2021"
zip = "0.6.6"
toml = "0.8.12"
image = "0.25.1"
actix = "0.13.3"
chrono = "0.4.38"
futures = "0.3.30"
actix-rt = "2.9.0"
@ -21,6 +22,7 @@ rust-embed = "8.3.0"
actix-files = "0.6.5"
lazy_static = "1.4.0"
serde_json = "1.0.116"
actix-web-actors = "4.3.0"
actix-multipart = "0.6.1"
sanitize-filename = "0.5.0"
gstreamer-pbutils = "0.22.0"

View File

@ -4,13 +4,14 @@ use std::time::Duration;
use lazy_static::lazy_static;
use actix_web::{App, HttpServer};
use tokio::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
use crate::utils::logging::*;
use crate::utils::config::Config;
use crate::management::agent::Agent;
use crate::utils::logging::*;
use crate::management::monitor::Monitor;
use crate::management::file_manager::FileManager;
use crate::management::agent_manager::AgentManager;
use crate::connection::socket::agent_socket::AgentSocket;
use crate::web::page::{config, inference, javascript, log, misc};
use crate::web::api::{config, inference, javascript, log, misc};
lazy_static!{
static ref MANAGEMENT: RwLock<Management> = RwLock::new(Management::new());
@ -37,6 +38,7 @@ impl Management {
pub async fn run() {
FileManager::run().await;
Monitor::run().await;
Self::register_agent().await;
let http_server = loop {
let config = Config::now().await;
@ -67,6 +69,7 @@ impl Management {
pub async fn terminate() {
logging_information!("Management", "Termination in progress");
Self::instance_mut().await.terminate = true;
Monitor::terminate().await;
FileManager::terminate().await;
logging_information!("Management", "Termination complete");
}

View File

@ -5,3 +5,5 @@ pub mod file_manager;
pub mod management;
pub mod result_repository;
pub mod task_manager;
pub use Common::management::*;

View File

@ -0,0 +1,5 @@
use actix_web::{Scope, web};
pub fn initialize() -> Scope {
web::scope("/dashboard")
}

View File

@ -0,0 +1,5 @@
use actix_web::{Scope, web};
pub fn initialize() -> Scope {
web::scope("/")
}

View File

@ -1,5 +1,8 @@
pub mod inference;
pub mod config;
pub mod dashboard;
pub mod home;
pub mod javascript;
pub mod log;
pub mod misc;
pub mod websocket;

View File

@ -0,0 +1,13 @@
use actix_web_actors::ws::start;
use actix_web::{HttpRequest, Scope, web};
use actix_web::{get, HttpResponse, Error};
use crate::web::utils::websocket::WebSocket;
pub fn initialize() -> Scope {
web::scope("/websocket")
}
#[get("connect")]
async fn connect(req: HttpRequest, stream: web::Payload) -> Result<HttpResponse, Error> {
start(WebSocket {}, &req, stream)
}

View File

@ -1,2 +1,2 @@
pub mod page;
pub mod api;
pub mod utils;

View File

@ -1 +1,2 @@
pub mod response;
pub mod websocket;

View File

@ -0,0 +1,21 @@
use actix_web_actors::ws;
use actix::{Actor, StreamHandler};
pub struct WebSocket;
impl Actor for WebSocket {
type Context = ws::WebsocketContext<Self>;
}
impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for WebSocket {
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::Pong(_)) => (),
Ok(ws::Message::Text(text)) => ctx.text(text),
Ok(ws::Message::Binary(bin)) => ctx.binary(bin),
Ok(ws::Message::Close(reason)) => ctx.close(reason),
_ => (),
}
}
}