diff --git a/smart-house-web/backend/src/lib.rs b/smart-house-web/backend/src/lib.rs index 9bd7010..082cf83 100644 --- a/smart-house-web/backend/src/lib.rs +++ b/smart-house-web/backend/src/lib.rs @@ -67,7 +67,7 @@ pub fn build_runtime() -> tokio::runtime::Runtime { } mod server; -pub use server::server_main; +pub use server::{axum_app, server_main}; mod house; pub use house::{Device, House, PowerSocket, Room, Thermometer}; diff --git a/smart-house-web/backend/src/main.rs b/smart-house-web/backend/src/main.rs index ce2b9a4..fda7766 100644 --- a/smart-house-web/backend/src/main.rs +++ b/smart-house-web/backend/src/main.rs @@ -1,7 +1,8 @@ -use backend::{build_runtime, init_logger, server_main}; +use backend::{axum_app, build_runtime, init_logger, server_main}; fn main() { init_logger(); let runtime = build_runtime(); - runtime.block_on(server_main()); + let app = axum_app(); + runtime.block_on(server_main(app)); } diff --git a/smart-house-web/backend/src/server.rs b/smart-house-web/backend/src/server.rs index cb31124..f48a28d 100644 --- a/smart-house-web/backend/src/server.rs +++ b/smart-house-web/backend/src/server.rs @@ -1,4 +1,7 @@ -use axum::routing::{delete, get, post, put}; +use axum::{ + Router, + routing::{delete, get, post, put}, +}; use std::{process::exit, sync::Arc}; use tokio::sync::RwLock; use tracing::{error, info}; @@ -8,11 +11,9 @@ use crate::House; /// Тип состояния type ServerState = Arc>; -/// Основная функция сервера -pub async fn server_main() { +pub fn axum_app() -> Router { let state: ServerState = Arc::new(RwLock::new(House::default())); - - let app = axum::Router::new() + axum::Router::new() // Тестовый эндпоинт для экспериментов .route("/debug", get(debug::debug)) // API дома @@ -29,7 +30,11 @@ pub async fn server_main() { .route("/room/{name}/device/{name}", delete(device::delete_device)) // Состояние и роут по-умолчанию .with_state(state) - .fallback(fallback); + .fallback(fallback) +} + +/// Основная функция сервера +pub async fn server_main(app: Router) { let addr = "127.0.0.1:8080"; let listener = match tokio::net::TcpListener::bind(addr).await { Ok(listener) => listener, diff --git a/smart-house-web/backend/tests/api_smoke_test.rs b/smart-house-web/backend/tests/api_smoke_test.rs index c71801b..f6094f4 100644 --- a/smart-house-web/backend/tests/api_smoke_test.rs +++ b/smart-house-web/backend/tests/api_smoke_test.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use backend::{Device, Room, init_logger, server_main}; +use backend::{Device, Room, axum_app, init_logger, server_main}; use reqwest::{Client, Response, StatusCode}; use serde_json::json; use tokio::spawn; @@ -11,7 +11,7 @@ type RqResult = Result; #[tokio::test(flavor = "current_thread")] async fn smoke() -> RqResult<()> { init_logger(); - spawn(server_main()); + spawn(server_main(axum_app())); let client = Client::new();