smart-house-web: в работе
This commit is contained in:
@@ -4,6 +4,7 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
axum = "0.8.9"
|
tracing = "0.1"
|
||||||
tracing = "0.1.44"
|
tracing-subscriber = "0.3"
|
||||||
tracing-subscriber = "0.3.23"
|
axum = "0.8"
|
||||||
|
tokio = { version = "1.52", features = ["macros", "rt-multi-thread"] }
|
||||||
|
|||||||
@@ -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() {
|
pub fn init_logger() {
|
||||||
use tracing_subscriber::{Layer, layer::SubscriberExt, util::SubscriberInitExt};
|
use tracing_subscriber::{Layer, layer::SubscriberExt, util::SubscriberInitExt};
|
||||||
|
|
||||||
@@ -9,8 +14,11 @@ pub fn init_logger() {
|
|||||||
.boxed();
|
.boxed();
|
||||||
if let Err(e) = tracing_subscriber::registry().with(vec![layer]).try_init() {
|
if let Err(e) = tracing_subscriber::registry().with(vec![layer]).try_init() {
|
||||||
eprintln!("Logger initialization failed: {:?}", e);
|
eprintln!("Logger initialization failed: {:?}", e);
|
||||||
std::process::exit(1);
|
std::process::exit(CODE_LOGGER_INITIALIZATION_ERROR);
|
||||||
} else {
|
} else {
|
||||||
tracing::trace!("Logger succesfully initialized");
|
tracing::trace!("Logger succesfully initialized");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mod server;
|
||||||
|
pub use server::run_server;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use backend::init_logger;
|
use backend::{init_logger, run_server};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
init_logger();
|
init_logger();
|
||||||
println!("Hello, world!");
|
run_server();
|
||||||
}
|
}
|
||||||
|
|||||||
43
smart-house-web/backend/src/server.rs
Normal file
43
smart-house-web/backend/src/server.rs
Normal file
@@ -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() {}
|
||||||
Reference in New Issue
Block a user