ResourceHub API. Not for production. Code slitted into fucntional Modules

This commit is contained in:
SocioCyber
2026-03-17 00:40:16 -07:00
parent 8a4b324832
commit 21d0a0dd07
18 changed files with 175 additions and 270 deletions

View File

@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="BetterCommentsSettings">
<option name="initialized" value="true" />
<option name="tags">
<list>
<CustomTag>
<option name="color" value="#FF2D00" />
<option name="type" value="!" />
</CustomTag>
<CustomTag>
<option name="color" value="#3498DB" />
<option name="type" value="?" />
</CustomTag>
<CustomTag>
<option name="color" value="#474747" />
<option name="hasStrikethrough" value="true" />
<option name="type" value="//" />
</CustomTag>
<CustomTag>
<option name="color" value="#FF8C00" />
<option name="type" value="todo" />
</CustomTag>
<CustomTag>
<option name="color" value="#98C379" />
<option name="type" value="*" />
</CustomTag>
</list>
</option>
</component>
</project>

View File

@@ -2,5 +2,6 @@
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

120
ResourceHub/Cargo.lock generated
View File

@@ -373,9 +373,9 @@ dependencies = [
[[package]]
name = "getrandom"
version = "0.4.1"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "139ef39800118c7683f2fd3c98c1b23c09ae076556b435f8e9064ae108aaeeec"
checksum = "0de51e6874e94e7bf76d726fc5d13ba782deca734ff60d5bb2fb2607c7406555"
dependencies = [
"cfg-if",
"libc",
@@ -661,9 +661,9 @@ checksum = "09edd9e8b54e49e587e4f6295a7d29c3ea94d469cb40ab8ca70b288248a81db2"
[[package]]
name = "libc"
version = "0.2.182"
version = "0.2.183"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6800badb6cb2082ffd7b6a67e6125bb39f18782f793520caee8cb8846be06112"
checksum = "b5b646652bf6661599e1da8901b3b9522896f01e736bad5f723fe7a3a27f899d"
[[package]]
name = "linux-raw-sys"
@@ -933,18 +933,18 @@ dependencies = [
[[package]]
name = "quote"
version = "1.0.44"
version = "1.0.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21b2ebcf727b7760c461f091f9f0f539b77b8e87f2fd88131e7f1b433b3cece4"
checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924"
dependencies = [
"proc-macro2",
]
[[package]]
name = "r-efi"
version = "5.3.0"
version = "6.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf"
[[package]]
name = "rand"
@@ -978,9 +978,9 @@ dependencies = [
[[package]]
name = "redis"
version = "1.0.4"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dbe7f6e08ce1c6a9b21684e643926f6fc3b683bc006cb89afd72a5e0eb16e3a2"
checksum = "b36964393906eb775b89b25b05b7b95685b8dd14062f1663a31ff93e75c452e5"
dependencies = [
"arcstr",
"bytes",
@@ -993,7 +993,7 @@ dependencies = [
"pin-project-lite",
"ryu",
"sha1_smol",
"socket2 0.6.2",
"socket2 0.6.3",
"tokio",
"tokio-util",
"url",
@@ -1278,12 +1278,12 @@ dependencies = [
[[package]]
name = "socket2"
version = "0.6.2"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86f4aa3ad99f2088c990dfa82d367e19cb29268ed67c574d10d0a4bfe71f07e0"
checksum = "3a766e1110788c36f4fa1c2b71b387a7815aa65f88ce0229841826633d93723e"
dependencies = [
"libc",
"windows-sys 0.60.2",
"windows-sys 0.61.2",
]
[[package]]
@@ -1345,7 +1345,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "82a72c767771b47409d2345987fda8628641887d5466101319899796367354a0"
dependencies = [
"fastrand",
"getrandom 0.4.1",
"getrandom 0.4.2",
"once_cell",
"rustix",
"windows-sys 0.61.2",
@@ -1409,9 +1409,9 @@ dependencies = [
[[package]]
name = "tokio"
version = "1.49.0"
version = "1.50.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72a2903cd7736441aac9df9d7688bd0ce48edccaadf181c3b90be801e81d3d86"
checksum = "27ad5e34374e03cfffefc301becb44e9dc3c17584f414349ebe29ed26661822d"
dependencies = [
"bytes",
"libc",
@@ -1419,16 +1419,16 @@ dependencies = [
"parking_lot",
"pin-project-lite",
"signal-hook-registry",
"socket2 0.6.2",
"socket2 0.6.3",
"tokio-macros",
"windows-sys 0.61.2",
]
[[package]]
name = "tokio-macros"
version = "2.6.0"
version = "2.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af407857209536a95c8e56f8231ef2c2e2aff839b22e07a1ffcbc617e9db9fa5"
checksum = "5c55a2eff8b69ce66c84f85e1da1c233edc36ceb85a2058d11b0d6a3c7e7569c"
dependencies = [
"proc-macro2",
"quote",
@@ -1725,15 +1725,6 @@ dependencies = [
"windows-targets 0.52.6",
]
[[package]]
name = "windows-sys"
version = "0.60.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2f500e4d28234f72040990ec9d39e3a6b950f9f22d3dba18416c35882612bcb"
dependencies = [
"windows-targets 0.53.5",
]
[[package]]
name = "windows-sys"
version = "0.61.2"
@@ -1767,30 +1758,13 @@ dependencies = [
"windows_aarch64_gnullvm 0.52.6",
"windows_aarch64_msvc 0.52.6",
"windows_i686_gnu 0.52.6",
"windows_i686_gnullvm 0.52.6",
"windows_i686_gnullvm",
"windows_i686_msvc 0.52.6",
"windows_x86_64_gnu 0.52.6",
"windows_x86_64_gnullvm 0.52.6",
"windows_x86_64_msvc 0.52.6",
]
[[package]]
name = "windows-targets"
version = "0.53.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4945f9f551b88e0d65f3db0bc25c33b8acea4d9e41163edf90dcd0b19f9069f3"
dependencies = [
"windows-link",
"windows_aarch64_gnullvm 0.53.1",
"windows_aarch64_msvc 0.53.1",
"windows_i686_gnu 0.53.1",
"windows_i686_gnullvm 0.53.1",
"windows_i686_msvc 0.53.1",
"windows_x86_64_gnu 0.53.1",
"windows_x86_64_gnullvm 0.53.1",
"windows_x86_64_msvc 0.53.1",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.48.5"
@@ -1803,12 +1777,6 @@ version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.53.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9d8416fa8b42f5c947f8482c43e7d89e73a173cead56d044f6a56104a6d1b53"
[[package]]
name = "windows_aarch64_msvc"
version = "0.48.5"
@@ -1821,12 +1789,6 @@ version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]]
name = "windows_aarch64_msvc"
version = "0.53.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9d782e804c2f632e395708e99a94275910eb9100b2114651e04744e9b125006"
[[package]]
name = "windows_i686_gnu"
version = "0.48.5"
@@ -1839,24 +1801,12 @@ version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
[[package]]
name = "windows_i686_gnu"
version = "0.53.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "960e6da069d81e09becb0ca57a65220ddff016ff2d6af6a223cf372a506593a3"
[[package]]
name = "windows_i686_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]]
name = "windows_i686_gnullvm"
version = "0.53.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa7359d10048f68ab8b09fa71c3daccfb0e9b559aed648a8f95469c27057180c"
[[package]]
name = "windows_i686_msvc"
version = "0.48.5"
@@ -1869,12 +1819,6 @@ version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
[[package]]
name = "windows_i686_msvc"
version = "0.53.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e7ac75179f18232fe9c285163565a57ef8d3c89254a30685b57d83a38d326c2"
[[package]]
name = "windows_x86_64_gnu"
version = "0.48.5"
@@ -1887,12 +1831,6 @@ version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
[[package]]
name = "windows_x86_64_gnu"
version = "0.53.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c3842cdd74a865a8066ab39c8a7a473c0778a3f29370b5fd6b4b9aa7df4a499"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.48.5"
@@ -1905,12 +1843,6 @@ version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.53.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ffa179e2d07eee8ad8f57493436566c7cc30ac536a3379fdf008f47f6bb7ae1"
[[package]]
name = "windows_x86_64_msvc"
version = "0.48.5"
@@ -1923,17 +1855,11 @@ version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]]
name = "windows_x86_64_msvc"
version = "0.53.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650"
[[package]]
name = "winnow"
version = "0.7.14"
version = "0.7.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a5364e9d77fcdeeaa6062ced926ee3381faa2ee02d3eb83a5c27a8825540829"
checksum = "df79d97927682d2fd8adb29682d1140b343be4ac0f08fd68b7765d9c059d3945"
dependencies = [
"memchr",
]

View File

@@ -1,6 +1,6 @@
services:
cache:
image: redis:7.4-alpine
image: redis:8.4.2-alpine
restart: always
ports:
- "${REDIS_PORT}:6379" # port_on_public:port_in_docker_container

View File

@@ -0,0 +1 @@
pub mod sensors;

View File

@@ -1,18 +1,12 @@
// use database
use rocket::serde::{Deserialize, Serialize, json::Json};
use rocket::serde::{json::Json};
use rocket::response::status;
use rocket::{get, post};
#[derive(Deserialize, Serialize)]
#[serde(crate = "rocket::serde")]
pub struct SensorData {
id: Option<u32>,
name: String,
value: f32,
unit: String,
}
use crate::models::sensors::SensorData;
use crate::models::sensors::get_sensor_data;
// use repository::Redis::
// get data json obj
// database.getSensorData()
// send 200 http response with json obj
@@ -26,16 +20,10 @@ pub struct SensorData {
pub async fn create(json: Json<SensorData>) -> status::Accepted<Json<SensorData>> {
let received_data = json.into_inner();
let data = SensorData {
id: None,
name: String::from(received_data.name),
value: received_data.value,
unit: received_data.unit
};
let data = get_sensor_data();
status::Accepted(Json(data))
// saved_data = database.create(data);
// status::Accepted(Json(saved_data))
}

View File

@@ -0,0 +1 @@
pub mod data;

View File

@@ -1,22 +1,31 @@
mod util;
mod endpoints;
mod models;
use rocket::{routes};
use crate::util::constants;
use util::constants;
use util::debug;
mod util {
pub mod constants;
pub mod colored_text;
}
pub fn connect() -> redis::RedisResult<()> {
let client = redis::Client::open("redis://127.0.0.1:7878/")?;
let mut connector = client.get_connection()?;
// let mut connector = client.get_connection();
// connector.set("Name", "Redis");
// println!("DB name:: {}", connector.get("Name"));
mod endpoints {
pub mod sensors {
pub mod data;
match client.get_connection() {
Ok(mut connector) => {
util::colored_text::print_colored_text_info("[DB] Connected ");
Ok(())
},
Err(e) => {
util::colored_text::print_colored_text_err("[DB] Error, check redis database is running? (./Database/run.sh) Fix REDIS_PORT if not correctly setted;\n[Wiki] Instructions: https://git.sociocyber.site:5000/SocioCybereeng/Hydroponic_systems/src/branch/master/ResourceHub/Readme.md ", e.to_string());
Err(e)
}
}
}
fn print_debug_info() {
util::colored_text::print_colored_text_info(&format!("- SERVER INFO:"));
util::colored_text::print_colored_text_info(&format!(" - API VERSION: {} ", constants::API_VERSION));
util::colored_text::print_colored_text_info(&format!(" - DB NAME: {} ", constants::DB_NAME));
}
#[rocket::main]
async fn main() -> Result<(), rocket::Error> {
@@ -25,48 +34,38 @@ fn print_debug_info() {
util::colored_text::print_colored_text_info("- [Welcome to ResourceHUB API] ");
if util::constants::IS_DEBUG == true {
print_debug_info();
debug::print_debug_info();
}
// let mut status = ServerStatus::Online;
// // Use the colored printing function based on the server status
// match status {
// ServerStatus::Online => {
// }
// ServerStatus::Offline => {
// util::colored_text::print_colored_text_notify("[SERVER_STATUS] The server is stopped.");
// }
// ServerStatus::Starting => {
// util::colored_text::print_colored_text_notify("[SERVER_STATUS] The server is starting.");
// }
// ServerStatus::Stopping => {
// util::colored_text::print_colored_text_notify("[SERVER_STATUS] The server is stopping.");
// }
// ServerStatus::Error(ref err) => {
// util::colored_text::print_colored_text_err(&format!("[SERVER_STATUS] An error occurred: {}", err));
// }
// }
// let DBconnector = ;
// Attempt to connect to DB and check the result
let conection = connect();
match conection {
// match DBconnector::connect() {
// Start server
let _rocket = rocket::build()
.mount(format!("/api/v{}/", constants::API_VERSION), routes![
endpoints::sensors::data::create,
endpoints::sensors::data::get,
endpoints::sensors::data::get_by_id,
])
.launch()
.await?;
Ok(())
Ok(conection) => {
// If the connection is successful, store it or use it as needed
let mut _db = conection;
// Start server
let _rocket = rocket::build()
.mount(format!("/api/v{}/", constants::API_VERSION), routes![
endpoints::sensors::data::create,
endpoints::sensors::data::get,
endpoints::sensors::data::get_by_id,
])
.launch()
.await?;
Ok(())
},
Err(err) => {
// Handle connection error and stop execution
std::process::exit(1);
}
}
}
// #[macro_use] extern crate rocket;
// #[launch]
// fn rocket() -> _ {
// Create an instance of ServerStatus

View File

@@ -0,0 +1,6 @@
pub mod sensors;
// pub mod something_data;
// pub mod something_data;
// pub mod something_data;
// pub mod something_data;
// pub mod something_data;

View File

@@ -0,0 +1,22 @@
use rocket::serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize)]
#[serde(crate = "rocket::serde")]
#[derive(Debug)]
pub struct SensorData {
pub id: Option<u32>,
pub name: String,
pub value: f32,
pub unit: String,
}
// #[derive(Debug)]
pub fn get_sensor_data() -> SensorData {
SensorData {
id: Some(1),
name: String::from("Sensor name"),
value: 4.5,
unit: String::from("C")
}
}

View File

@@ -0,0 +1,6 @@
// use redis::AsyncCommands;
// ! Load REDIS_PORT from env
// pub mod DBConnector {
// }

View File

@@ -1,94 +0,0 @@
// DBconnector
mod DBconnector {
use redis::AsyncCommands;
async fn run() {
let mut r = match redis::Client::open("redis://127.0.0.1:7878") {
Ok(client) => {
match client.get_multiplexed_async_connection().await {
Ok(conn) => conn,
Err(e) => {
println!("Failed to connect to Redis: {e}");
return;
}
}
},
Err(e) => {
println!("Failed to create Redis client: {e}");
return;
}
};
if let Ok(res) = r.set("foo", "bar").await {
let res: String = res;
println!("{res}"); // >>> OK
} else {
println!("Error setting foo");
}
match r.get("foo").await {
Ok(res) => {
let res: String = res;
println!("{res}"); // >>> bar
},
Err(e) => {
println!("Error getting foo: {e}");
return;
}
};
let hash_fields = [
("model", "Deimos"),
("brand", "Ergonom"),
("type", "Enduro bikes"),
("price", "4972"),
];
if let Ok(res) = r.hset_multiple("bike:1", &hash_fields).await {
let res: String = res;
println!("{res}"); // >>> OK
} else {
println!("Error setting bike:1");
}
match r.hget("bike:1", "model").await {
Ok(res) => {
let res: String = res;
println!("{res}"); // >>> Deimos
},
Err(e) => {
println!("Error getting bike:1 model: {e}");
return;
}
}
match r.hget("bike:1", "price").await {
Ok(res) => {
let res: String = res;
println!("{res}"); // >>> 4972
},
Err(e) => {
println!("Error getting bike:1 price: {e}");
return;
}
}
match r.hgetall("bike:1").await {
Ok(res) => {
let res: Vec<(String, String)> = res;
for (key, value) in res {
println!("{key}: {value}");
}
// >>> model: Deimos
// >>> brand: Ergonom
// >>> type: Enduro bikes
// >>> price: 4972
},
Err(e) => {
println!("Error getting bike:1: {e}");
return;
}
}
}
}

View File

@@ -0,0 +1 @@
sqlite

View File

@@ -0,0 +1,2 @@
redis
postgresql

View File

@@ -13,12 +13,16 @@ pub fn print_colored_text_info(string: &str) {
// );
// }
// pub fn print_colored_text_err(string: &str) {
// println!("{}", string
// .color(Color::RED)
// .bg(Color::BLACK)
// );
// }
pub fn print_colored_text_err(string: &str, error_message: String) {
println!("{}", string
.color(Color::RED)
.bg(Color::BLACK)
);
println!("{}", error_message
.color(Color::RED)
.bg(Color::BLACK)
);
}
pub fn print_colored_text_brand(string: &str) {
println!("{}", string

View File

@@ -0,0 +1,8 @@
use crate::constants;
use crate::util::colored_text::*;
pub fn print_debug_info() {
print_colored_text_info(&format!("- SERVER INFO:"));
print_colored_text_info(&format!(" - API VERSION: {} ", constants::API_VERSION));
print_colored_text_info(&format!(" - DB NAME: {} ", constants::DB_NAME));
}

View File

@@ -0,0 +1,3 @@
pub mod constants;
pub mod colored_text;
pub mod debug;