aboutsummaryrefslogtreecommitdiff
path: root/src/api
diff options
context:
space:
mode:
Diffstat (limited to 'src/api')
-rw-r--r--src/api/mod.rs27
-rw-r--r--src/api/routes/mod.rs3
-rw-r--r--src/api/routes/stats/mod.rs11
3 files changed, 27 insertions, 14 deletions
diff --git a/src/api/mod.rs b/src/api/mod.rs
index 17ffea4..c744f3d 100644
--- a/src/api/mod.rs
+++ b/src/api/mod.rs
@@ -1,14 +1,31 @@
// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective
// SPDX-License-Identifier: GPL-3.0-only
+use actix_web::web::resource;
+
mod routes;
pub struct Api;
impl Api {
- pub fn listen() {
- let _ = rocket::ignite()
- .mount("/", routes![routes::index])
- .mount("/api/v1", routes![routes::stats::statistics])
- .launch();
+ 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/src/api/routes/mod.rs b/src/api/routes/mod.rs
index 231236c..f5a2ff4 100644
--- a/src/api/routes/mod.rs
+++ b/src/api/routes/mod.rs
@@ -2,6 +2,3 @@
// SPDX-License-Identifier: GPL-3.0-only
pub mod stats;
-
-#[get("/")]
-pub fn index() -> &'static str { "Whirlsplash" }
diff --git a/src/api/routes/stats/mod.rs b/src/api/routes/stats/mod.rs
index f7af6e8..04ce5e6 100644
--- a/src/api/routes/stats/mod.rs
+++ b/src/api/routes/stats/mod.rs
@@ -3,7 +3,7 @@
pub mod structures;
-use rocket_contrib::json::Json;
+use actix_web::HttpResponse;
use sysinfo::{get_current_pid, ProcessExt, System, SystemExt};
use crate::{
@@ -13,24 +13,23 @@ use crate::{
// This is mostly for developmental testing, it consumes more CPU than it's
// worth.
-#[get("/statistics")]
-pub fn statistics() -> Json<Statistics> {
+pub fn statistics() -> HttpResponse {
let mut sys = System::new_all();
sys.refresh_all();
let process = sys.get_process(get_current_pid().unwrap()).unwrap();
- Json(Statistics {
+ 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 {
- memory_usage: (process.memory() / 1000).to_string(),
// (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),
+ // uptime: seconds_to_hrtime((sys.get_uptime() - process.start_time()) as usize),
},
})
}