aboutsummaryrefslogtreecommitdiff
path: root/crates/whirl_api
diff options
context:
space:
mode:
authorFuwn <[email protected]>2021-05-20 17:05:59 +0000
committerFuwn <[email protected]>2021-05-20 17:05:59 +0000
commit9dbc613765de8ab7dfa8e1374cf6661dcfd56bc8 (patch)
tree8cfff6a23bb72db2660e68c63a8cf9d0539a061f /crates/whirl_api
parentfeat(readme): add sqlfluff as a dev dep (diff)
downloadwhirl-9dbc613765de8ab7dfa8e1374cf6661dcfd56bc8.tar.xz
whirl-9dbc613765de8ab7dfa8e1374cf6661dcfd56bc8.zip
refactor(global): move crates around, stricter module isolation
Diffstat (limited to 'crates/whirl_api')
-rw-r--r--crates/whirl_api/Cargo.toml30
-rw-r--r--crates/whirl_api/src/lib.rs46
-rw-r--r--crates/whirl_api/src/routes/mod.rs4
-rw-r--r--crates/whirl_api/src/routes/stats/mod.rs33
-rw-r--r--crates/whirl_api/src/routes/stats/structures.rs21
5 files changed, 134 insertions, 0 deletions
diff --git a/crates/whirl_api/Cargo.toml b/crates/whirl_api/Cargo.toml
new file mode 100644
index 0000000..7687b42
--- /dev/null
+++ b/crates/whirl_api/Cargo.toml
@@ -0,0 +1,30 @@
+[package]
+name = "whirl_api"
+version = "0.1.0"
+authors = ["Fuwn <[email protected]>"]
+edition = "2018"
+description = "Whirl, an open-source WorldServer implementation in Rust."
+documentation = "https://whirlsplash.org/docs/"
+readme = "../../README.rst"
+homepage = "https://whirlsplash.org"
+repository = "https://github.com/Whirlsplash/whirl"
+license = "GPL-3.0-only"
+# license-file = "LICENSE"
+keywords = ["rust", "worldserver", "whirl", "whirlsplash"]
+publish = false
+
+[dependencies]
+# Web-server
+actix-web = { version = "3.3.2", features = ["rustls"] }
+actix-cors = "0.5.4"
+
+# Utility
+sysinfo = "0.17.5"
+whirl_common = { path = "../whirl_common" }
+
+# Serialization
+serde = "1.0.126"
+serde_derive = "1.0.126"
+
+# Logging
+log = "0.4.14"
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,
+}