diff options
| author | Fuwn <[email protected]> | 2021-05-20 17:05:59 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2021-05-20 17:05:59 -0700 |
| commit | 9e2121baf98b6fdc15cde6c387a7845a0b3f95d6 (patch) | |
| tree | 15460f59799a9f655ac5b213e4b8a8903d1e57e4 /crates/whirl_api/src | |
| parent | feat(readme): add sqlfluff as a dev dep (diff) | |
| download | whirl-9e2121baf98b6fdc15cde6c387a7845a0b3f95d6.tar.xz whirl-9e2121baf98b6fdc15cde6c387a7845a0b3f95d6.zip | |
refactor(global): move crates around, stricter module isolation
Diffstat (limited to 'crates/whirl_api/src')
| -rw-r--r-- | crates/whirl_api/src/lib.rs | 46 | ||||
| -rw-r--r-- | crates/whirl_api/src/routes/mod.rs | 4 | ||||
| -rw-r--r-- | crates/whirl_api/src/routes/stats/mod.rs | 33 | ||||
| -rw-r--r-- | crates/whirl_api/src/routes/stats/structures.rs | 21 |
4 files changed, 104 insertions, 0 deletions
diff --git a/crates/whirl_api/src/lib.rs b/crates/whirl_api/src/lib.rs new file mode 100644 index 0000000..e77e7d3 --- /dev/null +++ b/crates/whirl_api/src/lib.rs @@ -0,0 +1,46 @@ +// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective +// SPDX-License-Identifier: GPL-3.0-only + +#![feature( + type_ascription, + hash_set_entry, + type_name_of_val, + decl_macro, + proc_macro_hygiene +)] +#![warn(rust_2018_idioms)] +#![recursion_limit = "128"] + +#[macro_use] +extern crate log; +#[macro_use] +extern crate serde_derive; + +use actix_web::web::resource; + +mod routes; + +pub struct Api; +impl Api { + pub async fn listen( + tx: std::sync::mpsc::Sender<actix_web::dev::Server>, + address: &str, + ) -> std::io::Result<()> { + let mut sys = actix_web::rt::System::new("api"); + + let server = actix_web::HttpServer::new(|| { + actix_web::App::new() + .wrap(actix_cors::Cors::default().allow_any_origin()) + .service(resource("/").to(|| async { "Whirlsplash" })) + .service(resource("/api/v1/statistics").to(routes::stats::statistics)) + }) + .bind(address)? + .run(); + + info!("http api now listening at {}", address); + + let _ = tx.send(server.clone()); + + sys.block_on(server) + } +} diff --git a/crates/whirl_api/src/routes/mod.rs b/crates/whirl_api/src/routes/mod.rs new file mode 100644 index 0000000..f5a2ff4 --- /dev/null +++ b/crates/whirl_api/src/routes/mod.rs @@ -0,0 +1,4 @@ +// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective +// SPDX-License-Identifier: GPL-3.0-only + +pub mod stats; diff --git a/crates/whirl_api/src/routes/stats/mod.rs b/crates/whirl_api/src/routes/stats/mod.rs new file mode 100644 index 0000000..d5d0937 --- /dev/null +++ b/crates/whirl_api/src/routes/stats/mod.rs @@ -0,0 +1,33 @@ +// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective +// SPDX-License-Identifier: GPL-3.0-only + +pub mod structures; + +use actix_web::HttpResponse; +use sysinfo::{get_current_pid, ProcessExt, System, SystemExt}; +use whirl_common::system::seconds_to_hrtime; + +use crate::routes::stats::structures::{Statistics, StatisticsProcess, StatisticsSystem}; + +// This is mostly for developmental testing, it consumes more CPU than it's +// worth. +pub fn statistics() -> HttpResponse { + let mut sys = System::new_all(); + sys.refresh_all(); + + let process = sys.get_process(get_current_pid().unwrap()).unwrap(); + + HttpResponse::Ok().json(Statistics { + system: StatisticsSystem { + os_type: sys.get_name().unwrap(), + release: sys.get_kernel_version().unwrap(), + uptime: seconds_to_hrtime(sysinfo::System::new().get_uptime() as usize), + }, + process: StatisticsProcess { + // (process.cpu_usage() * 100.0).round() / 100.0 + memory_usage: (process.memory() / 1000).to_string(), + cpu_usage: (process.cpu_usage() / sys.get_processors().len() as f32).to_string(), + // uptime: seconds_to_hrtime((sys.get_uptime() - process.start_time()) as usize), + }, + }) +} diff --git a/crates/whirl_api/src/routes/stats/structures.rs b/crates/whirl_api/src/routes/stats/structures.rs new file mode 100644 index 0000000..88fc852 --- /dev/null +++ b/crates/whirl_api/src/routes/stats/structures.rs @@ -0,0 +1,21 @@ +// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective +// SPDX-License-Identifier: GPL-3.0-only + +#[derive(Serialize)] +pub struct Statistics { + pub system: StatisticsSystem, + pub process: StatisticsProcess, +} +#[derive(Serialize)] +pub struct StatisticsSystem { + #[serde(rename = "type")] + pub os_type: String, + pub release: String, + pub uptime: String, +} +#[derive(Serialize)] +pub struct StatisticsProcess { + pub memory_usage: String, + pub cpu_usage: String, + // pub uptime: String, +} |