diff options
| author | Fuwn <[email protected]> | 2021-08-04 16:05:42 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2021-08-04 16:05:42 -0700 |
| commit | a8063b0cc5f4eec4710081cbb0679a78f5b601bc (patch) | |
| tree | 6fc87e74234e2f4a324fe55d0d81343307344d3d /crates/whirl_api/src/routes/stats | |
| parent | fix(whirl): signal handling not working (diff) | |
| download | whirl-a8063b0cc5f4eec4710081cbb0679a78f5b601bc.tar.xz whirl-a8063b0cc5f4eec4710081cbb0679a78f5b601bc.zip | |
refactor(whirl_api): migrate to Axum
The Tokio group recently released a new web-framework called Axum, and being that almost every
aspect of Whirl harnesses Tokio's pre-existing libraries; I think it's more fitting to migrate the
web-server over to a Tokio-based one as well!
Diffstat (limited to 'crates/whirl_api/src/routes/stats')
| -rw-r--r-- | crates/whirl_api/src/routes/stats/mod.rs | 34 |
1 files changed, 19 insertions, 15 deletions
diff --git a/crates/whirl_api/src/routes/stats/mod.rs b/crates/whirl_api/src/routes/stats/mod.rs index 5ec5eee..595b441 100644 --- a/crates/whirl_api/src/routes/stats/mod.rs +++ b/crates/whirl_api/src/routes/stats/mod.rs @@ -5,7 +5,7 @@ pub mod structures; use std::convert::TryFrom; -use actix_web::HttpResponse; +use axum::response; use num_traits::cast::AsPrimitive; use sysinfo::{get_current_pid, ProcessExt, System, SystemExt}; @@ -13,23 +13,27 @@ use crate::routes::stats::structures::{Statistics, StatisticsProcess, Statistics // This is mostly for developmental testing, it consumes more CPU than it's // worth. -pub fn statistics() -> HttpResponse { +#[allow(clippy::unused_async)] +pub async fn statistics() -> impl response::IntoResponse { let mut sys = System::new_all(); sys.refresh_all(); let process = sys.process(get_current_pid().unwrap()).unwrap(); - HttpResponse::Ok().json(Statistics { - system: StatisticsSystem { - os_type: sys.name().unwrap(), - release: sys.kernel_version().unwrap(), - uptime: whirl_common::system::unixts_to_hrtime(usize::try_from(sys.uptime()).unwrap()), - }, - process: StatisticsProcess { - // (process.cpu_usage() * 100.0).round() / 100.0 - memory_usage: (process.memory() / 1000).to_string(), - cpu_usage: (process.cpu_usage() / sys.processors().len().as_(): f32).to_string(), - // uptime: seconds_to_hrtime((sys.get_uptime() - process.start_time()) as usize), - }, - }) + ( + hyper::StatusCode::OK, + response::Json(Statistics { + system: StatisticsSystem { + os_type: sys.name().unwrap(), + release: sys.kernel_version().unwrap(), + uptime: whirl_common::system::unixts_to_hrtime(usize::try_from(sys.uptime()).unwrap()), + }, + process: StatisticsProcess { + // (process.cpu_usage() * 100.0).round() / 100.0 + memory_usage: (process.memory() / 1000).to_string(), + cpu_usage: (process.cpu_usage() / sys.processors().len().as_(): f32).to_string(), + // uptime: seconds_to_hrtime((sys.get_uptime() - process.start_time()) as usize), + }, + }), + ) } |