Configurable Struct

This commit is contained in:
Michael Mikovsky
2025-12-21 12:04:53 -07:00
parent c7d66c5560
commit 78fda07ab2
11 changed files with 160 additions and 98 deletions
+11 -10
View File
@@ -22,16 +22,16 @@ macro_rules! route_get {
}};
}
// macro_rules! route_post {
// ($router:expr, $path:expr, $func:expr) => {{
// {
// $router.route(
// $path,
// post($func).layer(middleware::from_fn(auth::authorize)),
// )
// }
// }};
// }
macro_rules! route_post {
($router:expr, $path:expr, $func:expr) => {{
{
$router.route(
$path,
post($func).layer(middleware::from_fn(auth::authorize)),
)
}
}};
}
pub async fn start_api(address: &str, server: Server) {
let listener = TcpListener::bind(address)
@@ -51,6 +51,7 @@ pub async fn start_api(address: &str, server: Server) {
router = route_get!(router, "/api/interface/", Server::get_tree2_root);
router = route_get!(router, "/api/interface/{*path}", Server::get_tree2);
router = route_post!(router, "/api/interface/{*path}", Server::post_tree2);
// router = route_get_log(router);
+3 -1
View File
@@ -1,6 +1,8 @@
use std::collections::HashMap;
use crate::config::ConfigStructField;
use unshell_lib::config::config_struct::ConfigStructField;
// use crate::config::ConfigStructField;
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub struct Blob {
-27
View File
@@ -33,33 +33,6 @@ struct BuildConfig {
features: HashMap<String, String>,
}
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub enum ConfigStructField {
Header(String),
Text(String),
String {
// Default value of string edit in struct
#[serde(default)]
default: String,
max_length: Option<usize>,
// Display string edit as password
#[serde(default)]
protected: Option<bool>,
},
Integer {
// Default value of integer in struct
#[serde(default)]
default: i32,
min: Option<i32>,
max: Option<i32>,
},
// Checkbox
// Dropdown
// Collapsing header
// Slider
// ...
}
#[derive(Clone, Debug)]
pub struct ComponentState {
metadata: ComponentMetadata,
+30 -23
View File
@@ -1,15 +1,15 @@
use std::{
collections::HashMap,
path::PathBuf,
sync::{Arc, Mutex},
};
use serde_json::Value;
use unshell_lib::{
ModuleError,
config::{InterfaceData, Tree, TreeMessage, config_struct::ConfigStructField},
ModuleError, Result,
config::{
Tree, TreeMessage,
config_struct::{Config, ConfigStructField},
},
};
use unshell_lib::{Result, config::InterfaceStruct};
use unshell_manager::Manager;
mod blobs;
@@ -18,25 +18,41 @@ mod tree2;
#[derive(Clone)]
pub struct Server {
pub component_configs: Vec<crate::config::ComponentState>,
// pub component_configs: Vec<crate::config::ComponentState>,
// pub interface: InterfaceWrapper,
pub manager: Arc<Mutex<Manager>>,
pub db: sled::Db,
// pub tree: Tree2,
test_thing: Arc<Mutex<Config>>,
}
impl Server {
pub fn new(config_paths: Vec<PathBuf>, database: String) -> Result<Self> {
let mut component_configs: Vec<crate::config::ComponentState> = Vec::new();
pub fn new(_config_paths: Vec<PathBuf>, database: String) -> Result<Self> {
// let mut component_configs: Vec<crate::config::ComponentState> = Vec::new();
for config in &config_paths {
component_configs.extend(crate::config::load_config(config)?);
}
// for config in &config_paths {
// component_configs.extend(crate::config::load_config(config)?);
// }
Ok(Self {
component_configs,
// component_configs,
manager: Manager::start(&crate::SERVER_CONFIG, Vec::new()),
db: sled::open(database).map_err(|e| ModuleError::DatabaseError(e.to_string()))?,
test_thing: Arc::new(Mutex::new(Config::new(vec![
ConfigStructField::Header("Test Heading".into()),
ConfigStructField::Text("Test Texttttttttttttttt".into()),
ConfigStructField::String {
default: "Test Texttttttttttttttt".into(),
max_length: None,
protected: None,
},
ConfigStructField::String {
default: "Test ".into(),
max_length: None,
protected: None,
},
]))),
// tree: Tree2::default(),
// interface: get_test_interface(),
})
@@ -52,18 +68,9 @@ impl Tree for Server {
vec!["connection_count".into()]
}
fn select_child(&self, child: &str, _message: TreeMessage) -> Result<TreeMessage> {
fn select_child(&mut self, child: &str, message: TreeMessage) -> Result<TreeMessage> {
match child {
"connection_count" => {
let interface = vec![ConfigStructField::Header(format!("Test Heading!"))];
let value = vec![Value::Null];
Ok(TreeMessage::InterfaceAndValue(
InterfaceStruct::ConfigStruct(interface),
InterfaceData::ConfigStruct(value),
))
}
"connection_count" => self.test_thing.lock().unwrap().get(message),
_ => Err("No such child".into()),
}
}
+16 -1
View File
@@ -21,7 +21,7 @@ impl Server {
}
pub async fn get_tree2(
State(server): State<Server>,
State(mut server): State<Server>,
Path(path): Path<String>,
Extension(_): Extension<CurrentUser>,
) -> Json<Value> {
@@ -33,4 +33,19 @@ impl Server {
Json(serde_json::to_value(result).unwrap())
}
pub async fn post_tree2(
State(mut server): State<Server>,
Path(path): Path<String>,
Extension(_): Extension<CurrentUser>,
Json(tree_message): Json<TreeMessage>,
) -> Json<Value> {
debug!("POST /api/interface/{}", path);
let result = server
.get(&path, tree_message)
.map_err(|e| ModuleError::CryptError(e.to_string()));
Json(serde_json::to_value(result).unwrap())
}
}