ResourceHub API. Not for production. Code slitted into fucntional Modules
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user