From 76c89b47fd74e069f9db73503a5131a5a60b8516 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Sat, 10 Aug 2024 21:21:34 +0200 Subject: add 'devices' path to request all available devices --- src/main.rs | 4 +++- src/routes.rs | 1 + src/routes/devices.rs | 24 ++++++++++++++++++++++++ src/storage.rs | 25 ++++++++++++++++++++++--- 4 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 src/routes/devices.rs (limited to 'src') diff --git a/src/main.rs b/src/main.rs index b550dd8..edee184 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use crate::{ config::Config, - routes::{device, start, status}, + routes::{device, devices, start, status}, services::ping::{BroadcastCommand, StatusMap}, storage::Device, }; @@ -36,6 +36,7 @@ mod wol; device::get, device::post, device::put, + devices::get, ), components( schemas( @@ -113,6 +114,7 @@ async fn main() -> color_eyre::eyre::Result<()> { .route("/start/:id", post(start::post).get(start::get)) .route("/device", post(device::post).put(device::put)) .route("/device/:id", get(device::get)) + .route("/devices", get(devices::get)) .route("/status", get(status::status)) .route_layer(from_fn_with_state(shared_state.clone(), auth::auth)) .merge(SwaggerUi::new("/swagger-ui").url("/api-docs/openapi.json", ApiDoc::openapi())) diff --git a/src/routes.rs b/src/routes.rs index a72f27b..b6ad590 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -1,3 +1,4 @@ pub mod start; pub mod device; +pub mod devices; pub mod status; diff --git a/src/routes/devices.rs b/src/routes/devices.rs new file mode 100644 index 0000000..616441c --- /dev/null +++ b/src/routes/devices.rs @@ -0,0 +1,24 @@ +use crate::error::Error; +use crate::storage::Device; +use axum::Json; +use serde_json::{json, Value}; +use tracing::{debug, info}; + +#[utoipa::path( + get, + path = "/devices", + responses( + (status = 200, description = "Get an array of all `Device`s", body = [Vec]) + ), + security((), ("api_key" = [])) +)] +pub async fn get( +) -> Result, Error> { + info!("get all devices"); + + let devices = Device::read_all()?; + + debug!("got devices"); + + Ok(Json(json!(devices))) +} diff --git a/src/storage.rs b/src/storage.rs index 90ff1b4..e069875 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -1,5 +1,6 @@ use std::{ - fs::{create_dir_all, File}, + ffi::OsStr, + fs::{create_dir_all, File, read_dir}, io::{Read, Write}, path::Path, }; @@ -26,14 +27,14 @@ impl Device { const STORAGE_PATH: &'static str = "devices"; pub fn setup() -> Result { - trace!("check for storage at {}", Self::STORAGE_PATH); + trace!("check for storage STORAGE_PATH=\"{}\"", Self::STORAGE_PATH); let sp = Path::new(Self::STORAGE_PATH); if !sp.exists() { warn!("device storage path doesn't exist, creating it"); create_dir_all(Self::STORAGE_PATH)?; }; - debug!("device storage at '{}'", Self::STORAGE_PATH); + debug!("device storage STORAGE_PATH=\"{}\"", Self::STORAGE_PATH); Ok(Self::STORAGE_PATH.to_string()) } @@ -49,6 +50,24 @@ impl Device { Ok(dev) } + pub fn read_all() -> Result, Error> { + trace!("attempt to read all files"); + let st_path = read_dir(Self::STORAGE_PATH)?; + + let mut devices = vec![]; + for file_path in st_path { + let file_path = file_path?; + if file_path.path().extension() != Some(OsStr::new("json")) { continue; }; + let mut file = File::open(file_path.path())?; + let mut buf = String::new(); + file.read_to_string(&mut buf)?; + trace!(?file, ?buf, "read successfully from file"); + devices.push(serde_json::from_str(&buf)?); + } + + Ok(devices) + } + pub fn write(&self) -> Result<(), Error> { trace!(?self.id, ?self, "attempt to write to file"); let mut file = File::create(format!("{}/{}.json", Self::STORAGE_PATH, self.id))?; -- cgit v1.2.3