feat: start to implement node_registry

This commit is contained in:
max_richter 2024-03-01 18:23:40 +01:00
parent 27b6514167
commit 738bc1cf7a
8 changed files with 1473 additions and 140 deletions

1472
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -5,7 +5,7 @@ members = [
"nodes/add", "nodes/add",
"nodes/subtract", "nodes/subtract",
"frontend/src-tauri", "frontend/src-tauri",
"packages/node_registry_rust", "nodes/multiply", "packages/node_registry_rust", "nodes/multiply", "packages/node_registry_server",
] ]
[profile.release] [profile.release]

View File

@ -5,6 +5,5 @@ pub extern "C" fn execute(a: u32, b: u32) -> u32 {
#[no_mangle] #[no_mangle]
pub extern "C" fn get_definition() -> String { pub extern "C" fn get_definition() -> String {
let bytes = include_str!("./node.json"); include_str!("./node.json").to_string()
bytes.to_string()
} }

View File

@ -6,3 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
wasmtime = "18.0.2"

View File

@ -1,3 +1,73 @@
pub fn add(left: usize, right: usize) -> usize { use std::fs;
left + right use wasmtime::*;
pub struct NodeRegistryCore {
storage_path: String,
}
impl NodeRegistryCore {
pub fn new(storage_path: &str) -> NodeRegistryCore {
NodeRegistryCore {
storage_path: storage_path.to_string(),
}
}
pub fn get_node_definition(
&self,
_author: &str,
_namespace: &str,
node_id: &str,
) -> Result<Vec<u8>, String> {
let bytes = self.get_node(_author, _namespace, node_id);
let engine = Engine::default();
}
// Function that takes a string and returns bytes
pub fn get_node(
&self,
_author: &str,
_namespace: &str,
node_id: &str,
) -> Result<Vec<u8>, String> {
let res = fs::read_dir(&self.storage_path).map_err(|_| "Could not read dir")?;
let file_names = res
.filter_map(|entry| match entry {
Ok(entry) => {
let file_type = entry.file_type().ok()?;
let file_path = entry.path();
// Skip directories
if file_type.is_dir() {
return None;
}
// Extract file name and extension
let file_name = entry.file_name().to_string_lossy().into_owned();
let extension = file_path.extension()?.to_string_lossy().into_owned();
if !file_name.starts_with(node_id) {
return None;
}
if extension != "wasm" {
return None;
}
Some(file_name)
}
Err(_) => None,
})
.collect::<Vec<String>>();
if let Some(file_name) = file_names.get(0) {
let bytes = fs::read(self.storage_path.clone() + "/" + file_name)
.map_err(|_| "Could not read file".to_string())?;
Ok(bytes)
} else {
Err("Not Found".into())
}
}
} }

View File

@ -0,0 +1,10 @@
[package]
name = "node_registry_server"
version = "0.1.0"
edition = "2021"
[dependencies]
async-std = { version = "1.6.0", features = ["attributes"] }
node_registry_rust = { path = "../node_registry_rust" }
serde = { version = "1.0.197", features = ["derive"] }
tide = "0.16.0"

View File

@ -0,0 +1,33 @@
use node_registry_rust::NodeRegistryCore;
use tide::Request;
#[async_std::main]
async fn main() -> tide::Result<()> {
let mut app = tide::new();
app.at("/").get(index);
app.at("/nodes/:author/:namespace/:node_id").get(get_node);
app.listen("0.0.0.0:8080").await?;
Ok(())
}
async fn get_node(req: Request<()>) -> tide::Result {
let author = req.param("author")?;
let namespace = req.param("namespace")?;
let node_id = req.param("node_id")?;
let node_registry = NodeRegistryCore::new("../../target/wasm32-unknown-unknown/release");
if node_id.ends_with(".json") {
Ok("asd".into())
} else {
let res = node_registry.get_node(author, namespace, node_id);
match res {
Ok(res) => Ok(format!("Hello {}", res.len()).into()),
Err(er) => Ok(format!("Err: {}", er).into()),
}
}
}
async fn index(_req: Request<()>) -> tide::Result {
Ok(format!("Hello {}", "World").into())
}

View File

@ -0,0 +1,18 @@
pub struct NodeRegistryCore {
storage_path: String,
}
impl NodeRegistryCore {
pub fn new(storage_path: &str) -> NodeRegistryCore {
NodeRegistryCore {
storage_path: storage_path.to_string()
}
}
// Function that takes a string and returns bytes
pub fn string_to_bytes(&self, input: &str) -> Vec<u8> {
// Combine the initialization argument and input string into bytes
let result: Vec<u8> = format!("{} {}", self.storage_path, input).into_bytes();
result
}
}