From 8169dfd02216dd086d569b69d176dcd4cbd879ef Mon Sep 17 00:00:00 2001 From: Jordon Brooks Date: Sat, 24 Jun 2023 02:19:55 +0100 Subject: [PATCH] Renamed endpoints.rs to endpoints_v1.rs APIs will now use /v1/... --- src/api/{endpoints.rs => endpoints_v1.rs} | 36 ++++++++++++++++++++++- src/api/mod.rs | 2 +- src/api/server.rs | 11 ++----- src/api/structs.rs | 5 +++- src/battery/structs.rs | 7 +++-- src/docking/structs.rs | 10 +++++-- src/os/structs.rs | 9 ++++-- src/version/info.rs | 5 +++- 8 files changed, 66 insertions(+), 19 deletions(-) rename src/api/{endpoints.rs => endpoints_v1.rs} (56%) diff --git a/src/api/endpoints.rs b/src/api/endpoints_v1.rs similarity index 56% rename from src/api/endpoints.rs rename to src/api/endpoints_v1.rs index 5134775..3e3c108 100644 --- a/src/api/endpoints.rs +++ b/src/api/endpoints_v1.rs @@ -1,3 +1,4 @@ +use actix_web::web; use actix_web::{HttpResponse, get}; use crate::docking; @@ -13,9 +14,15 @@ pub async fn index() -> HttpResponse { pub async fn get_all_info() -> HttpResponse { match crate::api::all_info::get_all_info() { Ok(info) => { + + #[cfg(debug_assertions)] + { + println!("Successfully got all info: {}", &info.clone().to_string()); + } HttpResponse::Ok().json(&info) }, Err(err) => { + eprintln!("Failed to get all info: {}", err); HttpResponse::InternalServerError().body(format!("Failed to get device info: {}", err)) } } @@ -25,9 +32,14 @@ pub async fn get_all_info() -> HttpResponse { pub async fn get_dock_info() -> HttpResponse { match docking::stats::get_dock() { Ok(info) => { + #[cfg(debug_assertions)] + { + println!("Successfully got dock info: {}", &info.clone().to_string()); + } HttpResponse::Ok().json(&info) }, Err(err) => { + eprintln!("Failed to get dock info: {}", err); HttpResponse::InternalServerError().body(format!("Failed to get dock info: {}", err)) } } @@ -38,9 +50,14 @@ pub async fn get_dock_info() -> HttpResponse { pub async fn get_os_info() -> HttpResponse { match crate::os::stats::get_os() { Ok(info) => { + #[cfg(debug_assertions)] + { + println!("Successfully got os info: {}", &info.clone().to_string()); + } HttpResponse::Ok().json(&info) }, Err(err) => { + eprintln!("Failed to get os info: {}", err); HttpResponse::InternalServerError().body(format!("Failed to get OS info: {}", err)) } } @@ -50,9 +67,14 @@ pub async fn get_os_info() -> HttpResponse { pub async fn get_battery_info() -> HttpResponse { match battery::stats::get_battery_info() { Ok(info) => { + #[cfg(debug_assertions)] + { + println!("Successfully got battery info: {}", &info.clone().to_string()); + } HttpResponse::Ok().json(&info) }, Err(err) => { + eprintln!("Failed to get battery info: {}", err); HttpResponse::InternalServerError().body(format!("Failed to get battery info: {}", err)) } } @@ -60,6 +82,18 @@ pub async fn get_battery_info() -> HttpResponse { #[get("/version_info")] pub async fn get_version_info() -> HttpResponse { - + #[cfg(debug_assertions)] + { + println!("Successfully got version info: {}", version::info::Version::get().to_string()); + } HttpResponse::Ok().json(&version::info::Version::get()) +} + +pub fn configure(cfg: &mut web::ServiceConfig) { + cfg.service(get_all_info); + cfg.service(get_dock_info); + cfg.service(get_os_info); + cfg.service(get_battery_info); + cfg.service(get_version_info); + // Register other version 1 handlers here... } \ No newline at end of file diff --git a/src/api/mod.rs b/src/api/mod.rs index 4816e81..11801f1 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -1,4 +1,4 @@ pub mod server; -mod endpoints; +mod endpoints_v1; mod structs; mod all_info; \ No newline at end of file diff --git a/src/api/server.rs b/src/api/server.rs index 251dbfd..79f8486 100644 --- a/src/api/server.rs +++ b/src/api/server.rs @@ -1,6 +1,6 @@ -use actix_web::HttpServer; +use actix_web::{HttpServer, web}; -use crate::api::endpoints::*; +use crate::api::endpoints_v1; #[allow(dead_code)] pub async fn stop_actix_web(server: actix_web::dev::Server) -> std::io::Result<()> { @@ -17,12 +17,7 @@ pub fn start_actix_web(port: u16) -> std::io::Result { let logger = actix_web::middleware::Logger::default(); actix_web::App::new() .wrap(logger) - .service(index) - .service(get_all_info) - .service(get_os_info) - .service(get_battery_info) - .service(get_version_info) - .service(get_dock_info) + .service(web::scope("/v1").configure(endpoints_v1::configure)) }) .bind(("127.0.0.1", port))? .run(); diff --git a/src/api/structs.rs b/src/api/structs.rs index c036712..5ae1d7b 100644 --- a/src/api/structs.rs +++ b/src/api/structs.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use crate::{os, battery, docking::{self, structs::DockInfo}, version}; -#[derive(Deserialize, Serialize)] +#[derive(Deserialize, Serialize, Clone)] pub struct Allinfo { pub os: os::structs::OSInfo, pub battery: battery::structs::BatteryInfo, @@ -17,4 +17,7 @@ impl Allinfo { version: version::info::Version::get() } } + pub fn to_string(self) -> String { + serde_json::to_string(&self).expect("Failed to parse into string") + } } \ No newline at end of file diff --git a/src/battery/structs.rs b/src/battery/structs.rs index fb7e1e2..2f9f202 100644 --- a/src/battery/structs.rs +++ b/src/battery/structs.rs @@ -1,13 +1,13 @@ use serde::{Deserialize, Serialize}; -#[derive(Deserialize, Serialize, PartialEq)] +#[derive(Deserialize, Serialize, PartialEq, Clone)] pub enum ChargingStatus { Charging, Battery, UNKNOWN, } -#[derive(Deserialize, Serialize)] +#[derive(Deserialize, Serialize, Clone)] pub struct BatteryInfo { pub has_battery: bool, pub battery_percent: i8, @@ -22,4 +22,7 @@ impl BatteryInfo { charging_status: ChargingStatus::UNKNOWN } } + pub fn to_string(self) -> String { + serde_json::to_string(&self).expect("Failed to parse into string") + } } \ No newline at end of file diff --git a/src/docking/structs.rs b/src/docking/structs.rs index 3133435..4a165f2 100644 --- a/src/docking/structs.rs +++ b/src/docking/structs.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; -#[derive(Deserialize, Serialize)] +#[derive(Deserialize, Serialize, Clone)] pub struct Dock { pub brand: String, // ex: JSAUX pub model: String, // ex: HB0603 @@ -14,9 +14,12 @@ impl Dock { usb_ids: vec![], } } + pub fn to_string(self) -> String { + serde_json::to_string(&self).expect("Failed to parse into string") + } } -#[derive(Deserialize, Serialize)] +#[derive(Deserialize, Serialize, Clone)] pub struct DockInfo { pub dock_info: Dock, pub is_docked: bool, @@ -30,4 +33,7 @@ impl DockInfo { fallback_detection: false } } + pub fn to_string(self) -> String { + serde_json::to_string(&self).expect("Failed to parse into string") + } } \ No newline at end of file diff --git a/src/os/structs.rs b/src/os/structs.rs index be941d1..48db188 100644 --- a/src/os/structs.rs +++ b/src/os/structs.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; -#[derive(Deserialize, Serialize, Debug, PartialEq)] +#[derive(Deserialize, Serialize, PartialEq, Clone)] pub enum Platform { WINDOWS = 0, LINUX = 1, @@ -8,14 +8,14 @@ pub enum Platform { UNKNOWN = 255 } -#[derive(Deserialize, Serialize)] +#[derive(Deserialize, Serialize, Clone)] pub enum Architecture { X86 = 0, X86_64 = 1, UNKNOWN = 255, } -#[derive(Deserialize, Serialize)] +#[derive(Deserialize, Serialize, Clone)] pub struct OSInfo { pub platform: Platform, // Windows, Mac, Linux pub name: String, // "Windows 11 2306", "Ubuntu 22.04 LTS" @@ -32,4 +32,7 @@ impl OSInfo { bits: Architecture::UNKNOWN } } + pub fn to_string(self) -> String { + serde_json::to_string(&self).expect("Failed to parse into string") + } } \ No newline at end of file diff --git a/src/version/info.rs b/src/version/info.rs index 90c006b..d5b4ec7 100644 --- a/src/version/info.rs +++ b/src/version/info.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; -#[derive(Deserialize, Serialize)] +#[derive(Deserialize, Serialize, Clone)] pub struct Version { pub build_timestamp: String, pub git_branch: String, @@ -29,4 +29,7 @@ impl Version { version_pre: env!("CARGO_PKG_VERSION_PRE").parse().unwrap() } } + pub fn to_string(self) -> String { + serde_json::to_string(&self).expect("Failed to parse into string") + } } \ No newline at end of file