diff --git a/smart-house-web/backend/src/server.rs b/smart-house-web/backend/src/server.rs index 5ca00f2..a341e78 100644 --- a/smart-house-web/backend/src/server.rs +++ b/smart-house-web/backend/src/server.rs @@ -1,6 +1,5 @@ use axum::routing::{delete, get, put}; use std::{ - collections::HashMap, process::exit, sync::{ Arc, @@ -15,6 +14,7 @@ use crate::House; mod debug; mod rooms; +/// Запуск сервера pub fn run_server() { let runtime = match tokio::runtime::Builder::new_multi_thread() .name("tokio") @@ -37,13 +37,17 @@ pub fn run_server() { runtime.block_on(server_main()); } +/// Тип состояния type ServerState = Arc>; +/// Основная функция сервера async fn server_main() { - let state: ServerState = Arc::new(RwLock::new(House::new(HashMap::new()))); + let state: ServerState = Arc::new(RwLock::new(House::default())); let app = axum::Router::new() + // Тестовый эндпоинт для экспериментов .route("/debug", get(debug::debug)) + // API комнат .route("/rooms", get(rooms::get_rooms)) .route("/room", put(rooms::add_room)) .route("/room/{name}", delete(rooms::remove_room)) @@ -69,11 +73,13 @@ async fn server_main() { info!("Shutdown server"); } +/// Эндпоинт по-умолчанию async fn fallback() -> axum::response::Response { use axum::response::IntoResponse; (axum::http::StatusCode::NOT_FOUND, "404 NOT FOUND").into_response() } +/// Аккуратное завершение работы сервера async fn shutdown_signal() { // let timeout = async { // tokio::time::sleep(std::time::Duration::from_secs(10)).await; diff --git a/smart-house-web/backend/src/server/debug.rs b/smart-house-web/backend/src/server/debug.rs index 76d72a8..1dc9dbe 100644 --- a/smart-house-web/backend/src/server/debug.rs +++ b/smart-house-web/backend/src/server/debug.rs @@ -1,12 +1,5 @@ -use std::collections::HashMap; - use axum::{Json, extract::State}; -use crate::{Device, PowerSocket, Room, Thermometer}; - -pub async fn debug(State(_server_state): State) -> Json { - let mut map = HashMap::::with_capacity(16); - map.insert("therm_0".into(), Thermometer::new(22.5).into()); - map.insert("psock_0".into(), PowerSocket::new(12.1, false).into()); - Room::new(map).into() +pub async fn debug(State(_server_state): State) -> Json<(String, String)> { + ("ONE".into(), "TWO".into()).into() }