Renamed endpoints.rs to endpoints_v1.rs
APIs will now use /v1/...
This commit is contained in:
parent
560b90be55
commit
8169dfd022
8 changed files with 66 additions and 19 deletions
|
@ -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...
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
pub mod server;
|
||||
mod endpoints;
|
||||
mod endpoints_v1;
|
||||
mod structs;
|
||||
mod all_info;
|
|
@ -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<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("/v1").configure(endpoints_v1::configure))
|
||||
})
|
||||
.bind(("127.0.0.1", port))?
|
||||
.run();
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
}
|
|
@ -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")
|
||||
}
|
||||
}
|
|
@ -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")
|
||||
}
|
||||
}
|
|
@ -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")
|
||||
}
|
||||
}
|
|
@ -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")
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue