Renamed endpoints.rs to endpoints_v1.rs

APIs will now use /v1/...
This commit is contained in:
Jordon Brooks 2023-06-24 02:19:55 +01:00
parent 560b90be55
commit 8169dfd022
8 changed files with 66 additions and 19 deletions

View file

@ -1,3 +1,4 @@
use actix_web::web;
use actix_web::{HttpResponse, get}; use actix_web::{HttpResponse, get};
use crate::docking; use crate::docking;
@ -13,9 +14,15 @@ pub async fn index() -> HttpResponse {
pub async fn get_all_info() -> HttpResponse { pub async fn get_all_info() -> HttpResponse {
match crate::api::all_info::get_all_info() { match crate::api::all_info::get_all_info() {
Ok(info) => { Ok(info) => {
#[cfg(debug_assertions)]
{
println!("Successfully got all info: {}", &info.clone().to_string());
}
HttpResponse::Ok().json(&info) HttpResponse::Ok().json(&info)
}, },
Err(err) => { Err(err) => {
eprintln!("Failed to get all info: {}", err);
HttpResponse::InternalServerError().body(format!("Failed to get device 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 { pub async fn get_dock_info() -> HttpResponse {
match docking::stats::get_dock() { match docking::stats::get_dock() {
Ok(info) => { Ok(info) => {
#[cfg(debug_assertions)]
{
println!("Successfully got dock info: {}", &info.clone().to_string());
}
HttpResponse::Ok().json(&info) HttpResponse::Ok().json(&info)
}, },
Err(err) => { Err(err) => {
eprintln!("Failed to get dock info: {}", err);
HttpResponse::InternalServerError().body(format!("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 { pub async fn get_os_info() -> HttpResponse {
match crate::os::stats::get_os() { match crate::os::stats::get_os() {
Ok(info) => { Ok(info) => {
#[cfg(debug_assertions)]
{
println!("Successfully got os info: {}", &info.clone().to_string());
}
HttpResponse::Ok().json(&info) HttpResponse::Ok().json(&info)
}, },
Err(err) => { Err(err) => {
eprintln!("Failed to get os info: {}", err);
HttpResponse::InternalServerError().body(format!("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 { pub async fn get_battery_info() -> HttpResponse {
match battery::stats::get_battery_info() { match battery::stats::get_battery_info() {
Ok(info) => { Ok(info) => {
#[cfg(debug_assertions)]
{
println!("Successfully got battery info: {}", &info.clone().to_string());
}
HttpResponse::Ok().json(&info) HttpResponse::Ok().json(&info)
}, },
Err(err) => { Err(err) => {
eprintln!("Failed to get battery info: {}", err);
HttpResponse::InternalServerError().body(format!("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")] #[get("/version_info")]
pub async fn get_version_info() -> HttpResponse { 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()) 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...
}

View file

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

View file

@ -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)] #[allow(dead_code)]
pub async fn stop_actix_web(server: actix_web::dev::Server) -> std::io::Result<()> { 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<actix_web::dev::Server> {
let logger = actix_web::middleware::Logger::default(); let logger = actix_web::middleware::Logger::default();
actix_web::App::new() actix_web::App::new()
.wrap(logger) .wrap(logger)
.service(index) .service(web::scope("/v1").configure(endpoints_v1::configure))
.service(get_all_info)
.service(get_os_info)
.service(get_battery_info)
.service(get_version_info)
.service(get_dock_info)
}) })
.bind(("127.0.0.1", port))? .bind(("127.0.0.1", port))?
.run(); .run();

View file

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::{os, battery, docking::{self, structs::DockInfo}, version}; use crate::{os, battery, docking::{self, structs::DockInfo}, version};
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize, Clone)]
pub struct Allinfo { pub struct Allinfo {
pub os: os::structs::OSInfo, pub os: os::structs::OSInfo,
pub battery: battery::structs::BatteryInfo, pub battery: battery::structs::BatteryInfo,
@ -17,4 +17,7 @@ impl Allinfo {
version: version::info::Version::get() 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,13 +1,13 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize, PartialEq)] #[derive(Deserialize, Serialize, PartialEq, Clone)]
pub enum ChargingStatus { pub enum ChargingStatus {
Charging, Charging,
Battery, Battery,
UNKNOWN, UNKNOWN,
} }
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize, Clone)]
pub struct BatteryInfo { pub struct BatteryInfo {
pub has_battery: bool, pub has_battery: bool,
pub battery_percent: i8, pub battery_percent: i8,
@ -22,4 +22,7 @@ impl BatteryInfo {
charging_status: ChargingStatus::UNKNOWN charging_status: ChargingStatus::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}; use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize, Clone)]
pub struct Dock { pub struct Dock {
pub brand: String, // ex: JSAUX pub brand: String, // ex: JSAUX
pub model: String, // ex: HB0603 pub model: String, // ex: HB0603
@ -14,9 +14,12 @@ impl Dock {
usb_ids: vec![], 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 struct DockInfo {
pub dock_info: Dock, pub dock_info: Dock,
pub is_docked: bool, pub is_docked: bool,
@ -30,4 +33,7 @@ impl DockInfo {
fallback_detection: false fallback_detection: false
} }
} }
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}; use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize, Debug, PartialEq)] #[derive(Deserialize, Serialize, PartialEq, Clone)]
pub enum Platform { pub enum Platform {
WINDOWS = 0, WINDOWS = 0,
LINUX = 1, LINUX = 1,
@ -8,14 +8,14 @@ pub enum Platform {
UNKNOWN = 255 UNKNOWN = 255
} }
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize, Clone)]
pub enum Architecture { pub enum Architecture {
X86 = 0, X86 = 0,
X86_64 = 1, X86_64 = 1,
UNKNOWN = 255, UNKNOWN = 255,
} }
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize, Clone)]
pub struct OSInfo { pub struct OSInfo {
pub platform: Platform, // Windows, Mac, Linux pub platform: Platform, // Windows, Mac, Linux
pub name: String, // "Windows 11 2306", "Ubuntu 22.04 LTS" pub name: String, // "Windows 11 2306", "Ubuntu 22.04 LTS"
@ -32,4 +32,7 @@ impl OSInfo {
bits: Architecture::UNKNOWN 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}; use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize, Clone)]
pub struct Version { pub struct Version {
pub build_timestamp: String, pub build_timestamp: String,
pub git_branch: String, pub git_branch: String,
@ -29,4 +29,7 @@ impl Version {
version_pre: env!("CARGO_PKG_VERSION_PRE").parse().unwrap() 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")
}
} }