From 8169dfd02216dd086d569b69d176dcd4cbd879ef Mon Sep 17 00:00:00 2001 From: Jordon Brooks Date: Sat, 24 Jun 2023 02:19:55 +0100 Subject: [PATCH 1/6] 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 From 1e5bda520a05956cc37a357a4b2af15d9d478009 Mon Sep 17 00:00:00 2001 From: Jordon Brooks Date: Sun, 25 Jun 2023 11:53:22 +0100 Subject: [PATCH 2/6] Moved battery, os docking into a new "v1" module Added all_info to a module in v1 --- src/api/endpoints_v1.rs | 7 +++---- src/api/mod.rs | 4 +--- src/main.rs | 4 +--- src/{battery => v1/all_info}/mod.rs | 0 src/{api/all_info.rs => v1/all_info/stats.rs} | 6 +++--- src/{api => v1/all_info}/structs.rs | 3 ++- src/{docking => v1/battery}/mod.rs | 0 src/{ => v1}/battery/stats.rs | 2 +- src/{ => v1}/battery/structs.rs | 0 src/{os => v1/docking}/mod.rs | 0 src/{ => v1}/docking/stats.rs | 2 +- src/{ => v1}/docking/structs.rs | 0 src/v1/mod.rs | 4 ++++ src/v1/os/mod.rs | 2 ++ src/{ => v1}/os/stats.rs | 0 src/{ => v1}/os/structs.rs | 0 16 files changed, 18 insertions(+), 16 deletions(-) rename src/{battery => v1/all_info}/mod.rs (100%) rename src/{api/all_info.rs => v1/all_info/stats.rs} (87%) rename src/{api => v1/all_info}/structs.rs (89%) rename src/{docking => v1/battery}/mod.rs (100%) rename src/{ => v1}/battery/stats.rs (96%) rename src/{ => v1}/battery/structs.rs (100%) rename src/{os => v1/docking}/mod.rs (100%) rename src/{ => v1}/docking/stats.rs (96%) rename src/{ => v1}/docking/structs.rs (100%) create mode 100644 src/v1/mod.rs create mode 100644 src/v1/os/mod.rs rename src/{ => v1}/os/stats.rs (100%) rename src/{ => v1}/os/structs.rs (100%) diff --git a/src/api/endpoints_v1.rs b/src/api/endpoints_v1.rs index 3e3c108..671628a 100644 --- a/src/api/endpoints_v1.rs +++ b/src/api/endpoints_v1.rs @@ -1,9 +1,8 @@ use actix_web::web; use actix_web::{HttpResponse, get}; -use crate::docking; +use crate::v1::{docking, os, all_info, battery}; use crate::version; -use crate::battery; #[get("/are_you_there")] pub async fn index() -> HttpResponse { @@ -12,7 +11,7 @@ pub async fn index() -> HttpResponse { #[get("/all_info")] pub async fn get_all_info() -> HttpResponse { - match crate::api::all_info::get_all_info() { + match all_info::stats::get_all_info() { Ok(info) => { #[cfg(debug_assertions)] @@ -48,7 +47,7 @@ pub async fn get_dock_info() -> HttpResponse { #[get("/os_info")] pub async fn get_os_info() -> HttpResponse { - match crate::os::stats::get_os() { + match os::stats::get_os() { Ok(info) => { #[cfg(debug_assertions)] { diff --git a/src/api/mod.rs b/src/api/mod.rs index 11801f1..960c300 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -1,4 +1,2 @@ pub mod server; -mod endpoints_v1; -mod structs; -mod all_info; \ No newline at end of file +mod endpoints_v1; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 236d956..fe9861c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,4 @@ -mod battery; -mod docking; -mod os; +mod v1; mod version; use version::info::Version; diff --git a/src/battery/mod.rs b/src/v1/all_info/mod.rs similarity index 100% rename from src/battery/mod.rs rename to src/v1/all_info/mod.rs diff --git a/src/api/all_info.rs b/src/v1/all_info/stats.rs similarity index 87% rename from src/api/all_info.rs rename to src/v1/all_info/stats.rs index e155ad3..1e3ebc6 100644 --- a/src/api/all_info.rs +++ b/src/v1/all_info/stats.rs @@ -1,6 +1,6 @@ -use crate::docking; -use crate::battery; -use crate::os; +use crate::v1::docking; +use crate::v1::battery; +use crate::v1::os; use crate::version; use super::structs::Allinfo; diff --git a/src/api/structs.rs b/src/v1/all_info/structs.rs similarity index 89% rename from src/api/structs.rs rename to src/v1/all_info/structs.rs index 5ae1d7b..6e39a11 100644 --- a/src/api/structs.rs +++ b/src/v1/all_info/structs.rs @@ -1,5 +1,6 @@ use serde::{Deserialize, Serialize}; -use crate::{os, battery, docking::{self, structs::DockInfo}, version}; +use crate::{v1::{os, battery, docking::{self, structs::DockInfo}}, version}; + #[derive(Deserialize, Serialize, Clone)] pub struct Allinfo { diff --git a/src/docking/mod.rs b/src/v1/battery/mod.rs similarity index 100% rename from src/docking/mod.rs rename to src/v1/battery/mod.rs diff --git a/src/battery/stats.rs b/src/v1/battery/stats.rs similarity index 96% rename from src/battery/stats.rs rename to src/v1/battery/stats.rs index 1a17a8d..0ffb957 100644 --- a/src/battery/stats.rs +++ b/src/v1/battery/stats.rs @@ -1,4 +1,4 @@ -use crate::battery::structs::ChargingStatus; +use crate::v1::battery::structs::ChargingStatus; use super::structs::BatteryInfo; diff --git a/src/battery/structs.rs b/src/v1/battery/structs.rs similarity index 100% rename from src/battery/structs.rs rename to src/v1/battery/structs.rs diff --git a/src/os/mod.rs b/src/v1/docking/mod.rs similarity index 100% rename from src/os/mod.rs rename to src/v1/docking/mod.rs diff --git a/src/docking/stats.rs b/src/v1/docking/stats.rs similarity index 96% rename from src/docking/stats.rs rename to src/v1/docking/stats.rs index acaa84c..6701a7c 100644 --- a/src/docking/stats.rs +++ b/src/v1/docking/stats.rs @@ -3,7 +3,7 @@ use std::{io::BufReader, fs::File}; #[allow(unused_imports)] use std::collections::hash_set; -use crate::{battery::{stats::get_battery_info, structs::ChargingStatus}, USE_FALLBACK_DOCK_DETECTION}; +use crate::{v1::{battery::{stats::get_battery_info, structs::ChargingStatus}}, USE_FALLBACK_DOCK_DETECTION}; use super::structs::{Dock, DockInfo}; diff --git a/src/docking/structs.rs b/src/v1/docking/structs.rs similarity index 100% rename from src/docking/structs.rs rename to src/v1/docking/structs.rs diff --git a/src/v1/mod.rs b/src/v1/mod.rs new file mode 100644 index 0000000..1f4beb0 --- /dev/null +++ b/src/v1/mod.rs @@ -0,0 +1,4 @@ +pub mod battery; +pub mod docking; +pub mod os; +pub mod all_info; \ No newline at end of file diff --git a/src/v1/os/mod.rs b/src/v1/os/mod.rs new file mode 100644 index 0000000..8a30527 --- /dev/null +++ b/src/v1/os/mod.rs @@ -0,0 +1,2 @@ +pub mod stats; +pub mod structs; \ No newline at end of file diff --git a/src/os/stats.rs b/src/v1/os/stats.rs similarity index 100% rename from src/os/stats.rs rename to src/v1/os/stats.rs diff --git a/src/os/structs.rs b/src/v1/os/structs.rs similarity index 100% rename from src/os/structs.rs rename to src/v1/os/structs.rs From 89b2a2ae51562b85b1c8b09bbf593ba89653209c Mon Sep 17 00:00:00 2001 From: Jordon Brooks Date: Sun, 25 Jun 2023 12:33:26 +0100 Subject: [PATCH 3/6] Added supported version variable in version struct (currently only "v1") --- src/main.rs | 2 ++ src/version/info.rs | 10 ++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index fe9861c..ea17ce1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,6 +20,8 @@ fn main() { println!("Git Describe: {}", version_info.git_describe); println!("Git Commit Timestamp: {}", version_info.git_commit_timestamp); println!("Debug Build: {}", version_info.debug); + println!("API versions: {}", version_info.supported_api_versions_to_string()); + println!("\n\n"); } diff --git a/src/version/info.rs b/src/version/info.rs index d5b4ec7..a7dc9a3 100644 --- a/src/version/info.rs +++ b/src/version/info.rs @@ -11,7 +11,8 @@ pub struct Version { pub version_major: i32, pub version_minor: i32, pub version_patch: i32, - pub version_pre: String + pub version_pre: String, + pub supported_api_versions: Vec } impl Version { @@ -26,10 +27,15 @@ impl Version { version_major: env!("CARGO_PKG_VERSION_MAJOR").parse().unwrap(), version_minor: env!("CARGO_PKG_VERSION_MINOR").parse().unwrap(), version_patch: env!("CARGO_PKG_VERSION_PATCH").parse().unwrap(), - version_pre: env!("CARGO_PKG_VERSION_PRE").parse().unwrap() + version_pre: env!("CARGO_PKG_VERSION_PRE").parse().unwrap(), + supported_api_versions: vec!["v1".to_string()] } } pub fn to_string(self) -> String { serde_json::to_string(&self).expect("Failed to parse into string") } + pub fn supported_api_versions_to_string(self) -> String { + self.supported_api_versions.join(", ") + } + } \ No newline at end of file From bb5fb386a75768950a05832a02889eb9ebe326b6 Mon Sep 17 00:00:00 2001 From: Jordon Brooks Date: Sun, 25 Jun 2023 12:36:27 +0100 Subject: [PATCH 4/6] Added a new api: "/api/supported_versions" to query the server of supported API versions --- src/api/api.rs | 14 ++++++++++++++ src/api/mod.rs | 3 ++- src/api/server.rs | 2 ++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 src/api/api.rs diff --git a/src/api/api.rs b/src/api/api.rs new file mode 100644 index 0000000..1709874 --- /dev/null +++ b/src/api/api.rs @@ -0,0 +1,14 @@ +use actix_web::{HttpResponse, get, web}; + +use crate::version::info::Version; + +#[get("/supported_versions")] +pub async fn versions() -> HttpResponse { + let version = Version::get(); + HttpResponse::Ok().json(&version.supported_api_versions) +} + +pub fn configure(cfg: &mut web::ServiceConfig) { + cfg.service(versions); + // 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 960c300..e02bcc6 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -1,2 +1,3 @@ pub mod server; -mod endpoints_v1; \ No newline at end of file +mod endpoints_v1; +mod api; \ No newline at end of file diff --git a/src/api/server.rs b/src/api/server.rs index 79f8486..d9233f5 100644 --- a/src/api/server.rs +++ b/src/api/server.rs @@ -1,6 +1,7 @@ use actix_web::{HttpServer, web}; use crate::api::endpoints_v1; +use crate::api::api; #[allow(dead_code)] pub async fn stop_actix_web(server: actix_web::dev::Server) -> std::io::Result<()> { @@ -17,6 +18,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(web::scope("/api").configure(api::configure)) .service(web::scope("/v1").configure(endpoints_v1::configure)) }) .bind(("127.0.0.1", port))? From ba6bde2c66c46f20e2bfc7a4736c880014fa0ac3 Mon Sep 17 00:00:00 2001 From: Jordon Brooks Date: Sun, 25 Jun 2023 12:38:11 +0100 Subject: [PATCH 5/6] Renamed index in endpoints_v1 to heartbeat Added heartbeat to configure --- src/api/endpoints_v1.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/api/endpoints_v1.rs b/src/api/endpoints_v1.rs index 671628a..d055b7c 100644 --- a/src/api/endpoints_v1.rs +++ b/src/api/endpoints_v1.rs @@ -5,7 +5,7 @@ use crate::v1::{docking, os, all_info, battery}; use crate::version; #[get("/are_you_there")] -pub async fn index() -> HttpResponse { +pub async fn heartbeat() -> HttpResponse { HttpResponse::Ok().body("yes") } @@ -89,6 +89,7 @@ pub async fn get_version_info() -> HttpResponse { } pub fn configure(cfg: &mut web::ServiceConfig) { + cfg.service(heartbeat); cfg.service(get_all_info); cfg.service(get_dock_info); cfg.service(get_os_info); From 74a89995474b85a89d1e5a02b3c644e529f2f1d6 Mon Sep 17 00:00:00 2001 From: Jordon Brooks Date: Mon, 26 Jun 2023 18:32:43 +0100 Subject: [PATCH 6/6] Fixed compile error on Linux --- src/v1/docking/stats.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/v1/docking/stats.rs b/src/v1/docking/stats.rs index 6701a7c..f24fc90 100644 --- a/src/v1/docking/stats.rs +++ b/src/v1/docking/stats.rs @@ -1,7 +1,7 @@ use std::{io::BufReader, fs::File}; #[allow(unused_imports)] -use std::collections::hash_set; +use std::collections::HashSet; use crate::{v1::{battery::{stats::get_battery_info, structs::ChargingStatus}}, USE_FALLBACK_DOCK_DETECTION};