diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 4 | ||||
-rw-r--r-- | src/routes.rs | 1 | ||||
-rw-r--r-- | src/routes/devices.rs | 24 | ||||
-rw-r--r-- | src/storage.rs | 25 |
4 files changed, 50 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs index 352023b..7878669 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | use crate::{ | 1 | use crate::{ |
2 | config::Config, | 2 | config::Config, |
3 | routes::{device, start, status}, | 3 | routes::{device, devices, start, status}, |
4 | services::ping::{BroadcastCommand, StatusMap}, | 4 | services::ping::{BroadcastCommand, StatusMap}, |
5 | storage::Device, | 5 | storage::Device, |
6 | }; | 6 | }; |
@@ -36,6 +36,7 @@ mod wol; | |||
36 | device::get, | 36 | device::get, |
37 | device::post, | 37 | device::post, |
38 | device::put, | 38 | device::put, |
39 | devices::get, | ||
39 | ), | 40 | ), |
40 | components( | 41 | components( |
41 | schemas( | 42 | schemas( |
@@ -113,6 +114,7 @@ async fn main() -> color_eyre::eyre::Result<()> { | |||
113 | .route("/start/:id", post(start::post).get(start::get)) | 114 | .route("/start/:id", post(start::post).get(start::get)) |
114 | .route("/device", post(device::post).put(device::put)) | 115 | .route("/device", post(device::post).put(device::put)) |
115 | .route("/device/:id", get(device::get)) | 116 | .route("/device/:id", get(device::get)) |
117 | .route("/devices", get(devices::get)) | ||
116 | .route("/status", get(status::status)) | 118 | .route("/status", get(status::status)) |
117 | .route_layer(from_fn_with_state(shared_state.clone(), auth::auth)) | 119 | .route_layer(from_fn_with_state(shared_state.clone(), auth::auth)) |
118 | .merge(SwaggerUi::new("/swagger-ui").url("/api-docs/openapi.json", ApiDoc::openapi())) | 120 | .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 @@ | |||
1 | pub mod start; | 1 | pub mod start; |
2 | pub mod device; | 2 | pub mod device; |
3 | pub mod devices; | ||
3 | pub mod status; | 4 | 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 @@ | |||
1 | use crate::error::Error; | ||
2 | use crate::storage::Device; | ||
3 | use axum::Json; | ||
4 | use serde_json::{json, Value}; | ||
5 | use tracing::{debug, info}; | ||
6 | |||
7 | #[utoipa::path( | ||
8 | get, | ||
9 | path = "/devices", | ||
10 | responses( | ||
11 | (status = 200, description = "Get an array of all `Device`s", body = [Vec<Device>]) | ||
12 | ), | ||
13 | security((), ("api_key" = [])) | ||
14 | )] | ||
15 | pub async fn get( | ||
16 | ) -> Result<Json<Value>, Error> { | ||
17 | info!("get all devices"); | ||
18 | |||
19 | let devices = Device::read_all()?; | ||
20 | |||
21 | debug!("got devices"); | ||
22 | |||
23 | Ok(Json(json!(devices))) | ||
24 | } | ||
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 @@ | |||
1 | use std::{ | 1 | use std::{ |
2 | fs::{create_dir_all, File}, | 2 | ffi::OsStr, |
3 | fs::{create_dir_all, File, read_dir}, | ||
3 | io::{Read, Write}, | 4 | io::{Read, Write}, |
4 | path::Path, | 5 | path::Path, |
5 | }; | 6 | }; |
@@ -26,14 +27,14 @@ impl Device { | |||
26 | const STORAGE_PATH: &'static str = "devices"; | 27 | const STORAGE_PATH: &'static str = "devices"; |
27 | 28 | ||
28 | pub fn setup() -> Result<String, Error> { | 29 | pub fn setup() -> Result<String, Error> { |
29 | trace!("check for storage at {}", Self::STORAGE_PATH); | 30 | trace!("check for storage STORAGE_PATH=\"{}\"", Self::STORAGE_PATH); |
30 | let sp = Path::new(Self::STORAGE_PATH); | 31 | let sp = Path::new(Self::STORAGE_PATH); |
31 | if !sp.exists() { | 32 | if !sp.exists() { |
32 | warn!("device storage path doesn't exist, creating it"); | 33 | warn!("device storage path doesn't exist, creating it"); |
33 | create_dir_all(Self::STORAGE_PATH)?; | 34 | create_dir_all(Self::STORAGE_PATH)?; |
34 | }; | 35 | }; |
35 | 36 | ||
36 | debug!("device storage at '{}'", Self::STORAGE_PATH); | 37 | debug!("device storage STORAGE_PATH=\"{}\"", Self::STORAGE_PATH); |
37 | 38 | ||
38 | Ok(Self::STORAGE_PATH.to_string()) | 39 | Ok(Self::STORAGE_PATH.to_string()) |
39 | } | 40 | } |
@@ -49,6 +50,24 @@ impl Device { | |||
49 | Ok(dev) | 50 | Ok(dev) |
50 | } | 51 | } |
51 | 52 | ||
53 | pub fn read_all() -> Result<Vec<Self>, Error> { | ||
54 | trace!("attempt to read all files"); | ||
55 | let st_path = read_dir(Self::STORAGE_PATH)?; | ||
56 | |||
57 | let mut devices = vec![]; | ||
58 | for file_path in st_path { | ||
59 | let file_path = file_path?; | ||
60 | if file_path.path().extension() != Some(OsStr::new("json")) { continue; }; | ||
61 | let mut file = File::open(file_path.path())?; | ||
62 | let mut buf = String::new(); | ||
63 | file.read_to_string(&mut buf)?; | ||
64 | trace!(?file, ?buf, "read successfully from file"); | ||
65 | devices.push(serde_json::from_str(&buf)?); | ||
66 | } | ||
67 | |||
68 | Ok(devices) | ||
69 | } | ||
70 | |||
52 | pub fn write(&self) -> Result<(), Error> { | 71 | pub fn write(&self) -> Result<(), Error> { |
53 | trace!(?self.id, ?self, "attempt to write to file"); | 72 | trace!(?self.id, ?self, "attempt to write to file"); |
54 | let mut file = File::create(format!("{}/{}.json", Self::STORAGE_PATH, self.id))?; | 73 | let mut file = File::create(format!("{}/{}.json", Self::STORAGE_PATH, self.id))?; |