diff options
| author | Fuwn <[email protected]> | 2021-05-10 15:50:51 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2021-05-10 15:55:30 +0000 |
| commit | 0a97100e9c2c550ae4141142cb06b05804b4c2e7 (patch) | |
| tree | 073f1eb758b311372d90470fe304f3d923844c98 /src | |
| parent | Merge pull request #16 from Whirlsplash/renovate/commitizen-4.x (diff) | |
| download | whirl-0a97100e9c2c550ae4141142cb06b05804b4c2e7.tar.xz whirl-0a97100e9c2c550ae4141142cb06b05804b4c2e7.zip | |
perf(api): use actix_web instead of rocket for api
Refactors API for Actix, also enables CORS.
BREAKING CHANGE: `whirlsplash.port.api` configuration key created.
closes #20
Diffstat (limited to 'src')
| -rw-r--r-- | src/api/mod.rs | 27 | ||||
| -rw-r--r-- | src/api/routes/mod.rs | 3 | ||||
| -rw-r--r-- | src/api/routes/stats/mod.rs | 11 | ||||
| -rw-r--r-- | src/config/Whirl.default.toml | 1 | ||||
| -rw-r--r-- | src/config/mod.rs | 8 | ||||
| -rw-r--r-- | src/lib.rs | 2 | ||||
| -rw-r--r-- | src/subs.rs | 10 |
7 files changed, 45 insertions, 17 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), }, }) } diff --git a/src/config/Whirl.default.toml b/src/config/Whirl.default.toml index be63635..ed6a874 100644 --- a/src/config/Whirl.default.toml +++ b/src/config/Whirl.default.toml @@ -3,6 +3,7 @@ worldsmaster_username = "WORLDSMASTER" # DO NOT CHANGE UNLESS YOU KNOW WHAT YOU log_level = 1 # Generally, you should only change this if you are experiencing issues ip = "0.0.0.0" # DO NOT CHANGE UNLESS YOU KNOW WHAT YOU ARE DOING prompt_ps1 = "[WORLDSMASTER@Whirlsplash ~]$" +api.port = 8080 [distributor] worldsmaster_greeting = "Welcome to Whirlsplash!" diff --git a/src/config/mod.rs b/src/config/mod.rs index ef86810..6b50086 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -9,6 +9,11 @@ pub struct WhirlsplashConfig { pub log_level: i64, pub ip: String, pub prompt_ps1: String, + pub api: WhirlsplashApiConfig, +} +#[derive(Serialize, Deserialize, Debug)] +pub struct WhirlsplashApiConfig { + pub port: i64, } #[derive(Serialize, Deserialize, Debug)] pub struct DistributorConfig { @@ -54,6 +59,9 @@ impl Default for Config { log_level: 1, ip: "0.0.0.0".to_string(), prompt_ps1: "[WORLDSMASTER@Whirlsplash ~]$".to_string(), + api: WhirlsplashApiConfig { + port: 80 + }, }, distributor: DistributorConfig { worldsmaster_greeting: "Welcome to Whirlsplash!".to_string(), @@ -14,8 +14,6 @@ #[macro_use] extern crate log; #[macro_use] -extern crate rocket; -#[macro_use] extern crate diesel; #[macro_use] extern crate serde_derive; diff --git a/src/subs.rs b/src/subs.rs index bf979de..0a7af5e 100644 --- a/src/subs.rs +++ b/src/subs.rs @@ -14,6 +14,8 @@ use crate::{ }; pub async fn run() -> ! { + let (tx, _rx) = std::sync::mpsc::channel(); + let _threads = vec![ tokio::spawn(async move { let _ = Distributor::listen( @@ -26,7 +28,11 @@ pub async fn run() -> ! { let _ = Hub::listen(&*format!("0.0.0.0:{}", Config::get().hub.port), RoomServer).await; }), tokio::spawn(async move { - let _ = Api::listen(); + let _ = Api::listen( + tx, + &*format!("0.0.0.0:{}", Config::get().whirlsplash.api.port), + ) + .await; }), ]; @@ -39,4 +45,6 @@ pub async fn run() -> ! { std::thread::sleep(std::time::Duration::from_secs(2)); Prompt::handle(); } + + // actix_web::rt::System::new("").block_on(rx.recv().unwrap().stop(true)); } |