diff --git a/central_backend/src/server/devices_api/mod.rs b/central_backend/src/server/devices_api/mod.rs new file mode 100644 index 0000000..e7c75c0 --- /dev/null +++ b/central_backend/src/server/devices_api/mod.rs @@ -0,0 +1 @@ +pub mod utils_controller; diff --git a/central_backend/src/server/devices_api/utils_controller.rs b/central_backend/src/server/devices_api/utils_controller.rs new file mode 100644 index 0000000..4b73720 --- /dev/null +++ b/central_backend/src/server/devices_api/utils_controller.rs @@ -0,0 +1,15 @@ +use crate::server::custom_error::HttpResult; +use crate::utils::time_utils::time_millis; +use actix_web::HttpResponse; + +#[derive(serde::Serialize)] +pub struct CurrTime { + time_ms: u128, +} + +/// Get current time +pub async fn curr_time() -> HttpResult { + Ok(HttpResponse::Ok().json(CurrTime { + time_ms: time_millis(), + })) +} diff --git a/central_backend/src/server/mod.rs b/central_backend/src/server/mod.rs index 81790af..0fa62e3 100644 --- a/central_backend/src/server/mod.rs +++ b/central_backend/src/server/mod.rs @@ -4,6 +4,7 @@ use crate::energy::energy_actor::EnergyActorAddr; pub mod auth_middleware; pub mod custom_error; +pub mod devices_api; pub mod servers; pub mod unsecure_server; pub mod web_api; diff --git a/central_backend/src/server/servers.rs b/central_backend/src/server/servers.rs index 87cdaf8..1cc4d4e 100644 --- a/central_backend/src/server/servers.rs +++ b/central_backend/src/server/servers.rs @@ -3,6 +3,7 @@ use crate::constants; use crate::crypto::pki; use crate::energy::energy_actor::EnergyActorAddr; use crate::server::auth_middleware::AuthChecker; +use crate::server::devices_api::utils_controller; use crate::server::unsecure_server::*; use crate::server::web_api::*; use actix_cors::Cors; @@ -105,6 +106,7 @@ pub async fn secure_server(energy_actor: EnergyActorAddr) -> anyhow::Result<()> proxy: AppConfig::get().proxy_ip.clone(), })) .route("/", web::get().to(server_controller::secure_home)) + // Web API .route( "/web_api/server/config", web::get().to(server_controller::config), @@ -129,6 +131,11 @@ pub async fn secure_server(energy_actor: EnergyActorAddr) -> anyhow::Result<()> "/web_api/energy/cached_consumption", web::get().to(energy_controller::cached_consumption), ) + // Devices API + .route( + "/devices_api/utils/time", + web::get().to(utils_controller::curr_time), + ) }) .bind_openssl(&AppConfig::get().listen_address, builder)? .run() diff --git a/central_backend/src/utils/mod.rs b/central_backend/src/utils/mod.rs index b68ecd0..25676ba 100644 --- a/central_backend/src/utils/mod.rs +++ b/central_backend/src/utils/mod.rs @@ -1 +1,2 @@ pub mod files_utils; +pub mod time_utils; diff --git a/central_backend/src/utils/time_utils.rs b/central_backend/src/utils/time_utils.rs new file mode 100644 index 0000000..4c73ed9 --- /dev/null +++ b/central_backend/src/utils/time_utils.rs @@ -0,0 +1,10 @@ +use std::time::{SystemTime, UNIX_EPOCH}; + +/// Get the current time since epoch + +pub fn time_millis() -> u128 { + SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_millis() +}