Skip to content

Commit e9b5e5a

Browse files
committed
[FEATURE] Major improvements:
- Crate configuration added. - Server status route for controller implemented. - Project reestructured.
1 parent 15dfdc6 commit e9b5e5a

File tree

13 files changed

+166
-16
lines changed

13 files changed

+166
-16
lines changed

β€ŽCargo.lockβ€Ž

Lines changed: 40 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€ŽCargo.tomlβ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ edition = "2021"
77

88
[dependencies]
99
tokio = { version = "1", features = ["full"] }
10+
lazy_static = "1.4.0"
1011
axum = {version = "0.7.3", features = ["tokio"]}
1112
tower-http = { version = "0.5.2", features = ["cors"] }
1213
async-trait = "0.1.80"
1314
serde = { version = "1.0", features = ["derive"] }
1415
serde_json = "1.0"
1516
crossterm = "0.27.0"
1617
uuid = "1.8.0"
18+
cargo_metadata = "0.18.1"
1719
rust_db_manager_core = { git = "https://github.com/Rafael24595/rust-db-manager.git", branch = "dev" }
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
use std::sync::Mutex;
2+
3+
use cargo_metadata::{CargoOpt, MetadataCommand};
4+
use lazy_static::lazy_static;
5+
use rust_db_manager_core::commons::configuration::configuration::Configuration;
6+
7+
use crate::infrastructure::dto::dto_server_status::DTOServerStatus;
8+
9+
lazy_static! {
10+
static ref INSTANCE: Mutex<Option<WebConfiguration>> = Mutex::new(None);
11+
}
12+
13+
#[derive(Clone)]
14+
pub struct WebConfiguration {
15+
web_app_name: String,
16+
web_app_version: String,
17+
}
18+
19+
impl WebConfiguration {
20+
21+
pub fn initialize() -> WebConfiguration {
22+
let _ = Configuration::initialize();
23+
24+
let mut instance = INSTANCE.lock().expect("Could not lock mutex");
25+
if instance.is_some() {
26+
//TODO: Log.
27+
panic!("Configuration is already initialized.");
28+
}
29+
30+
let metadata = MetadataCommand::new()
31+
.manifest_path("./Cargo.toml")
32+
.features(CargoOpt::AllFeatures)
33+
.exec()
34+
.unwrap();
35+
36+
let root: &cargo_metadata::Package = metadata.packages.iter()
37+
.find(|i| i.name == "rust_db_manager_api").unwrap();
38+
39+
let web_app_name =root.name.clone();
40+
let web_app_version = root.version.clone().to_string();
41+
42+
let config = WebConfiguration {
43+
web_app_name, web_app_version
44+
};
45+
46+
*instance = Some(config);
47+
48+
return instance.as_ref().unwrap().clone();
49+
}
50+
51+
fn instance() -> WebConfiguration {
52+
let instance = INSTANCE.lock().expect("Could not lock mutex");
53+
if instance.is_none() {
54+
//TODO: Log.
55+
panic!("Configuration is not initialized.");
56+
}
57+
58+
return instance.as_ref().unwrap().clone();
59+
}
60+
61+
pub fn name() -> String {
62+
WebConfiguration::instance().web_app_name
63+
}
64+
65+
pub fn version() -> String {
66+
WebConfiguration::instance().web_app_version
67+
}
68+
69+
pub fn as_dto() -> DTOServerStatus {
70+
let web = WebConfiguration::instance();
71+
DTOServerStatus {
72+
rustc_version: Configuration::rustc_version(),
73+
cargo_version: Configuration::cargo_version(),
74+
core_name: Configuration::name(),
75+
core_version: Configuration::version(),
76+
web_name: web.web_app_name,
77+
web_version: web.web_app_version,
78+
session_id: Configuration::session_id(),
79+
timestamp: Configuration::timestamp(),
80+
services: Configuration::find_services().len()
81+
}
82+
}
83+
84+
}

β€Žsrc/infrastructure/controller.rsβ€Ž

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use axum::{body::Body, extract::{Path, Query}, http::{Response, StatusCode}, res
22

33
use rust_db_manager_core::commons::configuration::configuration::Configuration;
44

5-
use crate::{commons::exception::api_exception::ApiException, domain::builder_db_service::BuilderDBService};
5+
use crate::{commons::{configuration::web_configuration::WebConfiguration, exception::api_exception::ApiException}, domain::builder_db_service::BuilderDBService};
66

7-
use super::{dto::{db_service::{dto_db_service::DTODBService, dto_db_service_lite::DTODBServiceLite, dto_paginated_collection::DTOPaginatedCollection}, dto_query_pagination::DTOQueryPagination}, pagination::Pagination};
7+
use super::{dto::{db_service::{dto_db_service::DTODBService, dto_db_service_lite::DTODBServiceLite}, dto_server_status::DTOServerStatus, pagination::{dto_paginated_collection::DTOPaginatedCollection, dto_query_pagination::DTOQueryPagination}}, pagination::Pagination};
88

99
pub struct Controller{
1010
}
@@ -13,9 +13,15 @@ impl Controller {
1313

1414
pub fn route(router: Router) -> Router {
1515
router
16+
.route("/status", get(Controller::status))
1617
.route("/services", get(Controller::services))
18+
.route("/:service/status", get(Controller::service_status))
1719
.route("/:service", post(Controller::insert_service))
18-
.route("/:service/status", get(Controller::status))
20+
}
21+
22+
async fn status() -> (StatusCode, Json<DTOServerStatus>) {
23+
let result = WebConfiguration::as_dto();
24+
(StatusCode::ACCEPTED, Json(result))
1925
}
2026

2127
async fn services(Query(params): Query<DTOQueryPagination>) -> (StatusCode, Json<DTOPaginatedCollection<DTODBServiceLite>>) {
@@ -42,7 +48,7 @@ impl Controller {
4248
Ok((StatusCode::ACCEPTED, service.name()))
4349
}
4450

45-
async fn status(Path(service): Path<String>) -> Result<(StatusCode, String), impl IntoResponse> {
51+
async fn service_status(Path(service): Path<String>) -> Result<(StatusCode, String), impl IntoResponse> {
4652
let db_service = Configuration::find_service(service);
4753
if db_service.is_none() {
4854
return Err(Controller::not_found());

β€Žsrc/infrastructure/db_assets.rsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rust_db_manager_core::infrastructure::repository::e_db_repository::EDBRepository;
22

3-
use super::dto::dto_db_resources::DTODBResources;
3+
use super::dto::db_service::dto_db_resources::DTODBResources;
44

55
pub trait WebEDBRepository {
66

β€Žsrc/infrastructure/dto/db_service/dto_db_service_web_category.rsβ€Ž

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use rust_db_manager_core::infrastructure::{db_service_lite::DBServiceLite, repository::e_db_repository::EDBRepository};
1+
use rust_db_manager_core::infrastructure::repository::e_db_repository::EDBRepository;
22
use serde::{Deserialize, Serialize};
33

4-
use crate::infrastructure::{db_assets::WebEDBRepository, dto::dto_db_resources::DTODBResources};
4+
use super::dto_db_resources::DTODBResources;
5+
use crate::infrastructure::db_assets::WebEDBRepository;
56

67
#[derive(Clone, Serialize, Deserialize)]
78
pub struct DTODBServiceWebCategory {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use serde::Serialize;
2+
3+
#[derive(Clone, Serialize)]
4+
pub struct DTOServerStatus {
5+
pub rustc_version: String,
6+
pub cargo_version: String,
7+
pub core_name: String,
8+
pub core_version: String,
9+
pub web_name: String,
10+
pub web_version: String,
11+
pub session_id: String,
12+
pub timestamp: u128,
13+
pub services: usize
14+
}

0 commit comments

Comments
Β (0)