Skip to content

Commit

Permalink
Add support to uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
yamelsenih committed Jun 20, 2024
1 parent 8879485 commit b38d04f
Show file tree
Hide file tree
Showing 9 changed files with 645 additions and 1,432 deletions.
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,13 @@ cargo run --bin server
A output generated by the Arduino UNO emulator can be like it:

```Shell
INFO [server] Server Address: "0.0.0.0:7878"
INFO [server] └──!NULL!
└──api
├──[OPTIONS] -> server::options_response
├──[GET] -> server::get_system_info
├──security/get-allowed-menu
│ ├──[OPTIONS] -> server::options_response
│ └──[GET] -> server::get_allowed_menu
└──dictionary
├──browsers
│ ├──[OPTIONS] -> server::options_response
Expand All @@ -132,13 +135,18 @@ INFO [server] Server Address: "0.0.0.0:7878"
├──[OPTIONS] -> server::options_response
└──[GET] -> server::get_windows

INFO [server] Kafka Consumer is enabled
INFO [server] Kafka queue: "0.0.0.0:29092"
INFO [server] Topics to Subscribed: ["menu", "browser", "form", "process", "window"]
2024-06-20T19:28:59.081Z INFO [server] Kafka Consumer is enabled
2024-06-20T19:28:59.081Z INFO [server] Kafka queue: "localhost:29092"
2024-06-20T19:28:59.081Z INFO [server] Topics to Subscribed: ["menu", "browser", "form", "process", "window", "menu_item", "menu_tree", "role"]
```

# General Info

## Postman Documentation

- [Online Docs](https://documenter.getpostman.com/view/18440575/2sA3XV8ewq)
- [Local Docs](docs/Open_Search.json)

## Testing OpenSearch

For test it just run a CURL like this:
Expand Down
1,718 changes: 567 additions & 1,151 deletions docs/Open_Search.json

Large diffs are not rendered by default.

66 changes: 25 additions & 41 deletions src/bin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,9 @@ struct ErrorResponse {
async fn get_forms<'a>(_req: &mut Request, _res: &mut Response) {
let _id: Option<String> = _req.param::<String>("id");
let _language: Option<&String> = _req.queries().get("language");
let _client_id: Option<&String> = _req.queries().get("client_id");
let _role_id: Option<&String> = _req.queries().get("role_id");
let _user_id: Option<&String> = _req.queries().get("user_id");

let _search_value = _req.queries().get("search_value");
if _id.is_some() {
match form_from_id(_id, _language, _client_id, _role_id, _user_id).await {
match form_from_id(_id, _language).await {
Ok(form) => _res.render(Json(form)),
Err(error) => {
let error_response = ErrorResponse {
Expand All @@ -213,7 +210,7 @@ async fn get_forms<'a>(_req: &mut Request, _res: &mut Response) {
} else {
let _search_value: Option<&String> = _req.queries().get("search_value");

match forms(_language, _client_id, _role_id, _user_id, _search_value).await {
match forms(_language, _search_value).await {
Ok(forms_list) => {
_res.render(Json(forms_list));
},
Expand All @@ -236,8 +233,7 @@ async fn get_allowed_menu<'a>(_req: &mut Request, _res: &mut Response) {
let _language = _req.queries().get("language");
let _client_id = _req.queries().get("client_id");
let _role_id = _req.queries().get("role_id");
let _user_id = _req.queries().get("user_id");
match allowed_menu(_language, _client_id, _role_id, _user_id).await {
match allowed_menu(_language, _client_id, _role_id).await {
Ok(menu) => _res.render(Json(menu)),
Err(error) => {
let error_response = ErrorResponse {
Expand All @@ -256,13 +252,9 @@ async fn get_allowed_menu<'a>(_req: &mut Request, _res: &mut Response) {
async fn get_processes<'a>(_req: &mut Request, _res: &mut Response) {
let _id = _req.param::<String>("id");
let _language = _req.queries().get("language");
let _client_id = _req.queries().get("client_id");
let _role_id = _req.queries().get("role_id");
let _user_id = _req.queries().get("user_id");
let _search_value = _req.queries().get("search_value");

if _id.is_some() {
match process_from_id(_id, _language, _client_id, _role_id, _user_id).await {
match process_from_id(_id, _language).await {
Ok(process) => _res.render(Json(process)),
Err(error) => {
let error_response = ErrorResponse {
Expand All @@ -276,7 +268,7 @@ async fn get_processes<'a>(_req: &mut Request, _res: &mut Response) {
}
}
} else {
match processes(_language, _client_id, _role_id, _user_id, _search_value).await {
match processes(_language, _search_value).await {
Ok(processes_list) => {
_res.render(Json(processes_list));
},
Expand All @@ -298,13 +290,9 @@ async fn get_processes<'a>(_req: &mut Request, _res: &mut Response) {
async fn get_browsers<'a>(_req: &mut Request, _res: &mut Response) {
let _id = _req.param::<String>("id");
let _language = _req.queries().get("language");
let _client_id = _req.queries().get("client_id");
let _role_id = _req.queries().get("role_id");
let _user_id = _req.queries().get("user_id");
let _search_value = _req.queries().get("search_value");

if _id.is_some() {
match browser_from_id(_id, _language, _client_id, _role_id, _user_id).await {
match browser_from_id(_id, _language).await {
Ok(browser) => _res.render(Json(browser)),
Err(error) => {
let error_response = ErrorResponse {
Expand All @@ -318,7 +306,7 @@ async fn get_browsers<'a>(_req: &mut Request, _res: &mut Response) {
}
}
} else {
match browsers(_language, _client_id, _role_id, _user_id, _search_value).await {
match browsers(_language, _search_value).await {
Ok(browsers_list) => {
_res.render(Json(browsers_list));
},
Expand All @@ -338,15 +326,11 @@ async fn get_browsers<'a>(_req: &mut Request, _res: &mut Response) {

#[handler]
async fn get_windows<'a>(_req: &mut Request, _res: &mut Response) {
let _id = _req.param::<String>("id");
let _id: Option<String> = _req.param::<String>("id");
let _language = _req.queries().get("language");
let _client_id = _req.queries().get("client_id");
let _role_id = _req.queries().get("role_id");
let _user_id = _req.queries().get("user_id");
let _search_value = _req.queries().get("search_value");

if _id.is_some() {
match window_from_id(_id, _language, _client_id, _role_id, _user_id).await {
match window_from_id(_id, _language).await {
Ok(window) => _res.render(Json(window)),
Err(error) => {
let error_response = ErrorResponse {
Expand All @@ -360,7 +344,7 @@ async fn get_windows<'a>(_req: &mut Request, _res: &mut Response) {
}
}
} else {
match windows(_language, _client_id, _role_id, _user_id, _search_value).await {
match windows(_language, _search_value).await {
Ok(windows_list) => {
_res.render(Json(windows_list));
},
Expand Down Expand Up @@ -435,7 +419,7 @@ async fn consume_queue() {
let _document = match serde_json::from_str(payload) {
Ok(value) => value,
Err(error) => {
log::warn!("{}", error);
log::warn!("Topic: {:?}, {}", topic, error);
MenuItemDocument {
document: None
}
Expand All @@ -445,14 +429,14 @@ async fn consume_queue() {
let _menu_document: &dyn IndexDocument = &(_document.document.unwrap());
match process_index(event_type, _menu_document).await {
Ok(_) => consumer.commit_message(&message, CommitMode::Async).unwrap(),
Err(error) => log::warn!("{}", error)
Err(error) => log::warn!("Document: {:?} {}", _menu_document.index_name(), error)
}
}
} else if topic == "menu_tree" {
let _document = match serde_json::from_str(payload) {
Ok(value) => value,
Err(error) => {
log::warn!("{}", error);
log::warn!("Topic: {:?}, {}", topic, error);
MenuTreeDocument {
document: None
}
Expand All @@ -462,14 +446,14 @@ async fn consume_queue() {
let _menu_document: &dyn IndexDocument = &(_document.document.unwrap());
match process_index(event_type, _menu_document).await {
Ok(_) => consumer.commit_message(&message, CommitMode::Async).unwrap(),
Err(error) => log::warn!("{}", error)
Err(error) => log::warn!("Document: {:?} {}", _menu_document.index_name(), error)
}
}
} else if topic == "role" {
let _document = match serde_json::from_str(payload) {
Ok(value) => value,
Err(error) => {
log::warn!("{}", error);
log::warn!("Topic: {:?}, {}", topic, error);
RoleDocument {
document: None
}
Expand All @@ -479,14 +463,14 @@ async fn consume_queue() {
let _menu_document: &dyn IndexDocument = &(_document.document.unwrap());
match process_index(event_type, _menu_document).await {
Ok(_) => consumer.commit_message(&message, CommitMode::Async).unwrap(),
Err(error) => log::warn!("{}", error)
Err(error) => log::warn!("Document: {:?} {}", _menu_document.index_name(), error)
}
}
} else if topic == "process" {
let _document = match serde_json::from_str(payload) {
Ok(value) => value,
Err(error) => {
log::warn!("{}", error);
log::warn!("Topic: {:?}, {}", topic, error);
ProcessDocument {
document: None
}
Expand All @@ -496,14 +480,14 @@ async fn consume_queue() {
let _process_document: &dyn IndexDocument = &(_document.document.unwrap());
match process_index(event_type, _process_document).await {
Ok(_) => consumer.commit_message(&message, CommitMode::Async).unwrap(),
Err(error) => log::warn!("{}", error)
Err(error) => log::warn!("Document: {:?} {}", _process_document.index_name(), error)
}
}
} else if topic == "browser" {
let _document = match serde_json::from_str(payload) {
Ok(value) => value,
Err(error) => {
log::warn!("{}", error);
log::warn!("Topic: {:?}, {}", topic, error);
BrowserDocument {
document: None
}
Expand All @@ -513,14 +497,14 @@ async fn consume_queue() {
let _browser_document: &dyn IndexDocument = &(_document.document.unwrap());
match process_index(event_type, _browser_document).await {
Ok(_) => consumer.commit_message(&message, CommitMode::Async).unwrap(),
Err(error) => log::warn!("{}", error)
Err(error) => log::warn!("Document: {:?} {}", _browser_document.index_name(), error)
}
}
} else if topic == "window" {
let _document = match serde_json::from_str(payload) {
Ok(value) => value,
Err(error) => {
log::warn!("{}", error);
log::warn!("Topic: {:?}, {}", topic, error);
WindowDocument {
document: None
}
Expand All @@ -530,14 +514,14 @@ async fn consume_queue() {
let _window_document: &dyn IndexDocument = &(_document.document.unwrap());
match process_index(event_type, _window_document).await {
Ok(_) => consumer.commit_message(&message, CommitMode::Async).unwrap(),
Err(error) => log::warn!("{}", error)
Err(error) => log::warn!("Document: {:?} {}", _window_document.index_name(), error)
}
}
} else if topic == "form" {
let _document = match serde_json::from_str(payload) {
Ok(value) => value,
Err(error) => {
log::warn!("{}", error);
log::warn!("Topic: {:?}, {}", topic, error);
FormDocument {
document: None
}
Expand All @@ -547,7 +531,7 @@ async fn consume_queue() {
let _form_document: &dyn IndexDocument = &(_document.document.unwrap());
match process_index(event_type, _form_document).await {
Ok(_) => consumer.commit_message(&message, CommitMode::Async).unwrap(),
Err(error) => log::warn!("{}", error)
Err(error) => log::warn!("Document: {:?} {}", _form_document.index_name(), error)
}
}
}
Expand Down
61 changes: 5 additions & 56 deletions src/models/browser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ use salvo::prelude::*;
use serde_json::json;
use std::{io::ErrorKind, io::Error};

use crate::controller::opensearch::{IndexDocument, get_by_id, find, exists_index};

use super::{client_index, user_index, role_index};
use crate::{controller::opensearch::{find, get_by_id, IndexDocument}, models::get_index_name};

#[derive(Deserialize, Extractible, Debug, Clone)]
#[salvo(extract(default_source(from = "body")))]
Expand Down Expand Up @@ -276,13 +274,13 @@ pub struct DependendField {
pub parent_name: Option<String>
}

pub async fn browser_from_id(_id: Option<String>, _language: Option<&String>, _client_id: Option<&String>, _role_id: Option<&String>, _user_id: Option<&String>) -> Result<Browser, String> {
pub async fn browser_from_id(_id: Option<String>, _language: Option<&String>) -> Result<Browser, String> {
if _id.is_none() {
return Err(Error::new(ErrorKind::InvalidData.into(), "Browser Identifier is Mandatory").to_string());
}
let mut _document = Browser::from_id(_id);

let _index_name = match get_index_name(_language, _client_id, _role_id, _user_id).await {
let _index_name = match get_index_name("browser".to_string(),_language).await {
Ok(index_name) => index_name,
Err(error) => {
log::error!("Index name error: {:?}", error.to_string());
Expand Down Expand Up @@ -314,63 +312,14 @@ pub async fn browser_from_id(_id: Option<String>, _language: Option<&String>, _c
}
}

async fn get_index_name(_language: Option<&String>, _client_id: Option<&String>, _role_id: Option<&String>, _user_id: Option<&String>) -> Result<String, std::io::Error> {
// Validate
if _language.is_none() {
return Err(Error::new(ErrorKind::InvalidData.into(), "Language is Mandatory"));
}
if _client_id.is_none() {
return Err(Error::new(ErrorKind::InvalidData.into(), "Client is Mandatory"));
}
if _role_id.is_none() {
return Err(Error::new(ErrorKind::InvalidData.into(), "Role is Mandatory"));
}

let _index: String = "browser".to_string();

let _user_index = user_index(_index.to_owned(), _language, _client_id, _role_id, _user_id);
let _role_index = role_index(_index.to_owned(), _language, _client_id, _role_id);
let _client_index = client_index(_index.to_owned(), _language, _client_id);

// Find index
match exists_index(_user_index.to_owned()).await {
Ok(_) => {
log::info!("Find with user index `{:}`", _user_index);
Ok(_user_index)
},
Err(_) => {
log::warn!("No user index `{:}`", _user_index);
match exists_index(_role_index.to_owned()).await {
Ok(_) => {
log::info!("Find with role index `{:}`", _role_index);
Ok(_role_index)
},
Err(error) => {
log::warn!("No role index `{:}`", _role_index);
match exists_index(_client_index.to_owned()).await {
Ok(_) => {
log::info!("Find with client index `{:}`", _client_index);
Ok(_client_index)
},
Err(_) => {
log::error!("No client index `{:}`", _client_index);
return Err(Error::new(ErrorKind::InvalidData.into(), error))
}
}
}
}
}
}
}

pub async fn browsers(_language: Option<&String>, _client_id: Option<&String>, _role_id: Option<&String>, _user_id: Option<&String>, _search_value: Option<&String>) -> Result<BrowserListResponse, std::io::Error> {
pub async fn browsers(_language: Option<&String>, _search_value: Option<&String>) -> Result<BrowserListResponse, std::io::Error> {
let _search_value = match _search_value {
Some(value) => value.clone(),
None => "".to_owned()
};

// Find index
let _index_name = match get_index_name(_language, _client_id, _role_id, _user_id).await {
let _index_name = match get_index_name("browser".to_string(), _language).await {
Ok(index_name) => index_name,
Err(error) => {
log::error!("Index name error: {:?}", error.to_string());
Expand Down
Loading

0 comments on commit b38d04f

Please sign in to comment.