Merge branch 'feature/Versioned_API' into dev

This commit is contained in:
Jordon Brooks 2023-06-26 21:34:43 +01:00
commit 2e098ae0f4
19 changed files with 113 additions and 38 deletions

14
src/api/api.rs Normal file
View file

@ -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...
}

View file

@ -1,21 +1,27 @@
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 {
pub async fn heartbeat() -> HttpResponse {
HttpResponse::Ok().body("yes")
}
#[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)]
{
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 +31,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))
}
}
@ -36,11 +47,16 @@ 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)]
{
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 +66,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 +81,19 @@ 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(heartbeat);
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...
}

View file

@ -1,4 +1,3 @@
pub mod server;
mod endpoints;
mod structs;
mod all_info;
mod endpoints_v1;
mod api;

View file

@ -1,6 +1,7 @@
use actix_web::HttpServer;
use actix_web::{HttpServer, web};
use crate::api::endpoints::*;
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,12 +18,8 @@ pub fn start_actix_web(port: u16) -> std::io::Result<actix_web::dev::Server> {
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("/api").configure(api::configure))
.service(web::scope("/v1").configure(endpoints_v1::configure))
})
.bind(("127.0.0.1", port))?
.run();

View file

@ -1,6 +1,4 @@
mod battery;
mod docking;
mod os;
mod v1;
mod version;
use version::info::Version;
@ -22,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");
}

View file

@ -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;

View file

@ -1,7 +1,8 @@
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)]
#[derive(Deserialize, Serialize, Clone)]
pub struct Allinfo {
pub os: os::structs::OSInfo,
pub battery: battery::structs::BatteryInfo,
@ -17,4 +18,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")
}
}

View file

@ -1,4 +1,4 @@
use crate::battery::structs::ChargingStatus;
use crate::v1::battery::structs::ChargingStatus;
use super::structs::BatteryInfo;

View file

@ -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")
}
}

View file

@ -1,9 +1,9 @@
use std::{io::BufReader, fs::File};
#[allow(unused_imports)]
use std::collections::hash_set;
use std::collections::HashSet;
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};

View file

@ -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")
}
}

4
src/v1/mod.rs Normal file
View file

@ -0,0 +1,4 @@
pub mod battery;
pub mod docking;
pub mod os;
pub mod all_info;

2
src/v1/os/mod.rs Normal file
View file

@ -0,0 +1,2 @@
pub mod stats;
pub mod structs;

View file

@ -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")
}
}

View file

@ -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,
@ -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<String>
}
impl Version {
@ -26,7 +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(", ")
}
}