diff --git a/smart-house-web/backend/Cargo.toml b/smart-house-web/backend/Cargo.toml index 50d15ae..00ddd0c 100644 --- a/smart-house-web/backend/Cargo.toml +++ b/smart-house-web/backend/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" edition = "2024" [dependencies] -axum = "0.8.9" -tracing = "0.1.44" -tracing-subscriber = "0.3.23" +tracing = "0.1" +tracing-subscriber = "0.3" +axum = "0.8" +tokio = { version = "1.52", features = ["macros", "rt-multi-thread"] } diff --git a/smart-house-web/backend/src/lib.rs b/smart-house-web/backend/src/lib.rs index fba8d83..1036c99 100644 --- a/smart-house-web/backend/src/lib.rs +++ b/smart-house-web/backend/src/lib.rs @@ -1,3 +1,8 @@ +const CODE_LOGGER_INITIALIZATION_ERROR: i32 = 1; +const CODE_TOKIO_RUNTIME_CREATION_ERROR: i32 = 2; +const CODE_LISTENER_BINDING_ERROR: i32 = 3; +const CODE_STARTIG_SERVER_ERROR: i32 = 4; + pub fn init_logger() { use tracing_subscriber::{Layer, layer::SubscriberExt, util::SubscriberInitExt}; @@ -9,8 +14,11 @@ pub fn init_logger() { .boxed(); if let Err(e) = tracing_subscriber::registry().with(vec![layer]).try_init() { eprintln!("Logger initialization failed: {:?}", e); - std::process::exit(1); + std::process::exit(CODE_LOGGER_INITIALIZATION_ERROR); } else { tracing::trace!("Logger succesfully initialized"); } } + +mod server; +pub use server::run_server; diff --git a/smart-house-web/backend/src/main.rs b/smart-house-web/backend/src/main.rs index 339cd95..da4e57b 100644 --- a/smart-house-web/backend/src/main.rs +++ b/smart-house-web/backend/src/main.rs @@ -1,6 +1,6 @@ -use backend::init_logger; +use backend::{init_logger, run_server}; fn main() { init_logger(); - println!("Hello, world!"); + run_server(); } diff --git a/smart-house-web/backend/src/server.rs b/smart-house-web/backend/src/server.rs new file mode 100644 index 0000000..21df591 --- /dev/null +++ b/smart-house-web/backend/src/server.rs @@ -0,0 +1,43 @@ +pub fn run_server() { + use std::process::exit; + use tracing::{error, info}; + + let runtime = match tokio::runtime::Builder::new_multi_thread() + .worker_threads(2) + .enable_all() + .build() + { + Ok(runtime) => runtime, + Err(e) => { + error!("Failed to create Tokio runtime: {:?}", e); + exit(crate::CODE_TOKIO_RUNTIME_CREATION_ERROR); + } + }; + runtime.block_on(async { + let app = axum::Router::new().fallback(fallback); + let addr = "127.0.0.1:8080"; + let listener = match tokio::net::TcpListener::bind(addr).await { + Ok(listener) => listener, + Err(e) => { + error!("Failed to bind listener to {}: {:?}", addr, e); + exit(crate::CODE_LISTENER_BINDING_ERROR); + } + }; + info!("Starting server at {}...", addr); + if let Err(e) = axum::serve(listener, app) + .with_graceful_shutdown(shutdown_signal()) + .await + { + error!("Failed to start server: {:?}", e); + exit(crate::CODE_STARTIG_SERVER_ERROR); + }; + 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() {}